Merge pull request #125 from cdunn2001/issue-116

1.2.1

Fix issue #116, DOS line-endings. Never output \r.
This commit is contained in:
Christopher Dunn 2015-01-20 15:07:47 -06:00
commit ec251df6b7
4 changed files with 47 additions and 62 deletions

View File

@ -4,10 +4,10 @@
#ifndef JSON_VERSION_H_INCLUDED #ifndef JSON_VERSION_H_INCLUDED
# define JSON_VERSION_H_INCLUDED # define JSON_VERSION_H_INCLUDED
# define JSONCPP_VERSION_STRING "1.2.0" # define JSONCPP_VERSION_STRING "1.2.1"
# define JSONCPP_VERSION_MAJOR 1 # define JSONCPP_VERSION_MAJOR 1
# define JSONCPP_VERSION_MINOR 2 # define JSONCPP_VERSION_MINOR 2
# define JSONCPP_VERSION_PATCH 0 # define JSONCPP_VERSION_PATCH 1
# define JSONCPP_VERSION_QUALIFIER # define JSONCPP_VERSION_QUALIFIER
# define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8)) # define JSONCPP_VERSION_HEXA ((JSONCPP_VERSION_MAJOR << 24) | (JSONCPP_VERSION_MINOR << 16) | (JSONCPP_VERSION_PATCH << 8))

View File

