Using List<Char> instead of StringBuilder for building strings from chars

pull/288/head
csharptest 14 years ago committed by rogerk
parent afe844bc95
commit ddb74eb6a4
  1. 35
      src/ProtocolBuffers/Serialization/JsonTextCursor.cs

@ -156,7 +156,7 @@ namespace Google.ProtocolBuffers.Serialization
{ {
SkipWhitespace(); SkipWhitespace();
Consume('"'); Consume('"');
StringBuilder sb = new StringBuilder(); List<Char> sb = new List<char>(100);
while (_next != '"') while (_next != '"')
{ {
if (_next == '\\') if (_next == '\\')
@ -165,54 +165,53 @@ namespace Google.ProtocolBuffers.Serialization
char ch = ReadChar(); char ch = ReadChar();
switch (ch) switch (ch)
{ {
case 'b': sb.Append('\b'); break; case 'b': sb.Add('\b'); break;
case 'f': sb.Append('\f'); break; case 'f': sb.Add('\f'); break;
case 'n': sb.Append('\n'); break; case 'n': sb.Add('\n'); break;
case 'r': sb.Append('\r'); break; case 'r': sb.Add('\r'); break;
case 't': sb.Append('\t'); break; case 't': sb.Add('\t'); break;
case 'u': case 'u':
{ {
string hex = new string(new char[] { ReadChar(), ReadChar(), ReadChar(), ReadChar() }); string hex = new string(new char[] { ReadChar(), ReadChar(), ReadChar(), ReadChar() });
int result; int result;
Assert(int.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result), Assert(int.TryParse(hex, NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result),
"Expected a 4-character hex specifier."); "Expected a 4-character hex specifier.");
sb.Append((char)result); sb.Add((char)result);
break; break;
} }
default: default:
sb.Append(ch); break; sb.Add(ch); break;
} }
} }
else else
{ {
Assert(_next != '\n' && _next != '\r' && _next != '\f' && _next != -1, '"'); Assert(_next != '\n' && _next != '\r' && _next != '\f' && _next != -1, '"');
sb.Append(ReadChar()); sb.Add(ReadChar());
} }
} }
Consume('"'); Consume('"');
return sb.ToString(); return new String(sb.ToArray());
} }
public string ReadNumber() public string ReadNumber()
{ {
SkipWhitespace(); SkipWhitespace();
List<Char> sb = new List<char>(24);
StringBuilder sb = new StringBuilder();
if (_next == '-') if (_next == '-')
sb.Append(ReadChar()); sb.Add(ReadChar());
Assert(_next >= '0' && _next <= '9', "Expected a numeric type."); Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while ((_next >= '0' && _next <= '9') || _next == '.') while ((_next >= '0' && _next <= '9') || _next == '.')
sb.Append(ReadChar()); sb.Add(ReadChar());
if (_next == 'e' || _next == 'E') if (_next == 'e' || _next == 'E')
{ {
sb.Append(ReadChar()); sb.Add(ReadChar());
if (_next == '-' || _next == '+') if (_next == '-' || _next == '+')
sb.Append(ReadChar()); sb.Add(ReadChar());
Assert(_next >= '0' && _next <= '9', "Expected a numeric type."); Assert(_next >= '0' && _next <= '9', "Expected a numeric type.");
while (_next >= '0' && _next <= '9') while (_next >= '0' && _next <= '9')
sb.Append(ReadChar()); sb.Add(ReadChar());
} }
return sb.ToString(); return new String(sb.ToArray());
} }
public JsType ReadVariant(out object value) public JsType ReadVariant(out object value)

Loading…
Cancel
Save