Skip to content

Commit

Permalink
Add strings.remove{pre,suf}fix methods (google#395)
Browse files Browse the repository at this point in the history
* Add strings.remove{pre,suf}fix methods

* Avoid constant 6
  • Loading branch information
adonovan authored Feb 23, 2022
1 parent c55a923 commit 243c749
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions doc/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ reproducibility is paramount, such as build tools.
* [string·lower](#string·lower)
* [string·lstrip](#string·lstrip)
* [string·partition](#string·partition)
* [string·removeprefix](#string·removeprefix)
* [string·removesuffix](#string·removesuffix)
* [string·replace](#string·replace)
* [string·rfind](#string·rfind)
* [string·rindex](#string·rindex)
Expand Down Expand Up @@ -680,6 +682,8 @@ Strings have several built-in methods:
* [`lstrip`](#string·lstrip)
* [`partition`](#string·partition)
* [`replace`](#string·replace)
* [`removeprefix`](#string·removeprefix)
* [`removesuffix`](#string·removesuffix)
* [`rfind`](#string·rfind)
* [`rindex`](#string·rindex)
* [`rpartition`](#string·rpartition)
Expand Down Expand Up @@ -4026,6 +4030,30 @@ If S does not contain `x`, `partition` returns `(S, "", "")`.
"one/two/three".partition("/") # ("one", "/", "two/three")
```
<a id='string·removeprefix'></a>
### string·removeprefix
`S.removeprefix(prefix)` returns a copy of string S with the prefix `prefix`
removed if S starts with `prefix`, otherwise it returns S.
```python
"banana".removeprefix("ban") # "ana"
"banana".removeprefix("foo") # "banana"
"foofoobar".removeprefix("foo") # "foobar"
```
<a id='string·removesuffix'></a>
### string·removesuffix
`S.removesuffix(suffix)` returns a copy of string S with the suffix `suffix`
removed if S ends with `suffix`, otherwise it returns S.
```python
"banana".removesuffix("nana") # "ba"
"banana".removesuffix("foo") # "banana"
"banana".removesuffix("na") # "bana"
```
<a id='string·replace'></a>
### string·replace
Expand Down
18 changes: 18 additions & 0 deletions starlark/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ var (
"lower": NewBuiltin("lower", string_lower),
"lstrip": NewBuiltin("lstrip", string_strip), // sic
"partition": NewBuiltin("partition", string_partition),
"removeprefix": NewBuiltin("removeprefix", string_removefix),
"removesuffix": NewBuiltin("removesuffix", string_removefix),
"replace": NewBuiltin("replace", string_replace),
"rfind": NewBuiltin("rfind", string_rfind),
"rindex": NewBuiltin("rindex", string_rindex),
Expand Down Expand Up @@ -1889,6 +1891,22 @@ func string_partition(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value,
return tuple, nil
}

// https://github.com/google/starlark-go/blob/master/doc/spec.md#string·removeprefix
// https://github.com/google/starlark-go/blob/master/doc/spec.md#string·removesuffix
func string_removefix(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
recv := string(b.Receiver().(String))
var fix string
if err := UnpackPositionalArgs(b.Name(), args, kwargs, 1, &fix); err != nil {
return nil, err
}
if b.name[len("remove")] == 'p' {
recv = strings.TrimPrefix(recv, fix)
} else {
recv = strings.TrimSuffix(recv, fix)
}
return String(recv), nil
}

// https://github.com/google/starlark-go/blob/master/doc/spec.md#string·replace
func string_replace(_ *Thread, b *Builtin, args Tuple, kwargs []Tuple) (Value, error) {
recv := string(b.Receiver().(String))
Expand Down
21 changes: 21 additions & 0 deletions starlark/testdata/string.star
Original file line number Diff line number Diff line change
Expand Up @@ -470,3 +470,24 @@ assert.true(not "DŽenan LJubović".istitle())
assert.fails(lambda: "".starts_with, "no .starts_with field.*did you mean .startswith")
assert.fails(lambda: "".StartsWith, "no .StartsWith field.*did you mean .startswith")
assert.fails(lambda: "".fin, "no .fin field.*.did you mean .find")


# removesuffix
assert.eq("Apricot".removesuffix("cot"), "Apri")
assert.eq("Apricot".removesuffix("Cot"), "Apricot")
assert.eq("Apricot".removesuffix("t"), "Aprico")
assert.eq("a".removesuffix(""), "a")
assert.eq("".removesuffix(""), "")
assert.eq("".removesuffix("a"), "")
assert.eq("Apricot".removesuffix("co"), "Apricot")
assert.eq("Apricotcot".removesuffix("cot"), "Apricot")

# removeprefix
assert.eq("Apricot".removeprefix("Apr"), "icot")
assert.eq("Apricot".removeprefix("apr"), "Apricot")
assert.eq("Apricot".removeprefix("A"), "pricot")
assert.eq("a".removeprefix(""), "a")
assert.eq("".removeprefix(""), "")
assert.eq("".removeprefix("a"), "")
assert.eq("Apricot".removeprefix("pr"), "Apricot")
assert.eq("AprApricot".removeprefix("Apr"), "Apricot")

0 comments on commit 243c749

Please sign in to comment.