Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAAS-24673: Implement String.prototype.replaceAll #100

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions builtin_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,44 @@ func (r *Runtime) stringproto_replace(call FunctionCall) Value {
return stringReplace(call.ctx, s, found, str, rcall)
}

func (r *Runtime) stringproto_replaceAll(call FunctionCall) Value {
r.checkObjectCoercible(call.This)
searchValue := call.Argument(0)
replaceValue := call.Argument(1)
if searchValue != _undefined && searchValue != _null {
if isRegexp(searchValue) {
if o, ok := searchValue.(*Object); ok {
flags := nilSafe(o.self.getStr("flags", nil))
r.checkObjectCoercible(flags)
if !strings.Contains(flags.toString().String(), "g") {
panic(r.NewTypeError("String.prototype.replaceAll called with a non-global RegExp argument"))
}
}
}
if replacer := toMethod(r.getV(searchValue, SymReplace)); replacer != nil {
return replacer(FunctionCall{
This: searchValue,
Arguments: []Value{call.This, replaceValue},
})
}
}

s := call.This.toString()
var found [][]int
searchStr := searchValue.toString()
searchLength := searchStr.length()
advanceBy := toIntStrict(max(1, int64(searchLength)))

pos := s.index(searchStr, 0)
for pos != -1 {
found = append(found, []int{pos, pos + searchLength})
pos = s.index(searchStr, pos+advanceBy)
}

str, rcall := getReplaceValue(replaceValue)
return stringReplace(call.ctx, s, found, str, rcall)
}

func (r *Runtime) stringproto_search(call FunctionCall) Value {
r.checkObjectCoercible(call.This)
regexp := call.Argument(0)
Expand Down Expand Up @@ -974,6 +1012,7 @@ func (r *Runtime) initString() {
o._putProp("padStart", r.newNativeFunc(r.stringproto_padStart, nil, "padStart", nil, 1), true, false, true)
o._putProp("repeat", r.newNativeFunc(r.stringproto_repeat, nil, "repeat", nil, 1), true, false, true)
o._putProp("replace", r.newNativeFunc(r.stringproto_replace, nil, "replace", nil, 2), true, false, true)
o._putProp("replaceAll", r.newNativeFunc(r.stringproto_replaceAll, nil, "replaceAll", nil, 2), true, false, true)
o._putProp("search", r.newNativeFunc(r.stringproto_search, nil, "search", nil, 1), true, false, true)
o._putProp("slice", r.newNativeFunc(r.stringproto_slice, nil, "slice", nil, 2), true, false, true)
o._putProp("split", r.newNativeFunc(r.stringproto_split, nil, "split", nil, 2), true, false, true)
Expand Down
3 changes: 3 additions & 0 deletions string_ascii.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@
func (s asciiString) index(substr valueString, start int) int {
a, u := devirtualizeString(substr)
if u == nil {
if start > len(s) {
return -1
}
p := strings.Index(string(s[start:]), string(a))
if p >= 0 {
return p + start
Expand Down Expand Up @@ -341,10 +344,10 @@
return 0
}
if s == "Infinity" || s == "+Infinity" {
return math.MaxInt64

Check failure on line 347 in string_ascii.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest, 386)

constant 9223372036854775807 overflows int
}
if s == "-Infinity" {
return math.MinInt64

Check failure on line 350 in string_ascii.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest, 386)

constant -9223372036854775808 overflows int
}
i, err := s._toInt()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions tc39_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ var (

// Left-hand side as a CoverParenthesizedExpression
"test/language/expressions/assignment/fn-name-lhs-cover.js": true,

// Skip due to regexp named groups
"test/built-ins/String/prototype/replaceAll/searchValue-replacer-RegExp-call.js": true,
}

featuresBlackList = []string{
Expand Down
Loading