Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ if(RAPIDCSV_BUILD_TESTS)
add_unit_test(test102)
add_unit_test(test103)
add_unit_test(test104)
add_unit_test(test105)

# perf tests
add_perf_test(ptest001)
Expand Down
18 changes: 17 additions & 1 deletion src/rapidcsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* rapidcsv.h
*
* URL: https://github.com/d99kris/rapidcsv
* Version: 8.93
* Version: 8.94
*
* Copyright (C) 2017-2026 Kristofer Berggren
* All rights reserved.
Expand Down Expand Up @@ -1440,6 +1440,14 @@ namespace rapidcsv
}

const size_t dataColumnIdx = GetDataColumnIndex(pColumnIdx);

// remove old name from map before adding new one
const size_t nameRowIdx = static_cast<size_t>(mLabelParams.mColumnNameIdx);
if ((nameRowIdx < mData.size()) && (dataColumnIdx < mData.at(nameRowIdx).size()))
{
const std::string oldName = mData.at(nameRowIdx).at(dataColumnIdx);
mColumnNames.erase(oldName);
}
mColumnNames[pColumnName] = dataColumnIdx;

// increase table size if necessary:
Expand Down Expand Up @@ -1497,6 +1505,14 @@ namespace rapidcsv
void SetRowName(size_t pRowIdx, const std::string& pRowName)
{
const size_t dataRowIdx = GetDataRowIndex(pRowIdx);

// remove old name from map before adding new one
if ((mLabelParams.mRowNameIdx >= 0) && (dataRowIdx < mData.size()) &&
(static_cast<size_t>(mLabelParams.mRowNameIdx) < mData.at(dataRowIdx).size()))
{
const std::string oldName = mData.at(dataRowIdx).at(static_cast<size_t>(mLabelParams.mRowNameIdx));
mRowNames.erase(oldName);
}
mRowNames[pRowName] = dataRowIdx;
if (mLabelParams.mRowNameIdx < 0)
{
Expand Down
61 changes: 61 additions & 0 deletions tests/test105.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// test105.cpp - SetColumnName / SetRowName stale label cleanup

#include <rapidcsv.h>
#include "unittest.h"

int main()
{
int rv = 0;

std::string csv =
"name,value\n"
"row1,100\n"
"row2,200\n"
;

std::string path = unittest::TempPath();
unittest::WriteFile(path, csv);

try
{
// Test 1: SetColumnName removes old name from map
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0, 0));

unittest::ExpectEqual(int, doc.GetColumnIdx("value"), 0);
doc.SetColumnName(0, "price");
unittest::ExpectEqual(int, doc.GetColumnIdx("price"), 0);
unittest::ExpectEqual(int, doc.GetColumnIdx("value"), -1);
}

// Test 2: SetRowName removes old name from map
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0, 0));

unittest::ExpectEqual(int, doc.GetRowIdx("row1"), 0);
doc.SetRowName(0, "item1");
unittest::ExpectEqual(int, doc.GetRowIdx("item1"), 0);
unittest::ExpectEqual(int, doc.GetRowIdx("row1"), -1);
}

// Test 3: Data access by new name works correctly
{
rapidcsv::Document doc(path, rapidcsv::LabelParams(0, 0));

doc.SetColumnName(0, "price");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("price", "row1"), "100");

doc.SetRowName(0, "item1");
unittest::ExpectEqual(std::string, doc.GetCell<std::string>("price", "item1"), "100");
}
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
rv = 1;
}

unittest::DeleteFile(path);

return rv;
}
Loading