Skip to content

Commit 9b660bc

Browse files
d99krisYurunsoft
andauthored
add support for more integer types (#199)
* support more integer type conversions (#197) * Support more integer type conversions * remove unsigned int * follow-up to 376b317 - minimize test changes, fix ToStr handling of char types, add test for setting all numerical data types --------- Co-authored-by: Yurun <admin@yurunsoft.com>
1 parent 03133b5 commit 9b660bc

4 files changed

Lines changed: 153 additions & 3 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ if(RAPIDCSV_BUILD_TESTS)
188188
add_unit_test(test101)
189189
endif()
190190
add_unit_test(test102)
191+
add_unit_test(test103)
191192

192193
# perf tests
193194
add_perf_test(ptest001)

src/rapidcsv.h

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* rapidcsv.h
33
*
44
* URL: https://github.com/d99kris/rapidcsv
5-
* Version: 8.90
5+
* Version: 8.91
66
*
7-
* Copyright (C) 2017-2025 Kristofer Berggren
7+
* Copyright (C) 2017-2026 Kristofer Berggren
88
* All rights reserved.
99
*
1010
* rapidcsv is distributed under the BSD 3-Clause license, see LICENSE for details.
@@ -133,6 +133,8 @@ namespace rapidcsv
133133
void ToStr(const T& pVal, std::string& pStr) const
134134
{
135135
if (typeid(T) == typeid(int) ||
136+
typeid(T) == typeid(short) ||
137+
typeid(T) == typeid(unsigned short) ||
136138
typeid(T) == typeid(long) ||
137139
typeid(T) == typeid(long long) ||
138140
typeid(T) == typeid(unsigned) ||
@@ -157,6 +159,18 @@ namespace rapidcsv
157159
out << std::setprecision(17) << pVal;
158160
pStr = out.str();
159161
}
162+
else if (typeid(T) == typeid(signed char))
163+
{
164+
std::ostringstream out;
165+
out << static_cast<int>(pVal);
166+
pStr = out.str();
167+
}
168+
else if (typeid(T) == typeid(unsigned char))
169+
{
170+
std::ostringstream out;
171+
out << static_cast<unsigned int>(pVal);
172+
pStr = out.str();
173+
}
160174
else
161175
{
162176
throw no_converter();
@@ -177,6 +191,26 @@ namespace rapidcsv
177191
pVal = static_cast<T>(std::stoi(pStr));
178192
return;
179193
}
194+
else if (typeid(T) == typeid(signed char))
195+
{
196+
pVal = static_cast<T>(std::stoi(pStr));
197+
return;
198+
}
199+
else if (typeid(T) == typeid(unsigned char))
200+
{
201+
pVal = static_cast<T>(std::stoi(pStr));
202+
return;
203+
}
204+
else if (typeid(T) == typeid(short))
205+
{
206+
pVal = static_cast<T>(std::stoi(pStr));
207+
return;
208+
}
209+
else if (typeid(T) == typeid(unsigned short))
210+
{
211+
pVal = static_cast<T>(std::stoi(pStr));
212+
return;
213+
}
180214
else if (typeid(T) == typeid(long))
181215
{
182216
pVal = static_cast<T>(std::stol(pStr));

tests/test036.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// test036.cpp - supported datatypes
1+
// test036.cpp - get supported datatypes
22

33
#include <rapidcsv.h>
44
#include "unittest.h"
@@ -17,6 +17,10 @@ int main()
1717
"float,3.3E38\n"
1818
"double,1.6E308\n"
1919
"long double,1.6E308\n"
20+
"signed char,-128\n"
21+
"unsigned char,255\n"
22+
"short,32767\n"
23+
"unsigned short,65535\n"
2024
;
2125

2226
std::string path = unittest::TempPath();
@@ -42,6 +46,11 @@ int main()
4246

4347
long double longdoubleval = doc.GetCell<long double>(1, 8);
4448
unittest::ExpectTrue((longdoubleval > 1.5E308) && (longdoubleval < 1.7E308));
49+
50+
unittest::ExpectEqual(signed char, doc.GetCell<signed char>(1, 9), -128);
51+
unittest::ExpectEqual(unsigned char, doc.GetCell<unsigned char>(1, 10), 255);
52+
unittest::ExpectEqual(short, doc.GetCell<short>(1, 11), 32767);
53+
unittest::ExpectEqual(unsigned short, doc.GetCell<unsigned short>(1, 12), 65535);
4554
}
4655
catch (const std::exception& ex)
4756
{

tests/test103.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// test103.cpp - set supported datatypes
2+
3+
#include <rapidcsv.h>
4+
#include "unittest.h"
5+
6+
int main()
7+
{
8+
int rv = 0;
9+
10+
std::string csvref =
11+
"int,32767\n"
12+
"long,2147483647\n"
13+
"long long,9223372036854775807\n"
14+
"unsigned,65535\n"
15+
"unsigned long,4294967295\n"
16+
"unsigned long long,18446744073709551615\n"
17+
"float,3.40282347e+38\n"
18+
"double,1.6e+308\n"
19+
"long double,1.6e+308\n"
20+
"signed char,-128\n"
21+
"unsigned char,255\n"
22+
"short,32767\n"
23+
"unsigned short,65535\n"
24+
;
25+
26+
std::string csv =
27+
"int,0\n"
28+
"long,0\n"
29+
"long long,0\n"
30+
"unsigned,0\n"
31+
"unsigned long,0\n"
32+
"unsigned long long,0\n"
33+
"float,0\n"
34+
"double,0\n"
35+
"long double,0\n"
36+
"signed char,0\n"
37+
"unsigned char,0\n"
38+
"short,0\n"
39+
"unsigned short,0\n"
40+
;
41+
42+
std::string path = unittest::TempPath();
43+
unittest::WriteFile(path, csv);
44+
45+
try
46+
{
47+
rapidcsv::LabelParams labelParams(-1, -1);
48+
rapidcsv::SeparatorParams separatorParams(',', false, rapidcsv::sPlatformHasCR, false, false /*pAutoQuote*/);
49+
rapidcsv::Document doc(path, labelParams, separatorParams);
50+
51+
// set cells
52+
doc.SetCell<int>(1, 0, 32767);
53+
doc.SetCell<long>(1, 1, 2147483647);
54+
doc.SetCell<long long>(1, 2, 9223372036854775807);
55+
56+
doc.SetCell<unsigned>(1, 3, 65535);
57+
doc.SetCell<unsigned long>(1, 4, 4294967295);
58+
doc.SetCell<unsigned long long>(1, 5, 18446744073709551615llu);
59+
60+
doc.SetCell<float>(1, 6, 3.40282347e+38);
61+
doc.SetCell<double>(1, 7, 1.6E308);
62+
doc.SetCell<long double>(1, 8, 1.6E308);
63+
64+
doc.SetCell<signed char>(1, 9, -128);
65+
doc.SetCell<unsigned char>(1, 10, 255);
66+
doc.SetCell<short>(1, 11, 32767);
67+
doc.SetCell<unsigned short>(1, 12, 65535);
68+
69+
// read back
70+
unittest::ExpectEqual(int, doc.GetCell<int>(1, 0), 32767);
71+
unittest::ExpectEqual(long, doc.GetCell<long>(1, 1), 2147483647);
72+
unittest::ExpectEqual(long long, doc.GetCell<long long>(1, 2), 9223372036854775807);
73+
74+
unittest::ExpectEqual(unsigned, doc.GetCell<unsigned>(1, 3), 65535);
75+
unittest::ExpectEqual(unsigned long, doc.GetCell<unsigned long>(1, 4), 4294967295);
76+
unittest::ExpectEqual(unsigned long long, doc.GetCell<unsigned long long>(1, 5), 18446744073709551615llu);
77+
78+
float floatval = doc.GetCell<float>(1, 6);
79+
unittest::ExpectTrue((floatval > 3.40E38) && (floatval < 3.45E38));
80+
81+
double doubleval = doc.GetCell<double>(1, 7);
82+
unittest::ExpectTrue((doubleval > 1.5E308) && (doubleval < 1.7E308));
83+
84+
long double longdoubleval = doc.GetCell<long double>(1, 8);
85+
unittest::ExpectTrue((longdoubleval > 1.5E308) && (longdoubleval < 1.7E308));
86+
87+
unittest::ExpectEqual(signed char, doc.GetCell<signed char>(1, 9), -128);
88+
unittest::ExpectEqual(unsigned char, doc.GetCell<unsigned char>(1, 10), 255);
89+
unittest::ExpectEqual(short, doc.GetCell<short>(1, 11), 32767);
90+
unittest::ExpectEqual(unsigned short, doc.GetCell<unsigned short>(1, 12), 65535);
91+
92+
// check full doc
93+
doc.Save();
94+
std::string csvread = unittest::ReadFile(path);
95+
unittest::ExpectEqual(std::string, csvref, csvread);
96+
}
97+
catch (const std::exception& ex)
98+
{
99+
std::cout << ex.what() << std::endl;
100+
rv = 1;
101+
}
102+
103+
unittest::DeleteFile(path);
104+
105+
return rv;
106+
}

0 commit comments

Comments
 (0)