Skip to content

Commit

Permalink
str.replace supports a short form that doesn't require the n argument,
Browse files Browse the repository at this point in the history
…closes #262

```
"aaaa".replace("a", "x") # "xxxx"
```
  • Loading branch information
odino committed Aug 17, 2019
1 parent 152454a commit f4fc95c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
8 changes: 5 additions & 3 deletions docs/types/string.md
Expand Up @@ -337,19 +337,21 @@ Creates a new string, repeating the original one `i` times:
"string".repeat(2) # "stringstring"
```

### replace(x, y, n)
### replace(x, y [, n])

Replaces occurrences of `x` with `y`, `n` times.
If `n` is negative it will replace all occurrencies:
If `n` is omitted, or negative, it will replace all occurrencies:

``` bash
"string".replace("i", "o", -1) # "strong"
"aaaa".replace("a", "x") # "xxxx"
"aaaa".replace("a", "x", 2) # "xxaa"
```

You can also replace an array of characters:

``` bash
"string".replace(["i", "g"], "o", -1) # "strono"
"string".replace(["i", "g"], "o") # "strono"
```

### title()
Expand Down
2 changes: 2 additions & 0 deletions evaluator/evaluator_test.go
Expand Up @@ -895,7 +895,9 @@ c")`, []string{"a", "b", "c"}},
{`[1,2,3].slice(-1, 3)`, []int{3}},
{`[1,2,3].slice(-1, 1)`, []int{3}},
{`"a".replace("a", "b", -1)`, "b"},
{`"a".replace("a", "b")`, "b"},
{`"ac".replace(["a", "c"], "b", -1)`, "bb"},
{`"ac".replace(["a", "c"], "b")`, "bb"},
{`"a".str()`, "a"},
{`1.str()`, "1"},
{`[1].str()`, "[1]"},
Expand Down
18 changes: 16 additions & 2 deletions evaluator/functions.go
Expand Up @@ -1165,17 +1165,31 @@ func repeatFn(tok token.Token, args ...object.Object) object.Object {
return &object.String{Token: tok, Value: strings.Repeat(args[0].(*object.String).Value, int(args[1].(*object.Number).Value))}
}

// replace("abd", "d", "c") --> short form
// replace("abd", "d", "c", -1)
// replace("abc", ["a", "b"], "c", -1)
func replaceFn(tok token.Token, args ...object.Object) object.Object {
err := validateArgs(tok, "replace", args, 4, [][]string{{object.STRING_OBJ}, {object.STRING_OBJ, object.ARRAY_OBJ}, {object.STRING_OBJ}, {object.NUMBER_OBJ}})
var err object.Object

// Support short form
if len(args) == 3 {
err = validateArgs(tok, "replace", args, 3, [][]string{{object.STRING_OBJ}, {object.STRING_OBJ, object.ARRAY_OBJ}, {object.STRING_OBJ}})
} else {
err = validateArgs(tok, "replace", args, 4, [][]string{{object.STRING_OBJ}, {object.STRING_OBJ, object.ARRAY_OBJ}, {object.STRING_OBJ}, {object.NUMBER_OBJ}})
}

if err != nil {
return err
}

original := args[0].(*object.String).Value
replacement := args[2].(*object.String).Value
n := int(args[3].(*object.Number).Value)

n := -1

if len(args) == 4 {
n = int(args[3].(*object.Number).Value)
}

if characters, ok := args[1].(*object.Array); ok {
for _, c := range characters.Elements {
Expand Down

0 comments on commit f4fc95c

Please sign in to comment.