Click here to Skip to main content
15,904,348 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 3:49
mvahoney the codewitch13-May-24 3:49 
GeneralRe: There are many gotos, but these ones are mine Pin
klinkenbecker13-May-24 4:33
klinkenbecker13-May-24 4:33 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 4:34
mvahoney the codewitch13-May-24 4:34 
GeneralRe: There are many gotos, but these ones are mine Pin
klinkenbecker13-May-24 4:49
klinkenbecker13-May-24 4:49 
GeneralRe: There are many gotos, but these ones are mine Pin
TNCaver13-May-24 3:10
TNCaver13-May-24 3:10 
GeneralRe: There are many gotos, but these ones are mine Pin
dandy7213-May-24 7:27
dandy7213-May-24 7:27 
GeneralRe: There are many gotos, but these ones are mine Pin
glennPattonWork313-May-24 8:25
professionalglennPattonWork313-May-24 8:25 
AnswerRe: There are many gotos, but these ones are mine Pin
Payton Byrd 202313-May-24 9:16
Payton Byrd 202313-May-24 9:16 
Code runs in LinqPad.
Code runs in LinqPad.

This should be significantly faster than your original code because it speeds up the conditionals by using pattern matching instead of overloadable operators. Also, the local functions can be in-lined, meaning they will be executed in place, which is even more efficient than the `Goto` statements. And now it's not pure spaghetti.

string json = """
{
  "test": 0,
  "data": "value"
}
""";

JsonStringRunner runner = new();

List<FAMatch> matches = new();
FAMatch current = default;
Stopwatch sw = new();
sw.Start();
do{
    current = runner.GetMatch(json);
    matches.Add(current);
} while(!runner.isDone);
sw.Stop();
matches.Dump();
sw.Dump();

internal record struct FAMatch(int token, string match, int position, int length, int column)
{
    internal static FAMatch Create(int token, string match, int position, int length, int column)
        => new(token, match, position, length, column);
}

internal abstract class FAStringRunner
{
    protected int position = -1, line = 0, column = 0;
    internal bool isDone = false;
}

