@@ -421,26 +421,27 @@ void StyledWriter::writeCommentBeforeValue(const Value& root) {
421421
422422 document_ += " \n " ;
423423 writeIndent ();
424- std::string normalizedComment = normalizeEOL ( root.getComment (commentBefore) );
425- std::string::const_iterator iter = normalizedComment .begin ();
426- while (iter != normalizedComment .end ()) {
424+ const std::string& comment = root.getComment (commentBefore);
425+ std::string::const_iterator iter = comment .begin ();
426+ while (iter != comment .end ()) {
427427 document_ += *iter;
428- if (*iter == ' \n ' && *(iter + 1 ) == ' /' )
428+ if (*iter == ' \n ' &&
429+ (iter != comment.end () && *(iter + 1 ) == ' /' ))
429430 writeIndent ();
430431 ++iter;
431432 }
432433
433- // Comments are stripped of newlines, so add one here
434+ // Comments are stripped of trailing newlines, so add one here
434435 document_ += " \n " ;
435436}
436437
437438void StyledWriter::writeCommentAfterValueOnSameLine (const Value& root) {
438439 if (root.hasComment (commentAfterOnSameLine))
439- document_ += " " + normalizeEOL ( root.getComment (commentAfterOnSameLine) );
440+ document_ += " " + root.getComment (commentAfterOnSameLine);
440441
441442 if (root.hasComment (commentAfter)) {
442443 document_ += " \n " ;
443- document_ += normalizeEOL ( root.getComment (commentAfter) );
444+ document_ += root.getComment (commentAfter);
444445 document_ += " \n " ;
445446 }
446447}
@@ -451,25 +452,6 @@ bool StyledWriter::hasCommentForValue(const Value& value) {
451452 value.hasComment (commentAfter);
452453}
453454
454- std::string StyledWriter::normalizeEOL (const std::string& text) {
455- std::string normalized;
456- normalized.reserve (text.length ());
457- const char * begin = text.c_str ();
458- const char * end = begin + text.length ();
459- const char * current = begin;
460- while (current != end) {
461- char c = *current++;
462- if (c == ' \r ' ) // mac or dos EOL
463- {
464- if (*current == ' \n ' ) // convert dos EOL
465- ++current;
466- normalized += ' \n ' ;
467- } else // handle unix EOL & other char
468- normalized += c;
469- }
470- return normalized;
471- }
472-
473455// Class StyledStreamWriter
474456// //////////////////////////////////////////////////////////////////
475457
@@ -646,17 +628,17 @@ void StyledStreamWriter::unindent() {
646628void StyledStreamWriter::writeCommentBeforeValue (const Value& root) {
647629 if (!root.hasComment (commentBefore))
648630 return ;
649- *document_ << normalizeEOL ( root.getComment (commentBefore) );
631+ *document_ << root.getComment (commentBefore);
650632 *document_ << " \n " ;
651633}
652634
653635void StyledStreamWriter::writeCommentAfterValueOnSameLine (const Value& root) {
654636 if (root.hasComment (commentAfterOnSameLine))
655- *document_ << " " + normalizeEOL ( root.getComment (commentAfterOnSameLine) );
637+ *document_ << " " + root.getComment (commentAfterOnSameLine);
656638
657639 if (root.hasComment (commentAfter)) {
658640 *document_ << " \n " ;
659- *document_ << normalizeEOL ( root.getComment (commentAfter) );
641+ *document_ << root.getComment (commentAfter);
660642 *document_ << " \n " ;
661643 }
662644}
@@ -667,25 +649,6 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
667649 value.hasComment (commentAfter);
668650}
669651
670- std::string StyledStreamWriter::normalizeEOL (const std::string& text) {
671- std::string normalized;
672- normalized.reserve (text.length ());
673- const char * begin = text.c_str ();
674- const char * end = begin + text.length ();
675- const char * current = begin;
676- while (current != end) {
677- char c = *current++;
678- if (c == ' \r ' ) // mac or dos EOL
679- {
680- if (*current == ' \n ' ) // convert dos EOL
681- ++current;
682- normalized += ' \n ' ;
683- } else // handle unix EOL & other char
684- normalized += c;
685- }
686- return normalized;
687- }
688-
689652std::ostream& operator <<(std::ostream& sout, const Value& root) {
690653 Json::StyledStreamWriter writer;
691654 writer.write (sout, root);
0 commit comments