Skip to content

Commit 7687314

Browse files
committed
Merge pull request #10 from qiniu/develop
Release v7.0.4
2 parents fc29fd8 + 4c96df4 commit 7687314

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

bytes.v7/replace.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package bytes
2+
3+
import (
4+
"bytes"
5+
)
6+
7+
// ---------------------------------------------------
8+
9+
func ReplaceAt(b []byte, off int, src, dest []byte) []byte {
10+
11+
nsrc, ndest := len(src), len(dest)
12+
if nsrc >= ndest {
13+
left := b[off+nsrc:]
14+
off += copy(b[off:], dest)
15+
off += copy(b[off:], left)
16+
return b[:off]
17+
}
18+
tailoff := len(b) - (ndest - nsrc)
19+
b = append(b, b[tailoff:]...)
20+
copy(b[off+ndest:], b[off+nsrc:len(b)])
21+
copy(b[off:], dest)
22+
return b
23+
}
24+
25+
func ReplaceOne(b []byte, from int, src, dest []byte) ([]byte, int) {
26+
27+
pos := bytes.Index(b[from:], src)
28+
if pos < 0 {
29+
return b, -1
30+
}
31+
32+
from += pos
33+
return ReplaceAt(b, from, src, dest), from + len(dest)
34+
}
35+
36+
func Replace(b []byte, src, dest []byte, n int) []byte {
37+
38+
from := 0
39+
for n != 0 {
40+
b, from = ReplaceOne(b, from, src, dest)
41+
if from < 0 {
42+
break
43+
}
44+
n--
45+
}
46+
return b
47+
}
48+
49+
// ---------------------------------------------------
50+

bytes.v7/replace_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package bytes
2+
3+
import (
4+
"strings"
5+
"testing"
6+
)
7+
8+
type replaceCase struct {
9+
s string
10+
src string
11+
dest string
12+
n int
13+
}
14+
15+
func stringReplace(b string, src, dest string, n int) string {
16+
17+
return string(Replace([]byte(b), []byte(src), []byte(dest), n))
18+
}
19+
20+
func TestReplace(t *testing.T) {
21+
22+
cases := []replaceCase{
23+
{"hello, world!", "world", "xsw", -1},
24+
{"hello, world world world", "world", "xsw", 1},
25+
{"hello, world world world", "world", "xsw", -1},
26+
{"hello, xsw!", "xsw", "world", -1},
27+
{"hello, xsw xsw xsw", "xsw", "world", 1},
28+
{"hello, xsw xsw xsw", "xsw", "world", -1},
29+
}
30+
31+
for _, c := range cases {
32+
ret := stringReplace(c.s, c.src, c.dest, c.n)
33+
expected := strings.Replace(c.s, c.src, c.dest, c.n)
34+
if ret != expected {
35+
t.Fatal("Replace failed:", c, "ret:", ret, "expected:", expected)
36+
}
37+
}
38+
}
39+

errors.v7/error_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func Info(err error, cmd ...interface{}) *ErrorInfo {
8383
return &ErrorInfo{cmd: cmd, err: Err(err), pc: pc}
8484
}
8585

86-
func InfoEx(skip int, err error, cmd ...interface{}) *ErrorInfo {
87-
pc, _, _, ok := runtime.Caller(skip)
86+
func InfoEx(calldepth int, err error, cmd ...interface{}) *ErrorInfo {
87+
pc, _, _, ok := runtime.Caller(calldepth+1)
8888
if !ok {
8989
pc = 0
9090
}

errors.v7/error_info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func MysqlError(err error, cmd ...interface{}) error {
1010

11-
return InfoEx(2, syscall.EINVAL, cmd...).Detail(err)
11+
return InfoEx(1, syscall.EINVAL, cmd...).Detail(err)
1212
}
1313

1414
func (r *ErrorInfo) makeError() error {

0 commit comments

Comments
 (0)