summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-03-26 16:05:30 -0700
committerRobert Griesemer <gri@golang.org>2009-03-26 16:05:30 -0700
commit0927eb15dfd27af2dda7427839558cb37dfb19d5 (patch)
tree17c1392638a950e01717e1f410ae8d048afa5511
parentecfb1855e02ed9bc71b904a7a51afdaa3631c073 (diff)
downloadgolang-0927eb15dfd27af2dda7427839558cb37dfb19d5.tar.gz
EncodeRuneToString
R=rsc DELTA=22 (22 added, 0 deleted, 0 changed) OCL=26779 CL=26792
-rw-r--r--src/lib/utf8.go11
-rw-r--r--src/lib/utf8_test.go11
2 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/utf8.go b/src/lib/utf8.go
index ff55df802..e7a5594b9 100644
--- a/src/lib/utf8.go
+++ b/src/lib/utf8.go
@@ -256,6 +256,17 @@ func EncodeRune(rune int, p []byte) int {
return 4;
}
+// EncodeRuneToString returns the string corresponding to the UTF-8 encoding of the rune.
+func EncodeRuneToString(rune int) string {
+ if rune < _Rune1Max {
+ return string([1]byte{byte(rune)})
+ }
+
+ var buf[UTFMax] byte;
+ size := EncodeRune(rune, buf);
+ return string(buf[0:size]);
+}
+
// RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int {
diff --git a/src/lib/utf8_test.go b/src/lib/utf8_test.go
index e35aff938..966b2c975 100644
--- a/src/lib/utf8_test.go
+++ b/src/lib/utf8_test.go
@@ -98,6 +98,17 @@ func TestEncodeRune(t *testing.T) {
}
}
+func TestEncodeRuneToString(t *testing.T) {
+ for i := 0; i < len(utf8map); i++ {
+ m := utf8map[i];
+ s := m.str;
+ s1 := utf8.EncodeRuneToString(m.rune);
+ if s != s1 {
+ t.Errorf("EncodeRuneToString(0x%04x) = %s want %s", m.rune, s1, s);
+ }
+ }
+}
+
func TestDecodeRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ {
m := utf8map[i];