maid-lang 1.1.0

Maid Programming Language
Documentation
# file string.maid: string operations in maid

# get char at index in string
# returns the char at the specified index
func charat(str, index) {
    if type(str) != "string" {
        uhoh("expected type string in 'charat'");
    }

    if type(index) != "number" {
        uhoh("argument 'index' must be type number in 'charat'");
    }

    if index < 0 {
        uhoh("cannot index a strings characters at a value less than 0 in 'charat'");
    }

    give str ^ index;
}

# check if a string starts with the specified chars
# returns true if starts with chars otherwise false
func startswith(str, chars) {
    if type(str) != "string" {
        uhoh("expected type string in 'startswith'");
    }

    if type(chars) != "string" {
        uhoh("argument 'chars' must be type string in 'startswith'");
    }

    if length(chars) > length(str) {
        give false;
    }

    walk i = 0 through length(chars) {
        if charat(str, i) != charat(chars, i) {
            give false;
        }
    }

    give true;
}

# check if a string ends with the specified chars
# returns true if ends with chars otherwise false
func endswith(str, chars) {
    if type(str) != "string" {
        uhoh("expected type string in 'endswith'");
    }

    if type(chars) != "string" {
        uhoh("argument 'chars' must be type string in 'endswith'");
    }

    if length(chars) > length(str) {
        give false;
    }

    # we have to reverse each string because we can't iterate backwards through a string
    obj str = reverse_str(str);
    obj chars = reverse_str(chars);

    walk i = 0 through length(chars) {
        if charat(str, i) != charat(chars, i) {
            give false;
        }
    }

    give true;
}

# join two strings together
# returns the two strings combined as a new string
func join(string_a, string_b) {
    if type(string_a) != "string" {
        uhoh("argument 'string_a' must be type string in 'join'");
    }

    if type(string_b) != "string" {
        uhoh("argument 'string_b' must be type string in 'join'");
    }

    give string_a + string_b;
}