/// <summary> /// Try Get Encoded System String Type Marker /// </summary> /// <param name="utf8Span">The value.</param> /// <param name="multiByteTypeMarker">The multi byte type marker if found.</param> /// <returns>Whether or not the Encoded System String Type Marker was found.</returns> private static bool TryGetEncodedSystemStringTypeMarker( Utf8Span utf8Span, out MultiByteTypeMarker multiByteTypeMarker) { if (JsonBinaryEncoding.TryGetSystemStringId(utf8Span, out int systemStringId)) { multiByteTypeMarker = new MultiByteTypeMarker( length: 1, one: (byte)(TypeMarker.SystemString1ByteLengthMin + systemStringId)); return(true); } multiByteTypeMarker = default; return(false); }
/// <summary> /// Try Get Encoded User String Type Marker /// </summary> /// <param name="utf8Span">The value.</param> /// <param name="jsonStringDictionary">The optional json string dictionary.</param> /// <param name="multiByteTypeMarker">The multi byte type marker if found.</param> /// <returns>Whether or not the Encoded User String Type Marker was found.</returns> private static bool TryGetEncodedUserStringTypeMarker( Utf8Span utf8Span, JsonStringDictionary jsonStringDictionary, out MultiByteTypeMarker multiByteTypeMarker) { if (jsonStringDictionary == null) { multiByteTypeMarker = default; return(false); } const int MinStringLength = 2; const int MaxStringLength = 128; if ((utf8Span.Length < MinStringLength) || (utf8Span.Length > MaxStringLength)) { multiByteTypeMarker = default; return(false); } const byte OneByteCount = TypeMarker.UserString1ByteLengthMax - TypeMarker.UserString1ByteLengthMin; if (!jsonStringDictionary.TryAddString(utf8Span, out int index)) { multiByteTypeMarker = default; return(false); } // Convert the index to a multibyte type marker if (index < OneByteCount) { multiByteTypeMarker = new MultiByteTypeMarker( length: 1, one: (byte)(TypeMarker.UserString1ByteLengthMin + index)); } else { int twoByteOffset = index - OneByteCount; multiByteTypeMarker = new MultiByteTypeMarker( length: 2, one: (byte)((twoByteOffset / 0xFF) + TypeMarker.UserString2ByteLengthMin), two: (byte)(twoByteOffset % 0xFF)); } return(true); }
/// <summary> /// Try Get Encoded String Type Marker /// </summary> /// <param name="utf8Span">the value</param> /// <param name="jsonStringDictionary">The JSON string dictionary.</param> /// <param name="multiByteTypeMarker">The encoded string type marker if found.</param> /// <returns>Whether or not the type marker was found.</returns> public static bool TryGetEncodedStringTypeMarker( Utf8Span utf8Span, JsonStringDictionary jsonStringDictionary, out MultiByteTypeMarker multiByteTypeMarker) { if (JsonBinaryEncoding.TryGetEncodedSystemStringTypeMarker(utf8Span, out multiByteTypeMarker)) { return(true); } if (JsonBinaryEncoding.TryGetEncodedUserStringTypeMarker(utf8Span, jsonStringDictionary, out multiByteTypeMarker)) { return(true); } multiByteTypeMarker = default; return(false); }