Skip to content

Commit 21a4185

Browse files
committed
STYLE: Avoid unnecessary conversions from size_t to unsigned int
Make the index values consistent with size_t.
1 parent d117320 commit 21a4185

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

src/lib_json/json_reader.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ class OurFeatures {
888888
bool failIfExtra_;
889889
bool rejectDupKeys_;
890890
bool allowSpecialFloats_;
891-
int stackLimit_;
891+
size_t stackLimit_;
892892
}; // OurFeatures
893893

894894
// exact copy of Implementation of class Features
@@ -1087,7 +1087,7 @@ bool OurReader::parse(const char* beginDoc,
10871087

10881088
bool OurReader::readValue() {
10891089
// To preserve the old behaviour we cast size_t to int.
1090-
if (static_cast<int>(nodes_.size()) > features_.stackLimit_)
1090+
if (nodes_.size() > features_.stackLimit_)
10911091
throwRuntimeError("Exceeded stackLimit in readValue().");
10921092
Token token;
10931093
skipCommentTokens(token);
@@ -1917,7 +1917,11 @@ CharReader* CharReaderBuilder::newCharReader() const {
19171917
settings_["allowDroppedNullPlaceholders"].asBool();
19181918
features.allowNumericKeys_ = settings_["allowNumericKeys"].asBool();
19191919
features.allowSingleQuotes_ = settings_["allowSingleQuotes"].asBool();
1920-
features.stackLimit_ = settings_["stackLimit"].asInt();
1920+
#if defined(JSON_HAS_INT64)
1921+
features.stackLimit_ = settings_["stackLimit"].asUInt64();
1922+
#else
1923+
features.stackLimit_ = settings_["stackLimit"].asUInt();
1924+
#endif
19211925
features.failIfExtra_ = settings_["failIfExtra"].asBool();
19221926
features.rejectDupKeys_ = settings_["rejectDupKeys"].asBool();
19231927
features.allowSpecialFloats_ = settings_["allowSpecialFloats"].asBool();

src/test_lib_json/jsontest.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -224,18 +224,18 @@ Runner& Runner::add(TestCaseFactory factory) {
224224
return *this;
225225
}
226226

227-
unsigned int Runner::testCount() const {
228-
return static_cast<unsigned int>(tests_.size());
227+
size_t Runner::testCount() const {
228+
return tests_.size();
229229
}
230230

231-
JSONCPP_STRING Runner::testNameAt(unsigned int index) const {
231+
JSONCPP_STRING Runner::testNameAt(size_t index) const {
232232
TestCase* test = tests_[index]();
233233
JSONCPP_STRING name = test->testName();
234234
delete test;
235235
return name;
236236
}
237237

238-
void Runner::runTestAt(unsigned int index, TestResult& result) const {
238+
void Runner::runTestAt(size_t index, TestResult& result) const {
239239
TestCase* test = tests_[index]();
240240
result.setTestName(test->testName());
241241
printf("Testing %s: ", test->testName());
@@ -257,9 +257,9 @@ void Runner::runTestAt(unsigned int index, TestResult& result) const {
257257
}
258258

259259
bool Runner::runAllTest(bool printSummary) const {
260-
unsigned int count = testCount();
260+
size_t const count = testCount();
261261
std::deque<TestResult> failures;
262-
for (unsigned int index = 0; index < count; ++index) {
262+
for (size_t index = 0; index < count; ++index) {
263263
TestResult result;
264264
runTestAt(index, result);
265265
if (result.failed()) {
@@ -269,7 +269,7 @@ bool Runner::runAllTest(bool printSummary) const {
269269

270270
if (failures.empty()) {
271271
if (printSummary) {
272-
printf("All %u tests passed\n", count);
272+
printf("All %zu tests passed\n", count);
273273
}
274274
return true;
275275
} else {
@@ -278,19 +278,19 @@ bool Runner::runAllTest(bool printSummary) const {
278278
}
279279

280280
if (printSummary) {
281-
auto failedCount = static_cast<unsigned int>(failures.size());
282-
unsigned int passedCount = count - failedCount;
283-
printf("%u/%u tests passed (%u failure(s))\n", passedCount, count,
284-
failedCount);
281+
size_t const failedCount = failures.size();
282+
size_t const passedCount = count - failedCount;
283+
printf("%zu/%zu tests passed (%zu failure(s))\n",
284+
passedCount, count, failedCount);
285285
}
286286
return false;
287287
}
288288
}
289289

290290
bool Runner::testIndex(const JSONCPP_STRING& testName,
291-
unsigned int& indexOut) const {
292-
unsigned int count = testCount();
293-
for (unsigned int index = 0; index < count; ++index) {
291+
size_t& indexOut) const {
292+
const size_t count = testCount();
293+
for (size_t index = 0; index < count; ++index) {
294294
if (testNameAt(index) == testName) {
295295
indexOut = index;
296296
return true;
@@ -300,8 +300,8 @@ bool Runner::testIndex(const JSONCPP_STRING& testName,
300300
}
301301

302302
void Runner::listTests() const {
303-
unsigned int count = testCount();
304-
for (unsigned int index = 0; index < count; ++index) {
303+
const size_t count = testCount();
304+
for (size_t index = 0; index < count; ++index) {
305305
printf("%s\n", testNameAt(index).c_str());
306306
}
307307
}
@@ -319,7 +319,7 @@ int Runner::runCommandLine(int argc, const char* argv[]) const {
319319
} else if (opt == "--test") {
320320
++index;
321321
if (index < argc) {
322-
unsigned int testNameIndex;
322+
size_t testNameIndex;
323323
if (testIndex(argv[index], testNameIndex)) {
324324
subrunner.add(tests_[testNameIndex]);
325325
} else {

src/test_lib_json/jsontest.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ class Runner {
151151
bool runAllTest(bool printSummary) const;
152152

153153
/// Returns the number of test case in the suite
154-
unsigned int testCount() const;
154+
size_t testCount() const;
155155

156156
/// Returns the name of the test case at the specified index
157-
JSONCPP_STRING testNameAt(unsigned int index) const;
157+
JSONCPP_STRING testNameAt(size_t index) const;
158158

159159
/// Runs the test case at the specified index using the specified TestResult
160-
void runTestAt(unsigned int index, TestResult& result) const;
160+
void runTestAt(size_t index, TestResult& result) const;
161161

162162
static void printUsage(const char* appName);
163163

@@ -167,7 +167,7 @@ class Runner {
167167

168168
private:
169169
void listTests() const;
170-
bool testIndex(const JSONCPP_STRING& testName, unsigned int& indexOut) const;
170+
bool testIndex(const JSONCPP_STRING& testName, size_t& indexOut) const;
171171
static void preventDialogOnCrash();
172172

173173
private:

0 commit comments

Comments
 (0)