Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strings.ncompare (and backing strncmp_mem) to compare up to n characters of a string. #164

Merged
merged 2 commits into from
Mar 7, 2025

Conversation

gillham
Copy link
Contributor

@gillham gillham commented Mar 7, 2025

Compares up to 'length' characters of the two strings. This allows strings of different lengths to be compared.

For example:
strings.ncompare('abc', 'abcdefg', 3) would only look at the first 3 letters and match (returning 0).

Currently with strings.compare strings of unequal length cannot match.

@irmen
Copy link
Owner

irmen commented Mar 7, 2025

What is your use case if I may ask?

@gillham
Copy link
Contributor Author

gillham commented Mar 7, 2025

My simplest use case is when accepting commands with a "shell" or even say a text adventure game.
Below is a simple example, but basically to accept a less than the full command (just the unique length), it is currently necessary to do something like parse1 below. If de is entered for delete it is necessary to copy the first two characters of each command to a temp string, then do the comparison.
(or write your own comparison of course)

The strncmp style feature just lets me match the first N characters directly against the valid command string.

The same thing for a text adventure, 'g n' could be valid for 'go north' or similar.

Maybe there is a more obvious way to do this that I am missing, I am just used to strncmp from C.


%zeropage basicsafe
%import strings
%import textio

main {
    str[5] commands = ["catalog", "delete", "directory", "show", "type"]
    ubyte[5] unique = [1, 2, 2, 1, 1]
    str cmd = " " * 64
    str temp = " " * 64
    sub start() {
        void txt.input_chars(cmd)
        txt.nl()

        parse1()
        txt.nl()
        parse2()
        txt.nl()
    }

    sub parse1() {
        ubyte i
        for i in 0 to 4 {
            strings.left(commands[i], unique[i], temp)
            if strings.compare(cmd, temp) == 0 {
                txt.print("Valid command: ")
                txt.print(commands[i])
                }
        }
    }

    sub parse2() {
        ubyte i
        for i in 0 to 4 {
            if strings.ncompare(cmd, commands[i], unique[i]) == 0 {
                txt.print("Valid command: ")
                txt.print(commands[i])
                }
        }
    }

}

Copy link
Owner

@irmen irmen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks solid. Just 1 missed spot; the new routine should get a mention in the libraries.rst documentation chapter as well. There's a section in there detailing the "string manipulation routines".

@gillham
Copy link
Contributor Author

gillham commented Mar 7, 2025

Ok, let me look at that.

@gillham gillham requested a review from irmen March 7, 2025 22:33
@irmen
Copy link
Owner

irmen commented Mar 7, 2025

thank you

@irmen irmen merged commit 81c255c into irmen:master Mar 7, 2025
@gillham gillham deleted the strncmp branch March 7, 2025 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants