Skip to content

Commit 94665ea

Browse files
committed
copy fixes from StyledStreamWriter
1 parent 9e4bcf3 commit 94665ea

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/lib_json/json_writer.cpp

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,9 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
665665
value.hasComment(commentAfter);
666666
}
667667

668+
//////////////////////////
669+
// BuiltStyledStreamWriter
670+
668671
struct BuiltStyledStreamWriter : public StreamWriter
669672
{
670673
BuiltStyledStreamWriter(
@@ -683,8 +686,7 @@ struct BuiltStyledStreamWriter : public StreamWriter
683686
void unindent();
684687
void writeCommentBeforeValue(Value const& root);
685688
void writeCommentAfterValueOnSameLine(Value const& root);
686-
bool hasCommentForValue(const Value& value);
687-
static std::string normalizeEOL(std::string const& text);
689+
static bool hasCommentForValue(const Value& value);
688690

689691
typedef std::vector<std::string> ChildValues;
690692

@@ -693,7 +695,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
693695
int rightMargin_;
694696
std::string indentation_;
695697
CommentStyle cs_;
696-
bool addChildValues_;
698+
bool addChildValues_ : 1;
699+
bool indented_ : 1;
697700
};
698701
BuiltStyledStreamWriter::BuiltStyledStreamWriter(
699702
std::ostream* sout,
@@ -703,11 +706,14 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
703706
, rightMargin_(74)
704707
, indentation_(indentation)
705708
, cs_(cs)
709+
, addChildValues_(false)
710+
, indented_(false)
706711
{
707712
}
708713
int BuiltStyledStreamWriter::write(Value const& root)
709714
{
710715
addChildValues_ = false;
716+
indented_ = false;
711717
indentString_ = "";
712718
writeCommentBeforeValue(root);
713719
writeValue(root);
@@ -784,8 +790,10 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
784790
if (hasChildValue)
785791
writeWithIndent(childValues_[index]);
786792
else {
787-
writeIndent();
793+
if (!indented_) writeIndent();
794+
indented_ = true;
788795
writeValue(childValue);
796+
indented_ = false;
789797
}
790798
if (++index == size) {
791799
writeCommentAfterValueOnSameLine(childValue);
@@ -826,6 +834,9 @@ bool BuiltStyledStreamWriter::isMultineArray(Value const& value) {
826834
addChildValues_ = true;
827835
int lineLength = 4 + (size - 1) * 2; // '[ ' + ', '*n + ' ]'
828836
for (int index = 0; index < size; ++index) {
837+
if (hasCommentForValue(value[index])) {
838+
isMultiLine = true;
839+
}
829840
writeValue(value[index]);
830841
lineLength += int(childValues_[index].length());
831842
}
@@ -843,24 +854,17 @@ void BuiltStyledStreamWriter::pushValue(std::string const& value) {
843854
}
844855

845856
void BuiltStyledStreamWriter::writeIndent() {
846-
/*
847-
Some comments in this method would have been nice. ;-)
848-
849-
if ( !sout_.empty() )
850-
{
851-
char last = sout_[sout_.length()-1];
852-
if ( last == ' ' ) // already indented
853-
return;
854-
if ( last != '\n' ) // Comments may add new-line
855-
sout_ << '\n';
856-
}
857-
*/
857+
// blep intended this to look at the so-far-written string
858+
// to determine whether we are already indented, but
859+
// with a stream we cannot do that. So we rely on some saved state.
860+
// The caller checks indented_.
858861
sout_ << '\n' << indentString_;
859862
}
860863

861864
void BuiltStyledStreamWriter::writeWithIndent(std::string const& value) {
862-
writeIndent();
865+
if (!indented_) writeIndent();
863866
sout_ << value;
867+
indented_ = false;
864868
}
865869

866870
void BuiltStyledStreamWriter::indent() { indentString_ += indentation_; }
@@ -873,8 +877,22 @@ void BuiltStyledStreamWriter::unindent() {
873877
void BuiltStyledStreamWriter::writeCommentBeforeValue(Value const& root) {
874878
if (!root.hasComment(commentBefore))
875879
return;
876-
sout_ << root.getComment(commentBefore);
880+
877881
sout_ << "\n";
882+
writeIndent();
883+
const std::string& comment = root.getComment(commentBefore);
884+
std::string::const_iterator iter = comment.begin();
885+
while (iter != comment.end()) {
886+
sout_ << *iter;
887+
if (*iter == '\n' &&
888+
(iter != comment.end() && *(iter + 1) == '/'))
889+
writeIndent();
890+
++iter;
891+
}
892+
893+
// Comments are stripped of trailing newlines, so add one here
894+
sout_ << "\n";
895+
indented_ = false;
878896
}
879897

880898
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root) {
@@ -888,12 +906,16 @@ void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root
888906
}
889907
}
890908

909+
// static
891910
bool BuiltStyledStreamWriter::hasCommentForValue(const Value& value) {
892911
return value.hasComment(commentBefore) ||
893912
value.hasComment(commentAfterOnSameLine) ||
894913
value.hasComment(commentAfter);
895914
}
896915

916+
///////////////
917+
// StreamWriter
918+
897919
StreamWriter::StreamWriter(std::ostream* sout)
898920
: sout_(*sout)
899921
{

0 commit comments

Comments
 (0)