diff options
author | Jon Skeet <skeet@pobox.com> | 2011-01-27 20:47:23 +0000 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2011-01-27 20:47:23 +0000 |
commit | f31e34b17e24733bf98d84536ffa0c936719db9d (patch) | |
tree | 07f49c5e46418579324a18cc9dc4fb6c024f4d4a /src/ProtocolBuffers | |
parent | 12f0460a7121851832ded0f9b00ae9df720ea833 (diff) | |
download | protobuf-f31e34b17e24733bf98d84536ffa0c936719db9d.tar.gz protobuf-f31e34b17e24733bf98d84536ffa0c936719db9d.tar.bz2 protobuf-f31e34b17e24733bf98d84536ffa0c936719db9d.zip |
Default to Environment.NewLine for line breaks in ProtoGen, but allow it to be configured.
Diffstat (limited to 'src/ProtocolBuffers')
-rw-r--r-- | src/ProtocolBuffers/TextFormat.cs | 7 | ||||
-rw-r--r-- | src/ProtocolBuffers/TextGenerator.cs | 15 |
2 files changed, 16 insertions, 6 deletions
diff --git a/src/ProtocolBuffers/TextFormat.cs b/src/ProtocolBuffers/TextFormat.cs index 81b3447c..27bef8f4 100644 --- a/src/ProtocolBuffers/TextFormat.cs +++ b/src/ProtocolBuffers/TextFormat.cs @@ -43,7 +43,8 @@ using System.Collections; namespace Google.ProtocolBuffers { /// <summary> /// Provides ASCII text formatting support for messages. - /// TODO(jonskeet): Parsing support. + /// TODO(jonskeet): Support for alternative line endings. + /// (Easy to print, via TextGenerator. Not sure about parsing.) /// </summary> public static class TextFormat { @@ -52,7 +53,7 @@ namespace Google.ProtocolBuffers { /// the parameter output. /// </summary> public static void Print(IMessage message, TextWriter output) { - TextGenerator generator = new TextGenerator(output); + TextGenerator generator = new TextGenerator(output, "\n"); Print(message, generator); } @@ -60,7 +61,7 @@ namespace Google.ProtocolBuffers { /// Outputs a textual representation of <paramref name="fields" /> to <paramref name="output"/>. /// </summary> public static void Print(UnknownFieldSet fields, TextWriter output) { - TextGenerator generator = new TextGenerator(output); + TextGenerator generator = new TextGenerator(output, "\n"); PrintUnknownFields(fields, generator); } diff --git a/src/ProtocolBuffers/TextGenerator.cs b/src/ProtocolBuffers/TextGenerator.cs index 544fd86e..6b8fa048 100644 --- a/src/ProtocolBuffers/TextGenerator.cs +++ b/src/ProtocolBuffers/TextGenerator.cs @@ -44,6 +44,13 @@ namespace Google.ProtocolBuffers { public sealed class TextGenerator { /// <summary> + /// The string to use at the end of each line. We assume that "Print" is only called using \n + /// to indicate a line break; that's what we use to detect when we need to indent etc, and + /// *just* the \n is replaced with the contents of lineBreak. + /// </summary> + private readonly string lineBreak; + + /// <summary> /// Writer to write formatted text to. /// </summary> private readonly TextWriter writer; @@ -62,8 +69,9 @@ namespace Google.ProtocolBuffers { /// Creates a generator writing to the given writer. The writer /// is not closed by this class. /// </summary> - public TextGenerator(TextWriter writer) { + public TextGenerator(TextWriter writer, string lineBreak) { this.writer = writer; + this.lineBreak = lineBreak; } /// <summary> @@ -107,8 +115,9 @@ namespace Google.ProtocolBuffers { for (int i = 0; i < text.Length; i++) { if (text[i] == '\n') { - // TODO(jonskeet): Use Environment.NewLine? - Write(text.Substring(pos, i - pos + 1)); + // Strip off the \n from what we write + Write(text.Substring(pos, i - pos)); + Write(lineBreak); pos = i + 1; atStartOfLine = true; } |