Repository: microsoft/garnet
File: libs/server/Custom/ExpandableMap.cs (C#)

Code before:
            Map = newMap;
            Map[idx] = value;
            TryUpdateActualSize(id);
            return true;
        }
    }

    /// <summary>
    /// Extension methods for ExpandableMap
    /// </summary>
    internal static class ExpandableMapExtensions
    {
        /// <summary>
        /// Match command name with existing commands in map and return first matching instance
        /// </summary>
        /// <typeparam name="T">Type of command</typeparam>
        /// <param name="eMap">Current instance of ExpandableMap</param>
        /// <param name="cmd">Command name to match</param>
        /// <param name="value">Value of command found</param>
        /// <returns>True if command found</returns>
        internal static bool MatchCommand<T>(this ExpandableMap<T> eMap, ReadOnlySpan<byte> cmd, out T value)
            where T : ICustomCommand
        {
            // We need our own copy of EqualsUpperCaseSpanIgnoringCase, since the normal version
            // would assert on non-ascii chars like the dot in 'json.get'.
            static bool EqualsUpperCaseSpanIgnoringCase(ReadOnlySpan<byte> cmd, byte[] Name)
            {
                if (Name == null)
                    return false;

                if (cmd.Length != Name.Length)
                    return false;

                if (cmd.SequenceEqual(new ReadOnlySpan<byte>(Name)))
                    return true;

                for (var j = 0; j < cmd.Length; j++)
                {
                    var b1 = cmd[j];
                    var b2 = Name[j];

                    if ((b1 != b2) && ((b2 < 65) || (b2 > 90) || (b1 - 32 != b2)))
                    {
                        return false;
                    }
                }

                return true;
            }

            value = default;

Review comment (refactor):
This should probably be another helper method in `AsciiUtils` with an XML comment explaining that this version of the method supports all ASCII characters (could actually just be a flag in the original method).

Code after:
            Map = newMap;
            Map[idx] = value;
            TryUpdateActualSize(id);
            return true;
        }
    }

    /// <summary>
    /// Extension methods for ExpandableMap
    /// </summary>
    internal static class ExpandableMapExtensions
    {
        /// <summary>
        /// Match command name with existing commands in map and return first matching instance
        /// </summary>
        /// <typeparam name="T">Type of command</typeparam>
        /// <param name="eMap">Current instance of ExpandableMap</param>
        /// <param name="cmd">Command name to match</param>
        /// <param name="value">Value of command found</param>
        /// <returns>True if command found</returns>
        internal static bool MatchCommand<T>(this ExpandableMap<T> eMap, ReadOnlySpan<byte> cmd, out T value)
            where T : ICustomCommand
        {
            value = default;

            // Take the current map instance and its size
            // Note: Map instance could be updated as the map expands, but in this context we only care about its current snapshot
            // Map's actual size could only grow, which is why it's important to take the size snapshot before the map itself.
            var mapSize = eMap.ActualSize;
            var map = eMap.Map;

            // Try to match the specified command with the commands in the current map
            for (var i = 0; i < mapSize; i++)
            {
                var currCmd = map[i];
                if (currCmd != null && cmd.SequenceEqual(new ReadOnlySpan<byte>(currCmd.Name)))
                {
                    value = currCmd;
                    return true;
                }
            }

            return false;
        }

        /// <summary>
        /// Match sub-command name with existing sub-commands in map and return first matching instance
        /// </summary>
        /// <typeparam name="T">Type of command</typeparam>
        /// <param name="eMap">Current instance of ExpandableMap</param>
        /// <param name="cmd">Sub-command name to match</param>