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. & 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 & TODO: add trim, trim/left, trim/right; pad/left, pad/right
@ -618,6 +641,8 @@ fn slice {
base :into ([], 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 &&& keywords: funny names
@ -1326,6 +1351,7 @@ pkg Prelude {
div/0 div/0
div/safe div/safe
doc! doc!
downcase
each! each!
empty? empty?
eq? eq?
@ -1413,6 +1439,7 @@ pkg Prelude {
slice slice
some some
some? some?
split
square square
store! store!
string string
@ -1421,6 +1448,7 @@ pkg Prelude {
sum_of_squares sum_of_squares
tan tan
tau tau
trim
tuple? tuple?
turn/deg turn/deg
turn/rad turn/rad
@ -1431,6 +1459,7 @@ pkg Prelude {
unbox unbox
unwrap! unwrap!
unwrap_or unwrap_or
upcase
update update
update! update!
values values

View File

@ -281,6 +281,13 @@
"range" range "range" range
"unbox" unbox "unbox" unbox
"store!" store! "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 @{}] (def base (let [b @{}]

View File

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