@ -135,14 +135,9 @@ bool Reader::readValue() {
bool successful = true; bool successful = true;
if (collectComments_ && !commentsBefore_.empty()) { if (collectComments_ && !commentsBefore_.empty()) {
// Remove newline characters at the end of the comments // Remove newline at the end of the comment
size_t lastNonNewline = commentsBefore_.find_last_not_of("\r\n"); if (commentsBefore_[commentsBefore_.size() - 1] == '\n')
if (lastNonNewline != std::string::npos) { commentsBefore_.resize(commentsBefore_.size() - 1);
commentsBefore_.erase(lastNonNewline + 1);
} else {
commentsBefore_.clear();
}
currentValue().setComment(commentsBefore_, commentBefore); currentValue().setComment(commentsBefore_, commentBefore);
commentsBefore_ = ""; commentsBefore_ = "";
} }
@ -337,14 +332,34 @@ bool Reader::readComment() {
return true; return true;
} }
static std::string normalizeEOL(Reader::Location begin, Reader::Location end) {
std::string normalized;
normalized.reserve(end - begin);
Reader::Location current = begin;
while (current != end) {
char c = *current++;
if (c == '\r') {
if (current != end && *current == '\n')
// convert dos EOL
++current;
// convert Mac EOL
normalized += '\n';
} else {
normalized += c;
}
}
return normalized;
}
void void
Reader::addComment(Location begin, Location end, CommentPlacement placement) { Reader::addComment(Location begin, Location end, CommentPlacement placement) {
assert(collectComments_); assert(collectComments_);
const std::string& normalized = normalizeEOL(begin, end);
if (placement == commentAfterOnSameLine) { if (placement == commentAfterOnSameLine) {
assert(lastValue_ != 0); assert(lastValue_ != 0);
lastValue_->setComment(std::string(begin, end), placement); lastValue_->setComment(normalized, placement);
} else { } else {
commentsBefore_ += std::string(begin, end); commentsBefore_ += normalized;
} }
} }
@ -360,8 +375,15 @@ bool Reader::readCStyleComment() {
bool Reader::readCppStyleComment() { bool Reader::readCppStyleComment() {
while (current_ != end_) { while (current_ != end_) {
Char c = getNextChar(); Char c = getNextChar();
if (c == '\r' || c == '\n') if (c == '\n')
break; break;
if (c == '\r') {
// Consume DOS EOL. It will be normalized in addComment.
if (current_ != end_ && *current_ == '\n')
getNextChar();
// Break on Moc OS 9 EOL.
break;
}
} }
return true; return true;
} }

View File

@ -421,26 +421,27 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) {
document_ += "\n"; document_ += "\n";
writeIndent(); writeIndent();
std::string normalizedComment = normalizeEOL(root.getComment(commentBefore)); const std::string& comment = root.getComment(commentBefore);
std::string::const_iterator iter = normalizedComment.begin(); std::string::const_iterator iter = comment.begin();
while (iter != normalizedComment.end()) { while (iter != comment.end()) {
document_ += *iter; document_ += *iter;
if (*iter == '\n' && *(iter + 1) == '/') if (*iter == '\n' &&
(iter != comment.end() && *(iter + 1) == '/'))
writeIndent(); writeIndent();
++iter; ++iter;
} }
// Comments are stripped of newlines, so add one here // Comments are stripped of trailing newlines, so add one here
document_ += "\n"; document_ += "\n";
} }
void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) { void StyledWriter::writeCommentAfterValueOnSameLine(const Value& root) {
if (root.hasComment(commentAfterOnSameLine)) if (root.hasComment(commentAfterOnSameLine))
document_ += " " + normalizeEOL(root.getComment(commentAfterOnSameLine)); document_ += " " + root.getComment(commentAfterOnSameLine);
if (root.hasComment(commentAfter)) { if (root.hasComment(commentAfter)) {
document_ += "\n"; document_ += "\n";
document_ += normalizeEOL(root.getComment(commentAfter)); document_ += root.getComment(commentAfter);
document_ += "\n"; document_ += "\n";
} }
} }
@ -451,25 +452,6 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
value.hasComment(commentAfter); value.hasComment(commentAfter);
} }
std::string StyledWriter::normalizeEOL(const std::string& text) {
std::string normalized;
normalized.reserve(text.length());
const char* begin = text.c_str();
const char* end = begin + text.length();
const char* current = begin;
while (current != end) {
char c = *current++;
if (c == '\r') // mac or dos EOL
{
if (*current == '\n') // convert dos EOL
++current;
normalized += '\n';
} else // handle unix EOL & other char
normalized += c;
}
return normalized;
}
// Class StyledStreamWriter // Class StyledStreamWriter
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
@ -646,17 +628,17 @@ void StyledStreamWriter::unindent() {
void StyledStreamWriter::writeCommentBeforeValue(const Value& root) { void StyledStreamWriter::writeCommentBeforeValue(const Value& root) {
if (!root.hasComment(commentBefore)) if (!root.hasComment(commentBefore))
return; return;
*document_ << normalizeEOL(root.getComment(commentBefore)); *document_ << root.getComment(commentBefore);
*document_ << "\n"; *document_ << "\n";
} }
void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) { void StyledStreamWriter::writeCommentAfterValueOnSameLine(const Value& root) {
if (root.hasComment(commentAfterOnSameLine)) if (root.hasComment(commentAfterOnSameLine))
*document_ << " " + normalizeEOL(root.getComment(commentAfterOnSameLine)); *document_ << " " + root.getComment(commentAfterOnSameLine);
if (root.hasComment(commentAfter)) { if (root.hasComment(commentAfter)) {
*document_ << "\n"; *document_ << "\n";
*document_ << normalizeEOL(root.getComment(commentAfter)); *document_ << root.getComment(commentAfter);
*document_ << "\n"; *document_ << "\n";
} }
} }
@ -667,25 +649,6 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
value.hasComment(commentAfter); value.hasComment(commentAfter);
} }
std::string StyledStreamWriter::normalizeEOL(const std::string& text) {
std::string normalized;
normalized.reserve(text.length());
const char* begin = text.c_str();
const char* end = begin + text.length();
const char* current = begin;
while (current != end) {
char c = *current++;
if (c == '\r') // mac or dos EOL
{
if (*current == '\n') // convert dos EOL
++current;
normalized += '\n';
} else // handle unix EOL & other char
normalized += c;
}
return normalized;
}
std::ostream& operator<<(std::ostream& sout, const Value& root) { std::ostream& operator<<(std::ostream& sout, const Value& root) {
Json::StyledStreamWriter writer; Json::StyledStreamWriter writer;
writer.write(sout, root); writer.write(sout, root);

View File

@ -1 +1 @@
1.2.0 1.2.1