src/Markdig/Helpers/CompactPrefixTree.cs
/// <summary>
/// Tries to find the shortest prefix of text, that is contained in this <see cref="CompactPrefixTree{TValue}"/>
/// </summary>
/// <param name="text">The text in which to search for the prefix</param>
/// <param name="match">The found prefix and the corresponding value</param>
/// <returns>True if a match was found, false otherwise</returns>
Tries to find the shortest prefix of text, that is contained in this
Tries to find the shortest prefix of text that is contained in this
public bool TryMatchShortest(ReadOnlySpan<char> text, out KeyValuePair<string, TValue> match)
        {
            match = default;
            if (text.Length == 0 || !TryGetRoot(text[0], out int nodeIndex))
                return false;
            ref Node node = ref _tree[nodeIndex];
            if (node.MatchIndex != -1)
            {
                match = _matches[node.MatchIndex];
                return true;
            }
            for (int i = 1; i < text.Length; i++)
            {
                char c = text[i];
                if (node.ChildChar == c)
                {
                    node = ref _tree[node.ChildIndex];
                    goto NextChar;
                }
                var children = _children;
                int childrenIndex = node.Children;
                do
                {
                    if ((uint)childrenIndex >= (uint)children.Length)
                        return false;
                    node = ref _tree[children[childrenIndex]];
                    if (node.Char == c) goto NextChar;
                    childrenIndex = children[childrenIndex + 1];
                }
                while (true);
            NextChar:;
                if (node.MatchIndex != -1)
                {
                    match = _matches[node.MatchIndex];
                    return true;
                }
            }
            Debug.Assert(node.MatchIndex == -1);
            return false;
        }
public bool TryMatchShortest ( ReadOnlySpan < char > text , out KeyValuePair < string , TValue > match ) { match = default ; if ( text . Length == 0 || ! TryGetRoot ( text [ 0 ] , out int nodeIndex ) ) return false ; ref Node node = ref _tree [ nodeIndex ] ; if ( node . MatchIndex != - 1 ) { match = _matches [ node . MatchIndex ] ; return true ; } for ( int i = 1 ; i < text . Length ; i ++ ) { char c = text [ i ] ; if ( node . ChildChar == c ) { node = ref _tree [ node . ChildIndex ] ; goto NextChar ; } var children = _children ; int childrenIndex = node . Children ; do { if ( ( uint ) childrenIndex >= ( uint ) children . Length ) return false ; node = ref _tree [ children [ childrenIndex ] ] ; if ( node . Char == c ) goto NextChar ; childrenIndex = children [ childrenIndex + 1 ] ; } while ( true ) ; NextChar : ; if ( node . MatchIndex != - 1 ) { match = _matches [ node . MatchIndex ] ; return true ; } } Debug . Assert ( node . MatchIndex == - 1 ) ; return false ; }
Tries to find the shortest prefix of text, that is contained in this
Tries to find the shortest prefix of text that is contained in this
True if a match was found, false otherwise
True if a match was found false otherwise
text
ReadOnlySpan<char>
The text in which to search for the prefix
The text in which to search for the prefix
KeyValuePair<string, TValue>
out
match
The found prefix and the corresponding value
The found prefix and the corresponding value