Adds a string-replace function for straight replacements where regex is too much

This commit is contained in:
sloum 2022-07-10 17:11:58 -07:00
parent 834e2b19f3
commit ce7b630eff
2 changed files with 15 additions and 0 deletions

14
lib.go
View File

@ -1931,6 +1931,20 @@ var stdLibrary = vars{
}
return number(i)
},
"string-replace": func(a ...expression) expression {
if len(a) < 3 {
return exception("'string-replace' expected three values: a string, a thing to replace in that string, and a thing to replace it with. Insufficient values were given")
}
count := -1
if len(a) > 3 {
c, ok := a[3].(number)
if !ok {
return exception("'string-replace' expected its fourth argument to be a number representing a count of replacements to be made, a non-number value was given")
}
count = int(c)
}
return strings.Replace(String(a[0], false), String(a[1], false), String(a[2], false), count)
},
"string-fields": func(a ...expression) expression {
if len(a) == 0 {
return exception("insufficient number of arguments given to 'string-fields'")

View File

@ -178,6 +178,7 @@ var usageStrings = map[string]string{
"string-lower": "(string-lower [string]) => string\n\nConverts all characters in the given string to their lowercase form and returns the new lowercase string",
"string-make-buf": "(string-make-buf) => io-handle: string-buf\n\nReturns a new empty string buffer io-handle",
"string-ref": "(string-ref [string] [index: number]) => string\n\nReturns the character at the given index as a string. Like with lists indexes begin at 0",
"string-replace": "(string-replace [string] [find: string] [replacement: string] [[count: number]]) => string\n\nDoes a straight string replacement `count` number of times, defaulting to all matches found. For more complex/flexible matching use `regex-replace`",
"string-trim-space": "(string-trim-space [string]) => string\n\nRemoves any leading or trailing white-space from `string`",
"string-upper": "(string-upper [string]) => string\n\nConverts all characters in the given string to their uppercase form and returns the new uppercase string",
"subprocess": "(subprocess [list] [[output-redirection: io-handle|#f]] [[error-redirection: io-handle|#f]] [[input-redirection: io-handle|#f]]) => number\n\nPassing `#f` to any of the redirections will have them use the std version of the redirection (stdout, stderr, or stdin). If you want to have something redirect to devnull you must explicitly pass it",