diff --git a/assert.gs b/assert.gs index 867d7a4..b378dd7 100644 --- a/assert.gs +++ b/assert.gs @@ -21,3 +21,16 @@ proc assert result, expected, message = "" { } } %endif + +# assert true +proc assert_t condition, message { + if not $condition { + error $message; + } +} +# assert false +proc assert_f condition, message { + if $condition { + error $message; + } +} diff --git a/string.gs b/string.gs index b6386d7..8510a9f 100644 --- a/string.gs +++ b/string.gs @@ -1,4 +1,16 @@ +%define WHITESPACE " \t\n\r" +%define ASCII_LOWERCASE "abcdefghijklmnopqrstuvwxyz" +%define ASCII_UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +%define ASCII_LETTERS ASCII_LOWERCASE & ASCII_UPPERCASE +%define DIGITS "0123456789" +%define HEXDIGITS DIGITS & "abcdef" & "ABCDEF" +%define OCTDIGITS "01234567" +%define PUNCTUATION "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" +%define PRINTABLE_CHARS DIGITS & ASCII_LETTERS & PUNCTUATION & WHITESPACE + func strcmp(string1, string2) { + # This code assumes you have costumes A-Z and a-z + # If you need to compare chars e.g ä vs Ä, then you need to add those as costumes too if length($string1) != length($string2) { return false; } @@ -14,3 +26,119 @@ func strcmp(string1, string2) { } return true; } + +func slice(string, start, end) { + local ret = ""; + local i = $start; + + until i >= $end { + ret &= $string[i]; + i++; + } + return ret; +} + +func slice_step(string, start, end, step) { + if $start == $end or $step + "" == 0 { + return ""; + } else { + local ret = ""; + local i = $start; + + if $step < 0 { + until i <= $end { + ret &= $string[i]; + i += $step; + } + return ret; + } else { + until i >= $end { + ret &= $string[i]; + i += $step; + } + return ret; + } + } +} + +func startswith(string, start) { + local i = 1; + repeat length $start { + if $string[i] != $start[i] { + return false; + } + i++; + } + return true; +} + +# startswith_from(1, ...) is equivalent to startswith(...) +func startswith_from(i, string, start) { + local i = 1; + repeat length $start { + if $string[i + $i - 1] != $start[i] { + return false; + } + i++; + } + return true; +} + +func endswith(string, end) { + local i = 0; + repeat length $end { + if $string[length $string - i] != $end[length $end - i] { + return false; + } + i++; + } + return true; +} + +# endswith_from(length , , ...) is equivalent to endswith(, ...) +func endswith_from(i, string, end) { + local i = 0; + repeat length $end { + if $string[$i - i] != $end[length $end - i] { + return false; + } + i++; + } + return true; +} + +# Pad '0' to the left until it reaches the specified length. +func zfill(string, len) { + local ret = $string; + repeat $len - length $string { + ret = 0 & ret; + } + return ret; +} + +# Pad ' ' to the left until it reaches the specified length. +func ljust(string, len, char = " ") { + local ret = $string; + repeat $len - length $string { + ret = $char & ret; + } + return ret; +} + +# Pad ' ' to the right until it reaches the specified length. +func rjust(string, len, char = " ") { + local ret = $string; + repeat $len - length $string { + ret &= $char; + } + return ret; +} + +# repeat $string $times times (equivalent to int-string multiplication in python) +func repstr(string, times) { + local ret = ""; + repeat $times { + ret &= $string; + } + return ret; +} diff --git a/test/lib/test_string.gs b/test/lib/test_string.gs index 6542980..c316001 100644 --- a/test/lib/test_string.gs +++ b/test/lib/test_string.gs @@ -1,8 +1,27 @@ proc test_string { - if not strcmp("Hello, World!", "Hello, World!") == true { - error "strcmp(\"Hello, World!\", \"Hello, World!\")"; - } - if not strcmp("hello, world!", "Hello, World!") == false { - error "strcmp(\"hello, world!\", \"Hello, World!\")"; - } + assert_t strcmp("Hello, World!", "Hello, World!"), "strcmp(\"Hello, World!\", \"Hello, World!\")"; + assert_f strcmp("hello, world!", "Hello, World!"), "strcmp(\"hello, world!\", \"Hello, World!\")"; + + assert_t startswith("Hello, World!", "Hello"), "startswith(\"Hello, World!\", \"Hello\")"; + assert_t startswith_from(8, "Hello, World!", "World!"), "startswith_from(8, \"Hello, World!\", \"World!\")"; + assert_t endswith("Hello, World!", "World!"), "endswith(\"Hello, World!\", \"World!\")"; + assert_t endswith_from(6, "Hello, World!", "Hello,"), "endswith_from(6, \"Hello, World!\", \"Hello,\")"; + assert zfill("FFF", 6), "000FFF", "zfill(\"FFF\", 6)"; + assert ljust("FFF", 6), " FFF", "ljust(\"FFF\", 6)"; + assert ljust("FFF", 6, 1), "111FFF", "ljust(\"FFF\", 6, 1)"; + assert repstr("a", 3), "aaa", "repstr(\"a\", 3)"; + assert rjust("FFF", 6), "FFF ", "rjust(\"FFF\", 6)"; + assert rjust("FFF", 6, 0), "FFF000", "rjust(\"FFF\", 6, 1)"; + assert slice("Hello, world", 2, 6), "ello", "slice(\"Hello, world\", 2, 6)"; + assert slice_step("Hello, world", 5, 0, -2), "olH", "slice_step(\"Hello, world\", 5, 0, -2)"; + + assert WHITESPACE, " \t\n\r", "WHITESPACE"; + assert ASCII_LETTERS, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", "ASCII_LETTERS"; + assert ASCII_LOWERCASE, "abcdefghijklmnopqrstuvwxyz", "ASCII_LOWERCASE"; + assert ASCII_UPPERCASE, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "ASCII_UPPERCASE"; + assert DIGITS, "0123456789", "DIGITS"; + assert HEXDIGITS, "0123456789abcdefABCDEF", "HEXDIGITS"; + assert OCTDIGITS, "01234567", "OCTDIGITS"; + assert PUNCTUATION, "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~", "PUNCTUATION"; + assert PRINTABLE_CHARS, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r", "PRINTABLE_CHARS"; }