Skip to content

Instantly share code, notes, and snippets.

@naduma
Created March 14, 2016 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naduma/90a8fa3e21fffa1bbf80 to your computer and use it in GitHub Desktop.
Save naduma/90a8fa3e21fffa1bbf80 to your computer and use it in GitHub Desktop.
golang.org/x/text/width: patch sample
diff --git width/transform.go width/transform.go
index 8dd731c..ec8be34 100644
--- width/transform.go
+++ width/transform.go
@@ -22,7 +22,7 @@ func (foldTransform) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, err
if d := len(dst) - nDst; d < end-start {
end = nSrc + d
}
- for nSrc++; nSrc < end && src[nSrc] < utf8.RuneSelf; nSrc++ {
+ for ; nSrc < end && src[nSrc] < utf8.RuneSelf; nSrc++ {
}
nDst += copy(dst[nDst:], src[start:nSrc])
if nDst == len(dst) && nSrc < len(src) && src[nSrc] < utf8.RuneSelf {
@@ -72,7 +72,7 @@ func (narrowTransform) Transform(dst, src []byte, atEOF bool) (nDst, nSrc int, e
if d := len(dst) - nDst; d < end-start {
end = nSrc + d
}
- for nSrc++; nSrc < end && src[nSrc] < utf8.RuneSelf; nSrc++ {
+ for ; nSrc < end && src[nSrc] < utf8.RuneSelf; nSrc++ {
}
nDst += copy(dst[nDst:], src[start:nSrc])
if nDst == len(dst) && nSrc < len(src) && src[nSrc] < utf8.RuneSelf {
diff --git width/transform_test.go width/transform_test.go
index 535f24d..e7c43ea 100644
--- width/transform_test.go
+++ width/transform_test.go
@@ -6,6 +6,7 @@ package width
import (
"bytes"
+ "strings"
"testing"
"golang.org/x/text/internal/testtext"
@@ -132,6 +133,14 @@ func TestFold(t *testing.T) {
atEOF: true,
err: transform.ErrShortDst,
}, {
+ desc: "empty dst fast path",
+ src: "fast",
+ dst: "",
+ nDst: 0,
+ nSrc: 0,
+ atEOF: true,
+ err: transform.ErrShortDst,
+ }, {
desc: "fast path alternation",
src: "fast路徑fast路徑",
dst: "fast路徑fast路徑",
@@ -377,6 +386,14 @@ func TestNarrow(t *testing.T) {
atEOF: true,
err: transform.ErrShortDst,
}, {
+ desc: "empty dst fast path",
+ src: "fast",
+ dst: "",
+ nDst: 0,
+ nSrc: 0,
+ atEOF: true,
+ err: transform.ErrShortDst,
+ }, {
desc: "fast path alternation",
src: "fast路徑fast路徑",
dst: "fast路徑fast路徑",
@@ -465,3 +482,39 @@ func BenchmarkNarrowNonCanonical(b *testing.B) {
func BenchmarkNarrowOther(b *testing.B) {
bench(b, Narrow, testtext.TwoByteUTF8+testtext.ThreeByteUTF8)
}
+
+const initialBufSize = 128 // initialBufSize from golang.org/x/text/transform/transform.go
+
+func TestFoldLongString(t *testing.T) {
+ for i, s := range []string{
+ "\uff21" + strings.Repeat("A", initialBufSize-1),
+ "\uff21" + strings.Repeat("A", initialBufSize+0),
+ "\uff21" + strings.Repeat("A", initialBufSize+1),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+1),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+0),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+1),
+ } {
+ got := Fold.String(s)
+ want := strings.Repeat("A", len(s)-len(w)+1)
+ if got != want {
+ t.Errorf("%d:Fold String test: got %s (%d); want %s (%d)", i, got, len(got), want, len(want))
+ }
+ }
+}
+
+func TestNarrowLongString(t *testing.T) {
+ for i, s := range []string{
+ "\uff21" + strings.Repeat("A", initialBufSize-1),
+ "\uff21" + strings.Repeat("A", initialBufSize+0),
+ "\uff21" + strings.Repeat("A", initialBufSize+1),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+1),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+0),
+ "\uff21" + strings.Repeat("A", 2*initialBufSize+1),
+ } {
+ got := Narrow.String(s)
+ want := strings.Repeat("A", len(s)-len("\uff21")+1)
+ if got != want {
+ t.Errorf("%d:Fold String test: got %s (%d); want %s (%d)", i, got, len(got), want, len(want))
+ }
+ }
+}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment