Răsfoiți Sursa

actually use ParsingPrimitives.ReadBytes and ReadString

Jan Tattermusch 5 ani în urmă
părinte
comite
cf49962c57

+ 3 - 16
csharp/src/Google.Protobuf/CodedInputStream.cs

@@ -421,9 +421,8 @@ namespace Google.Protobuf
         /// </summary>
         public string ReadString()
         {
-            int length = ReadLength();
             var span = new ReadOnlySpan<byte>(buffer);
-            return ParsingPrimitives.ReadRawString(ref span, ref state, length);
+            return ParsingPrimitives.ReadString(ref span, ref state);
         }
 
         /// <summary>
@@ -470,20 +469,8 @@ namespace Google.Protobuf
         /// </summary>   
         public ByteString ReadBytes()
         {
-            int length = ReadLength();
-            if (length <= state.bufferSize - state.bufferPos && length > 0)
-            {
-                // Fast path:  We already have the bytes in a contiguous buffer, so
-                //   just copy directly from it.
-                ByteString result = ByteString.CopyFrom(buffer, state.bufferPos, length);
-                state.bufferPos += length;
-                return result;
-            }
-            else
-            {
-                // Slow path:  Build a byte array and attach it to a new ByteString.
-                return ByteString.AttachBytes(ReadRawBytes(length));
-            }
+            var span = new ReadOnlySpan<byte>(buffer);
+            return ParsingPrimitives.ReadBytes(ref span, ref state);
         }
 
         /// <summary>

+ 2 - 4
csharp/src/Google.Protobuf/ParseContext.cs

@@ -220,8 +220,7 @@ namespace Google.Protobuf
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public string ReadString()
         {
-            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
-            return ParsingPrimitives.ReadRawString(ref buffer, ref state, length);
+            return ParsingPrimitives.ReadString(ref buffer, ref state);
         }
 
         /// <summary>
@@ -248,8 +247,7 @@ namespace Google.Protobuf
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public ByteString ReadBytes()
         {
-            int length = ParsingPrimitives.ParseLength(ref buffer, ref state);
-            return ByteString.AttachBytes(ParsingPrimitives.ReadRawBytes(ref buffer, ref state, length));
+            return ParsingPrimitives.ReadBytes(ref buffer, ref state);
         }
         /// <summary>
         /// Reads a uint32 field value from the input.

+ 4 - 0
csharp/src/Google.Protobuf/ParsingPrimitives.cs

@@ -577,6 +577,10 @@ namespace Google.Protobuf
             }
         }
 
+        /// <summary>
+        /// Reads a string field value from the input.
+        /// </summary>
+        [MethodImpl(MethodImplOptions.AggressiveInlining)]
         public static string ReadString(ref ReadOnlySpan<byte> buffer, ref ParserInternalState state)
         {
             int length = ParsingPrimitives.ParseLength(ref buffer, ref state);