Exemple #1
0
        public static JsonStringDictionary CreateFromStringArray(IReadOnlyList <string> userStrings)
        {
            if (userStrings == null)
            {
                throw new ArgumentNullException(nameof(userStrings));
            }

            JsonStringDictionary jsonStringDictionary = new JsonStringDictionary(userStrings.Count);

            for (int i = 0; i < userStrings.Count; i++)
            {
                string userString = userStrings[i];
                if (!jsonStringDictionary.TryAddString(Encoding.UTF8.GetBytes(userString).AsSpan(), out int index))
                {
                    throw new ArgumentException($"Failed to add {userString} to {nameof(JsonStringDictionary)}.");
                }

                if (index != i)
                {
                    throw new ArgumentException($"Tried to add {userString} at index {i}, but instead it was inserted at index {index}.");
                }
            }

            return(jsonStringDictionary);
        }
        /// <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);
        }