@@ -665,6 +665,9 @@ bool StyledStreamWriter::hasCommentForValue(const Value& value) {
665
665
value.hasComment (commentAfter);
666
666
}
667
667
668
+ // ////////////////////////
669
+ // BuiltStyledStreamWriter
670
+
668
671
struct BuiltStyledStreamWriter : public StreamWriter
669
672
{
670
673
BuiltStyledStreamWriter (
@@ -683,8 +686,7 @@ struct BuiltStyledStreamWriter : public StreamWriter
683
686
void unindent ();
684
687
void writeCommentBeforeValue (Value const & root);
685
688
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);
688
690
689
691
typedef std::vector<std::string> ChildValues;
690
692
@@ -693,7 +695,8 @@ struct BuiltStyledStreamWriter : public StreamWriter
693
695
int rightMargin_;
694
696
std::string indentation_;
695
697
CommentStyle cs_;
696
- bool addChildValues_;
698
+ bool addChildValues_ : 1 ;
699
+ bool indented_ : 1 ;
697
700
};
698
701
BuiltStyledStreamWriter::BuiltStyledStreamWriter (
699
702
std::ostream* sout,
@@ -703,11 +706,14 @@ BuiltStyledStreamWriter::BuiltStyledStreamWriter(
703
706
, rightMargin_(74 )
704
707
, indentation_(indentation)
705
708
, cs_(cs)
709
+ , addChildValues_(false )
710
+ , indented_(false )
706
711
{
707
712
}
708
713
int BuiltStyledStreamWriter::write (Value const & root)
709
714
{
710
715
addChildValues_ = false ;
716
+ indented_ = false ;
711
717
indentString_ = " " ;
712
718
writeCommentBeforeValue (root);
713
719
writeValue (root);
@@ -784,8 +790,10 @@ void BuiltStyledStreamWriter::writeArrayValue(Value const& value) {
784
790
if (hasChildValue)
785
791
writeWithIndent (childValues_[index]);
786
792
else {
787
- writeIndent ();
793
+ if (!indented_) writeIndent ();
794
+ indented_ = true ;
788
795
writeValue (childValue);
796
+ indented_ = false ;
789
797
}
790
798
if (++index == size) {
791
799
writeCommentAfterValueOnSameLine (childValue);
@@ -826,6 +834,9 @@ bool BuiltStyledStreamWriter::isMultineArray(Value const& value) {
826
834
addChildValues_ = true ;
827
835
int lineLength = 4 + (size - 1 ) * 2 ; // '[ ' + ', '*n + ' ]'
828
836
for (int index = 0 ; index < size; ++index) {
837
+ if (hasCommentForValue (value[index])) {
838
+ isMultiLine = true ;
839
+ }
829
840
writeValue (value[index]);
830
841
lineLength += int (childValues_[index].length ());
831
842
}
@@ -843,24 +854,17 @@ void BuiltStyledStreamWriter::pushValue(std::string const& value) {
843
854
}
844
855
845
856
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_.
858
861
sout_ << ' \n ' << indentString_;
859
862
}
860
863
861
864
void BuiltStyledStreamWriter::writeWithIndent (std::string const & value) {
862
- writeIndent ();
865
+ if (!indented_) writeIndent ();
863
866
sout_ << value;
867
+ indented_ = false ;
864
868
}
865
869
866
870
void BuiltStyledStreamWriter::indent () { indentString_ += indentation_; }
@@ -873,8 +877,22 @@ void BuiltStyledStreamWriter::unindent() {
873
877
void BuiltStyledStreamWriter::writeCommentBeforeValue (Value const & root) {
874
878
if (!root.hasComment (commentBefore))
875
879
return ;
876
- sout_ << root. getComment (commentBefore);
880
+
877
881
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 ;
878
896
}
879
897
880
898
void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine (Value const & root) {
@@ -888,12 +906,16 @@ void BuiltStyledStreamWriter::writeCommentAfterValueOnSameLine(Value const& root
888
906
}
889
907
}
890
908
909
+ // static
891
910
bool BuiltStyledStreamWriter::hasCommentForValue (const Value& value) {
892
911
return value.hasComment (commentBefore) ||
893
912
value.hasComment (commentAfterOnSameLine) ||
894
913
value.hasComment (commentAfter);
895
914
}
896
915
916
+ // /////////////
917
+ // StreamWriter
918
+
897
919
StreamWriter::StreamWriter (std::ostream* sout)
898
920
: sout_(*sout)
899
921
{
0 commit comments