blob: b2a009c4e2144f43b606c15aec1f6c23898b1707 [file] [log] [blame]
csharptest980ba8d2010-11-07 16:30:39 -06001#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc. All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12// * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14// * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18// * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System;
36using System.Collections.Generic;
37using System.Collections;
csharptest272cb8a2010-11-09 20:49:12 -060038using System.Globalization;
39using Google.ProtocolBuffers.Descriptors;
csharptest980ba8d2010-11-07 16:30:39 -060040
41namespace Google.ProtocolBuffers {
42
43 /// <summary>
44 /// All generated protocol message classes extend this class. It implements
45 /// most of the IMessage interface using reflection. Users
46 /// can ignore this class as an implementation detail.
47 /// </summary>
48 public abstract class GeneratedMessageLite<TMessage, TBuilder> : AbstractMessageLite<TMessage, TBuilder>
49 where TMessage : GeneratedMessageLite<TMessage, TBuilder>
50 where TBuilder : GeneratedBuilderLite<TMessage, TBuilder> {
51
52 protected abstract TMessage ThisMessage { get; }
csharptest272cb8a2010-11-09 20:49:12 -060053
54 public sealed override string ToString() {
55 using (System.IO.StringWriter wtr = new System.IO.StringWriter()) {
56 PrintTo(wtr);
57 return wtr.ToString();
58 }
59 }
60
61 /// <summary>
62 /// PrintTo() helper methods for Lite Runtime
63 /// </summary>
64 protected static void PrintField<T>(string name, IList<T> value, System.IO.TextWriter writer) {
65 foreach (T item in value)
66 PrintField(name, true, (object)item, writer);
67 }
68 /// <summary>
69 /// PrintTo() helper methods for Lite Runtime
70 /// </summary>
71 protected static void PrintField(string name, bool hasValue, object value, System.IO.TextWriter writer) {
72 if (!hasValue) return;
73 if (value is IMessageLite) {
74 writer.WriteLine("{0} {{", name);
75 ((IMessageLite)value).PrintTo(writer);
76 writer.WriteLine("}");
77 } else if (value is ByteString || value is String) {
78 writer.Write("{0}: \"", name);
79 if(value is String)
80 EscapeBytes( System.Text.Encoding.UTF8.GetBytes((string)value), writer);
81 else
82 EscapeBytes(((ByteString)value), writer);
83 writer.WriteLine("\"");
84 } else if (value is bool) {
85 writer.WriteLine("{0}: {1}", name, (bool)value ? "true" : "false");
86 } else if (value is IEnumLite) {
87 writer.WriteLine("{0}: {1}", name, ((IEnumLite)value).Name);
88 }
89 else {
90 writer.WriteLine("{0}: {1}", name, ((IConvertible)value).ToString(CultureInfo.InvariantCulture));
91 }
92 }
93
94 /// <summary>
95 /// COPIED from TextFormat
96 /// Escapes bytes in the format used in protocol buffer text format, which
97 /// is the same as the format used for C string literals. All bytes
98 /// that are not printable 7-bit ASCII characters are escaped, as well as
99 /// backslash, single-quote, and double-quote characters. Characters for
100 /// which no defined short-hand escape sequence is defined will be escaped
101 /// using 3-digit octal sequences.
102 /// The returned value is guaranteed to be entirely ASCII.
103 /// </summary>
104 private static void EscapeBytes(IEnumerable<byte> input, System.IO.TextWriter writer) {
105 foreach (byte b in input) {
106 switch (b) {
107 // C# does not use \a or \v
108 case 0x07: writer.Write("\\a"); break;
109 case (byte)'\b': writer.Write("\\b"); break;
110 case (byte)'\f': writer.Write("\\f"); break;
111 case (byte)'\n': writer.Write("\\n"); break;
112 case (byte)'\r': writer.Write("\\r"); break;
113 case (byte)'\t': writer.Write("\\t"); break;
114 case 0x0b: writer.Write("\\v"); break;
115 case (byte)'\\': writer.Write("\\\\"); break;
116 case (byte)'\'': writer.Write("\\\'"); break;
117 case (byte)'"': writer.Write("\\\""); break;
118 default:
119 if (b >= 0x20 && b < 128) {
120 writer.Write((char)b);
121 } else {
122 writer.Write('\\');
123 writer.Write((char)('0' + ((b >> 6) & 3)));
124 writer.Write((char)('0' + ((b >> 3) & 7)));
125 writer.Write((char)('0' + (b & 7)));
126 }
127 break;
128 }
129 }
130 }
csharptest980ba8d2010-11-07 16:30:39 -0600131 }
132}