-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Code modernization (empty, for-range, auto) #787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Use auto when declaring iterators Use auto when initializing with a cast to avoid duplicating the type name
In the library code, yes. But Please consider the assertions carefully.
When they fail, they should be able to print some information about what
was expected and what was received, and a size() or a string value is
better diagnostic information than just whether something is not empty().
On Mon, Jun 4, 2018 at 4:53 PM Marian Klymov ***@***.***> wrote:
Using empty() increases readability.
------------------------------
You can view, comment on, or merge this pull request online at:
#787
Commit Summary
- Use empty() for emptiness checks.
File Changes
- *M* src/lib_json/json_reader.cpp
<https://github.com/open-source-parsers/jsoncpp/pull/787/files#diff-0>
(4)
- *M* src/lib_json/json_value.cpp
<https://github.com/open-source-parsers/jsoncpp/pull/787/files#diff-1>
(4)
- *M* src/test_lib_json/main.cpp
<https://github.com/open-source-parsers/jsoncpp/pull/787/files#diff-2>
(75)
Patch Links:
- https://github.com/open-source-parsers/jsoncpp/pull/787.patch
- https://github.com/open-source-parsers/jsoncpp/pull/787.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#787>, or mute the
thread
<https://github.com/notifications/unsubscribe-auth/AHFzPLHmt9i9wipFE5zk8inUdBTMgDzLks5t5Z5CgaJpZM4UZw9X>
.
--
ǝnɥɐuop ʎllıq
|
src/test_lib_json/jsontest.cpp
Outdated
@@ -436,4 +436,16 @@ TestResult& checkStringEqual(TestResult& result, | |||
return result; | |||
} | |||
|
|||
void checkStringEmpty(TestResult& result, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of void
, return a reference to result
, to match checkStringEqual
and allow for operator<< chaining.
src/test_lib_json/jsontest.h
Outdated
@@ -239,6 +245,10 @@ TestResult& checkStringEqual(TestResult& result, | |||
JsonTest::ToJsonString(actual), __FILE__, \ | |||
__LINE__, #expected " == " #actual) | |||
|
|||
/// \brief Asserts that string is empty. | |||
#define JSONTEST_ASSERT_STRING_EMPTY(actual) \ | |||
JsonTest::checkStringEmpty(*result_, (actual), __FILE__, __LINE__, #actual); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No semicolon here.
These macros are supposed to fluently take trailing extra info added with operator<< on TestResult&.
src/jsontestrunner/main.cpp
Outdated
fseek(file, 0, SEEK_SET); | ||
JSONCPP_STRING text; | ||
char* buffer = new char[size + 1]; | ||
auto* buffer = new char[size + 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The text
string is sitting right here, willing to manage a memory block of char
for us.
We can just use text
's buffer space directly and skip buffer
.
src/lib_json/json_reader.cpp
Outdated
@@ -583,7 +583,7 @@ bool Reader::decodeNumber(Token& token, Value& decoded) { | |||
Char c = *current++; | |||
if (c < '0' || c > '9') | |||
return decodeDouble(token, decoded); | |||
Value::UInt digit(static_cast<Value::UInt>(c - '0')); | |||
auto digit(static_cast<Value::UInt>(c - '0')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would be better for me with an = initializer.
auto digit = ...
src/lib_json/json_reader.cpp
Outdated
@@ -1579,7 +1576,7 @@ bool OurReader::decodeNumber(Token& token, Value& decoded) { | |||
Char c = *current++; | |||
if (c < '0' || c > '9') | |||
return decodeDouble(token, decoded); | |||
Value::UInt digit(static_cast<Value::UInt>(c - '0')); | |||
auto digit(static_cast<Value::UInt>(c - '0')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto var = initializer;
src/lib_json/json_reader.cpp
Outdated
itError != errors_.end(); ++itError) { | ||
const ErrorInfo& error = *itError; | ||
Reader::StructuredError structured; | ||
allErrors.reserve(errors_.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::vector<Reader::StructuredError> allErrors;
allErrors.reserve(errors_.size());
std::transform(errors_.begin(), errors_.end(), std::back_inserter(allErrors),
[](const ErrorInfo& e) -> Reader::StructuredError {
return {e.token_.start_ - begin_,
e.token_.end_ - begin_,
e.message_};
});
return allErrors;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMHO code without too much std boilerplate might be better
for (const ErrorInfo& e : errors_) {
allErrors.push_back(
{ e.token_.start_ - begin_, e.token_.end_ - begin_, e.message_ });
}
src/lib_json/json_reader.cpp
Outdated
itError != errors_.end(); ++itError) { | ||
const ErrorInfo& error = *itError; | ||
OurReader::StructuredError structured; | ||
allErrors.reserve(errors_.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as Reader:: code above.
src/lib_json/json_value.cpp
Outdated
@@ -107,7 +107,7 @@ static inline char* duplicateStringValue(const char* value, size_t length) { | |||
if (length >= static_cast<size_t>(Value::maxInt)) | |||
length = Value::maxInt - 1; | |||
|
|||
char* newString = static_cast<char*>(malloc(length + 1)); | |||
auto* newString = static_cast<char*>(malloc(length + 1)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not much value here.
src/test_lib_json/jsontest.cpp
Outdated
@@ -275,13 +273,12 @@ bool Runner::runAllTest(bool printSummary) const { | |||
} | |||
return true; | |||
} else { | |||
for (unsigned int index = 0; index < failures.size(); ++index) { | |||
TestResult& result = failures[index]; | |||
for (const TestResult& result : failures) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're adding constness here, and printFailure sounds like it might be or soon become non-const. let's leave off the const.
Rewrite readInputTestFile Small changes from pull request review
I'll let @BillyDonahue handle this. And we probably cannot merge until TravisCI is fixed. (They seem to have dropped gcc-4.9. Not sure yet.) |
TravisCI is fixed (with Meson for MSVC). This should be rebased to |
@Nekto89 Can you please rebase this work on top of the current master branch and address the issues. I'm making a push to remove the backlog of PR issues. If you can no longer work on this PR, just let me know and I'll close the PR. |
Using empty() increases readability.