internal sealed partial class JsonStringRunner : FAStringRunner
{
    private void Advance(string s, ref int ch, ref int len, bool flag)
    {
        // Assuming Advance takes consecutive characters in the string.
        ch = s[position];
        position++;
        len++;
        isDone = !(position < s.Length);
    }
    private FAMatch NextMatchImpl(string s)
    {
        int ch;
        int len;
        int l;
        int c;
        ch = -1;
        len = 0;
        if ((this.position is -1))
        {
            this.position = 0;
        }
        int p = this.position;
        l = this.line;
        c = this.column;
        this.Advance(s, ref ch, ref len, true);
        // q0:
        switch (ch)
        {
            // [\t-\n\r ]
            case 9 or 10 or 13 or 32:
                if(ch is 10 or 13){
                    l = line++;
                }
                return q1();
            // [\"]
            case 34:
                return q2();
            // [,]
            case 44:
                return q9();
            // [\-]
            case 45:
                return q10();
            // [0]
            case 48:
                return q11();
            // [1-9]
            case >= 49 and <= 57:
                return q17();
            // [\:]
            case 58:
                return q18();
            // [\[]
            case 91:
                return q19();
            // [\]]
            case 93:
                return q20();
            // [f]
            case 102:
                return q21();
            // [n]
            case 110:
                return q26();
            // [t]
            case 116:
                return q30();
            // [\{]
            case 123:
                return q32();
            // [\}]
            case 125:
                return q33();
        }
        return errorout();

        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q1()
        {
            // [\t-\n\r ]
            while (ch is 9 or 10 or 13 or 32)
            {
                this.Advance(s, ref ch, ref len, false);
            }
            return FAMatch.Create(10, s.Substring(p, len), p, l, c);
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q2()
        {
            // [\0-\t\v-!#-\[\]-\x10ffff]
            while (ch is (((((>= 0)
                        and (<= 9))
                        or ((>= 11)
                        and (<= 33)))
                        or ((>= 35)
                        and (<= 91)))
                        or ((>= 93)
                        and (<= 1114111))))
            {
                this.Advance(s, ref ch, ref len, false);
            }
            // [\"]
            if ((ch is 34))
            {
                this.Advance(s, ref ch, ref len, false);
                return q3();
            }
            // [\\]
            if ((ch is 92))
            {
                this.Advance(s, ref ch, ref len, false);
                return q4();
            }

            return default;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q3() => FAMatch.Create(9, s.Substring(p, len), p, l, c);

        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q4()
        {
            // [\"\/\\bfnrt]
            if (ch is 34 or 47
                        or 92
                        or 98
                        or 102
                        or 110
                        or 114
                        or 116)
            {
                this.Advance(s, ref ch, ref len, false);
                return q2();
            }

            // [u]
            if (ch is 117)
            {
                this.Advance(s, ref ch, ref len, false);
                return q5();
            }

            return errorout();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q5()
        {
            // [0-9A-Fa-f]
            if (ch is (>= 48 and <= 57) or (>= 65 and <= 70)
                        or (>= 97 and <= 102))
            {
                this.Advance(s, ref ch, ref len, false);
                return q6();
            }
            return errorout();
        }
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q6()
        {
            // [0-9A-Fa-f]
            if (ch is (>= 48 and <= 57) or (>= 65 and <= 70)
                        or (>= 97 and <= 102))
            {
                this.Advance(s, ref ch, ref len, false);
                return q7();
            }
            return errorout();
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q7() => FAMatch.Create(7, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q9() => FAMatch.Create(9, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q10() => FAMatch.Create(10, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q11() => FAMatch.Create(11, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q17() => FAMatch.Create(17, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q18() => FAMatch.Create(18, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q19() => FAMatch.Create(19, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q20() => FAMatch.Create(20, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q21() => FAMatch.Create(21, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q26() => FAMatch.Create(26, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q30() => FAMatch.Create(110, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q32() => FAMatch.Create(123, s.Substring(p, len), p, l, c);
        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch q33() => FAMatch.Create(125, s.Substring(p, len), p, l, c);

        [MethodImpl(MethodImplOptions.AggressiveInlining)] FAMatch errorout() => FAMatch.Create(0, s.Substring(p, len), p, l, c); // Dunno what you do in this section of code.
    }

    public FAMatch GetMatch(string s) => NextMatchImpl(s);
}

GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 9:36
mvahoney the codewitch13-May-24 9:36 
GeneralRe: There are many gotos, but these ones are mine Pin
Payton Byrd 202313-May-24 9:51
Payton Byrd 202313-May-24 9:51 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch13-May-24 10:08
mvahoney the codewitch13-May-24 10:08 
GeneralRe: There are many gotos, but these ones are mine Pin
jschell14-May-24 13:33
jschell14-May-24 13:33 
GeneralRe: There are many gotos, but these ones are mine Pin
honey the codewitch14-May-24 13:36
mvahoney the codewitch14-May-24 13:36 
NewsSite slowness Pin
Chris Maunder11-May-24 6:58
cofounderChris Maunder11-May-24 6:58 
GeneralRe: Site slowness Pin
RickZeeland11-May-24 8:20
mveRickZeeland11-May-24 8:20 
GeneralRe: Site slowness Pin
Chris Maunder11-May-24 8:32
cofounderChris Maunder11-May-24 8:32 
GeneralRe: Site slowness Pin
RickZeeland11-May-24 9:06
mveRickZeeland11-May-24 9:06 
GeneralRe: Site slowness Pin
BernardIE531711-May-24 9:52
BernardIE531711-May-24 9:52 
GeneralRe: Site slowness Pin
pkfox11-May-24 10:23
professionalpkfox11-May-24 10:23 
GeneralRe: Site slowness Pin
Chris Maunder13-May-24 2:40
cofounderChris Maunder13-May-24 2:40 
GeneralRe: Site slowness Pin
pkfox13-May-24 7:54
professionalpkfox13-May-24 7:54 
GeneralRe: Site slowness Pin
honey the codewitch11-May-24 12:01
mvahoney the codewitch11-May-24 12:01 
GeneralRe: Site slowness Pin
Chris Maunder13-May-24 2:41
cofounderChris Maunder13-May-24 2:41 
GeneralRe: Site slowness Pin
Brian C Hart11-May-24 13:30
professionalBrian C Hart11-May-24 13:30 
GeneralBashing MS...ALLOWED! :) Pin
Brian C Hart11-May-24 6:54
professionalBrian C Hart11-May-24 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.