add a string replace method

This commit is contained in:
Aidan 2020-08-26 22:32:01 -07:00
parent 591402b428
commit 12caeedf68
No known key found for this signature in database
GPG key ID: 327711E983899316
4 changed files with 36 additions and 3 deletions

View file

@ -84,6 +84,25 @@ func Split(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
return res
}
/* Takes three args
* 1. source string
* 2. token to be replaced
* 3. token to swap in place
* All three args are strings
* Returns final string
*/
func Replace(in *ast.Token, vt ast.VarTable, ft ast.FuncTable) *ast.Token {
body := in.Value()
sub := in.Next.Value()
tok := in.Next.Next.Value()
body = strings.ReplaceAll(body, sub, tok)
res := &ast.Token{Tag: ast.STRING}
res.Set(body)
return res
}
/* Takes two args, a delimiter and a list of strings
* Returns the list of strings concatenated together with the delimiter in between each element
* On error returns nil