Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Aug 20, 2024
1 parent 5661bfe commit 7bafa38
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/Functions.davi
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?davi

$explode = split("Hello World", " ");

echo("Explode: ",$explode);

?>
34 changes: 34 additions & 0 deletions interpreter/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ var builtins = map[string]builtinFunction{
"slice": {sliceFunction, "slice"},
"sort": {sortFunction, "sort"},
"split": {splitFunction, "split"},
"explode": {explodeFunction, "explode"},
"str": {strFunction, "str"},
"type": {typeFunction, "type"},
"upper": {upperFunction, "upper"},
Expand Down Expand Up @@ -512,6 +513,38 @@ func sortFunction(interp *interpreter, pos Position, args []Value) Value {
return Value(nil)
}

/**
* function: explode
* args: [separator], string
* return: list
* example: explode(", ", "a, b, c") => ["a", "b", "c"]
* output: ["a", "b", "c"]
* description: Explode a string into a list of substrings. It's the same as split() with the arguments reversed.
* title: Explode
* category: String
*/
func explodeFunction(interp *interpreter, pos Position, args []Value) Value {

if len(args) != 1 && len(args) != 2 {
panic(typeError(pos, "explode() requires 1 or 2 args, got %d", len(args)))
}
str, ok := args[1].(string)
if !ok {
panic(typeError(pos, "explode() requires first argument to be a str"))
}

var parts []string
if len(args) == 1 || args[0] == nil {
parts = strings.Fields(str)
} else if sep, ok := args[0].(string); ok {
parts = strings.Split(str, sep)
} else {
panic(typeError(pos, "explode() requires separator to be a str or nil"))
}
return stringsToList(parts)

}

/**
* function: split
* args: string, [separator]
Expand All @@ -530,6 +563,7 @@ func splitFunction(interp *interpreter, pos Position, args []Value) Value {
if !ok {
panic(typeError(pos, "split() requires first argument to be a str"))
}

var parts []string
if len(args) == 1 || args[1] == nil {
parts = strings.Fields(str)
Expand Down

0 comments on commit 7bafa38

Please sign in to comment.