Commit a0f95693 authored by Jon Skeet's avatar Jon Skeet

Use our "local" copy of Encoding.UTF8 in CodedInputStream too.

parent a09b4910
...@@ -339,12 +339,12 @@ namespace Google.Protobuf ...@@ -339,12 +339,12 @@ namespace Google.Protobuf
{ {
// Fast path: We already have the bytes in a contiguous buffer, so // Fast path: We already have the bytes in a contiguous buffer, so
// just copy directly from it. // just copy directly from it.
String result = Encoding.UTF8.GetString(buffer, bufferPos, size); String result = CodedOutputStream.Utf8Encoding.GetString(buffer, bufferPos, size);
bufferPos += size; bufferPos += size;
return result; return result;
} }
// Slow path: Build a byte array first then copy it. // Slow path: Build a byte array first then copy it.
return Encoding.UTF8.GetString(ReadRawBytes(size), 0, size); return CodedOutputStream.Utf8Encoding.GetString(ReadRawBytes(size), 0, size);
} }
/// <summary> /// <summary>
......
...@@ -135,7 +135,7 @@ namespace Google.Protobuf ...@@ -135,7 +135,7 @@ namespace Google.Protobuf
/// </summary> /// </summary>
public static int ComputeStringSize(String value) public static int ComputeStringSize(String value)
{ {
int byteArraySize = UTF8.GetByteCount(value); int byteArraySize = Utf8Encoding.GetByteCount(value);
return ComputeRawVarint32Size((uint) byteArraySize) + return ComputeRawVarint32Size((uint) byteArraySize) +
byteArraySize; byteArraySize;
} }
......
...@@ -59,7 +59,8 @@ namespace Google.Protobuf ...@@ -59,7 +59,8 @@ namespace Google.Protobuf
/// </remarks> /// </remarks>
public sealed partial class CodedOutputStream public sealed partial class CodedOutputStream
{ {
private static readonly Encoding UTF8 = Encoding.UTF8; // "Local" copy of Encoding.UTF8, for efficiency. (Yes, it makes a difference.)
internal static readonly Encoding Utf8Encoding = Encoding.UTF8;
/// <summary> /// <summary>
/// The buffer size used by CreateInstance(Stream). /// The buffer size used by CreateInstance(Stream).
...@@ -240,7 +241,7 @@ namespace Google.Protobuf ...@@ -240,7 +241,7 @@ namespace Google.Protobuf
{ {
// Optimise the case where we have enough space to write // Optimise the case where we have enough space to write
// the string directly to the buffer, which should be common. // the string directly to the buffer, which should be common.
int length = UTF8.GetByteCount(value); int length = Utf8Encoding.GetByteCount(value);
WriteRawVarint32((uint)length); WriteRawVarint32((uint)length);
if (limit - position >= length) if (limit - position >= length)
{ {
...@@ -253,13 +254,13 @@ namespace Google.Protobuf ...@@ -253,13 +254,13 @@ namespace Google.Protobuf
} }
else else
{ {
UTF8.GetBytes(value, 0, value.Length, buffer, position); Utf8Encoding.GetBytes(value, 0, value.Length, buffer, position);
} }
position += length; position += length;
} }
else else
{ {
byte[] bytes = UTF8.GetBytes(value); byte[] bytes = Utf8Encoding.GetBytes(value);
WriteRawBytes(bytes); WriteRawBytes(bytes);
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment