add string manipulation functions

This commit is contained in:
Scott Richmond 2024-06-10 18:26:48 -04:00
parent 2e7db1b969
commit 5913f9b594
4 changed files with 48 additions and 5 deletions

View File

@ -339,6 +339,29 @@ fn join {
)
}
fn split {
"Takes a string, and turns it into a list of strings, breaking on the separator."
(str as :string, break as :string) -> base :split (break, str)
}
fn trim {
"Trims whitespace from a string. Takes an optional argument, `:left` or `:right`, to trim only on the left or right."
(str as :string) -> base :trim (str)
(str as :string, :left) -> base :triml (str)
(str as :string, :right) -> base :trimr (str)
}
fn upcase {
"Takes a string and returns it in all uppercase. Works only for ascii characters."
(str as :string) -> base :upcase (str)
}
fn downcase {
"Takes a string and returns it in all lowercase. Works only for ascii characters."
(str as :string) -> base :downcase (str)
}
& in another prelude, with a better actual base language than Java (thanks, Rich), counting strings would be reasonable but complex: count/bytes, count/points, count/glyphs. Java's UTF16 strings make this unweildy.
& TODO: add trim, trim/left, trim/right; pad/left, pad/right
@ -618,6 +641,8 @@ fn slice {
base :into ([], slice)
}
}
(str as :string, end as :number) -> base :str_slice (str, 0, end)
(str as :string, start as :number, end as :number) -> base :str_slice (str, start, end)
}
&&& keywords: funny names
@ -1326,6 +1351,7 @@ pkg Prelude {
div/0
div/safe
doc!
downcase
each!
empty?
eq?
@ -1413,6 +1439,7 @@ pkg Prelude {
slice
some
some?
split
square
store!
string
@ -1421,6 +1448,7 @@ pkg Prelude {
sum_of_squares
tan
tau
trim
tuple?
turn/deg
turn/rad
@ -1431,6 +1459,7 @@ pkg Prelude {
unbox
unwrap!
unwrap_or
upcase
update
update!
values

View File

@ -281,6 +281,13 @@
"range" range
"unbox" unbox
"store!" store!
"split" string/split
"upcase" string/ascii-upper
"downcase" string/ascii-lower
"trim" string/trim
"trimr" string/trimr
"triml" string/triml
"str_slice" string/slice
})
(def base (let [b @{}]

View File

@ -36,7 +36,7 @@
# (set (out :errors) (validated :errors))
(each err (validated :errors)
(e/validation-error err))
(break out))
(break (-> out j/encode string)))
(setdyn :out console)
(try
(set result (i/interpret (parsed :ast) ctx))
@ -55,6 +55,13 @@
# (comment
(do
(-> (ludus `
doc! (mod)
`)))
(def source `let foo = foo bar baz`)
(def out (-> source
ludus
j/decode))
(def console (out "console"))
(print console)
(def result (out "result"))
(print result)
)

View File

@ -202,7 +202,7 @@
"{" (recur (advance scanner) (buffer/push buff char) true)
# allow multiline strings
"\n" (recur (update (advance scanner) :line inc) (buffer/push buff char) interpolate?)
"\"" (add-token (advance scanner) (if interpolate? :interpolated :string)(string buff))
"\"" (add-token (advance scanner) (if interpolate? :interpolated :string) (string buff))
"\\" (let [next (next-char scanner)]
(if (= next "{")
(do