Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
f25e016c
Commit
f25e016c
authored
Mar 07, 2010
by
Andrew Gerrand
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
regexp: add ReplaceAllFunc and ReplaceAllStringFunc
R=r CC=golang-dev
https://golang.org/cl/247041
parent
cba81d80
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
2 deletions
+64
-2
src/pkg/regexp/all_test.go
src/pkg/regexp/all_test.go
+46
-0
src/pkg/regexp/regexp.go
src/pkg/regexp/regexp.go
+18
-2
No files found.
src/pkg/regexp/all_test.go
View file @
f25e016c
...
...
@@ -5,9 +5,12 @@
package
regexp
import
(
"bytes"
"io"
"os"
"strings"
"testing"
"utf8"
)
var
good_re
=
[]
string
{
...
...
@@ -302,6 +305,18 @@ var replaceTests = []ReplaceTest{
ReplaceTest
{
"[a-c]*"
,
"x"
,
"abcbcdcdedef"
,
"xdxdxexdxexfx"
},
}
type
ReplaceFuncTest
struct
{
pattern
string
replacement
func
(
string
)
string
input
,
output
string
}
var
replaceFuncTests
=
[]
ReplaceFuncTest
{
ReplaceFuncTest
{
"[a-c]"
,
func
(
s
string
)
string
{
return
"x"
+
s
+
"y"
},
"defabcdef"
,
"defxayxbyxcydef"
},
ReplaceFuncTest
{
"[a-c]+"
,
func
(
s
string
)
string
{
return
"x"
+
s
+
"y"
},
"defabcdef"
,
"defxabcydef"
},
ReplaceFuncTest
{
"[a-c]*"
,
func
(
s
string
)
string
{
return
"x"
+
s
+
"y"
},
"defabcdef"
,
"xydxyexyfxabcydxyexyfxy"
},
}
func
TestReplaceAll
(
t
*
testing
.
T
)
{
for
_
,
tc
:=
range
replaceTests
{
re
,
err
:=
Compile
(
tc
.
pattern
)
...
...
@@ -323,6 +338,27 @@ func TestReplaceAll(t *testing.T) {
}
}
func
TestReplaceAllFunc
(
t
*
testing
.
T
)
{
for
_
,
tc
:=
range
replaceFuncTests
{
re
,
err
:=
Compile
(
tc
.
pattern
)
if
err
!=
nil
{
t
.
Errorf
(
"Unexpected error compiling %q: %v"
,
tc
.
pattern
,
err
)
continue
}
actual
:=
re
.
ReplaceAllStringFunc
(
tc
.
input
,
tc
.
replacement
)
if
actual
!=
tc
.
output
{
t
.
Errorf
(
"%q.ReplaceFunc(%q,%q) = %q; want %q"
,
tc
.
pattern
,
tc
.
input
,
tc
.
replacement
,
actual
,
tc
.
output
)
}
// now try bytes
actual
=
string
(
re
.
ReplaceAllFunc
([]
byte
(
tc
.
input
),
func
(
s
[]
byte
)
[]
byte
{
return
[]
byte
(
tc
.
replacement
(
string
(
s
)))
}))
if
actual
!=
tc
.
output
{
t
.
Errorf
(
"%q.ReplaceFunc(%q,%q) = %q; want %q"
,
tc
.
pattern
,
tc
.
input
,
tc
.
replacement
,
actual
,
tc
.
output
)
}
}
}
type
QuoteMetaTest
struct
{
pattern
,
output
string
}
...
...
@@ -510,3 +546,13 @@ func BenchmarkNotLiteral(b *testing.B) {
}
}
}
func
BenchmarkReplaceAll
(
b
*
testing
.
B
)
{
x
:=
"abcdefghijklmnopqrstuvwxyz"
b
.
StopTimer
()
re
,
_
:=
Compile
(
"[cjrw]"
)
b
.
StartTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
re
.
ReplaceAllString
(
x
,
""
)
}
}
src/pkg/regexp/regexp.go
View file @
f25e016c
...
...
@@ -1006,6 +1006,14 @@ func Match(pattern string, b []byte) (matched bool, error os.Error) {
// have been replaced by repl. No support is provided for expressions
// (e.g. \1 or $1) in the replacement string.
func
(
re
*
Regexp
)
ReplaceAllString
(
src
,
repl
string
)
string
{
return
re
.
ReplaceAllStringFunc
(
src
,
func
(
string
)
string
{
return
repl
})
}
// ReplaceAllStringFunc returns a copy of src in which all matches for the
// Regexp have been replaced by the return value of of function repl (whose
// first argument is the matched string). No support is provided for
// expressions (e.g. \1 or $1) in the replacement string.
func
(
re
*
Regexp
)
ReplaceAllStringFunc
(
src
string
,
repl
func
(
string
)
string
)
string
{
lastMatchEnd
:=
0
// end position of the most recent match
searchPos
:=
0
// position where we next look for a match
buf
:=
new
(
bytes
.
Buffer
)
...
...
@@ -1023,7 +1031,7 @@ func (re *Regexp) ReplaceAllString(src, repl string) string {
// (Otherwise, we get double replacement for patterns that
// match both empty and nonempty strings.)
if
a
[
1
]
>
lastMatchEnd
||
a
[
0
]
==
0
{
io
.
WriteString
(
buf
,
repl
)
io
.
WriteString
(
buf
,
repl
(
src
[
a
[
0
]
:
a
[
1
]])
)
}
lastMatchEnd
=
a
[
1
]
...
...
@@ -1050,6 +1058,14 @@ func (re *Regexp) ReplaceAllString(src, repl string) string {
// have been replaced by repl. No support is provided for expressions
// (e.g. \1 or $1) in the replacement text.
func
(
re
*
Regexp
)
ReplaceAll
(
src
,
repl
[]
byte
)
[]
byte
{
return
re
.
ReplaceAllFunc
(
src
,
func
([]
byte
)
[]
byte
{
return
repl
})
}
// ReplaceAllFunc returns a copy of src in which all matches for the
// Regexp have been replaced by the return value of of function repl (whose
// first argument is the matched []byte). No support is provided for
// expressions (e.g. \1 or $1) in the replacement string.
func
(
re
*
Regexp
)
ReplaceAllFunc
(
src
[]
byte
,
repl
func
([]
byte
)
[]
byte
)
[]
byte
{
lastMatchEnd
:=
0
// end position of the most recent match
searchPos
:=
0
// position where we next look for a match
buf
:=
new
(
bytes
.
Buffer
)
...
...
@@ -1067,7 +1083,7 @@ func (re *Regexp) ReplaceAll(src, repl []byte) []byte {
// (Otherwise, we get double replacement for patterns that
// match both empty and nonempty strings.)
if
a
[
1
]
>
lastMatchEnd
||
a
[
0
]
==
0
{
buf
.
Write
(
repl
)
buf
.
Write
(
repl
(
src
[
a
[
0
]
:
a
[
1
]])
)
}
lastMatchEnd
=
a
[
1
]
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment