]> Cypherpunks repositories - gostls13.git/commitdiff
EncodeRuneToString
authorRobert Griesemer <gri@golang.org>
Thu, 26 Mar 2009 23:05:30 +0000 (16:05 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 26 Mar 2009 23:05:30 +0000 (16:05 -0700)
R=rsc
DELTA=22  (22 added, 0 deleted, 0 changed)
OCL=26779
CL=26792

src/lib/utf8.go
src/lib/utf8_test.go

index ff55df80210b6076af0d662def66abd944a6ae24..e7a5594b90d93fbcee77a97018bac1ab696f1431 100644 (file)
@@ -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 {
index e35aff938c66baba0a4cbd44d0e6bc77a456a9d4..966b2c97515122f405cd0a006835843f04848e4d 100644 (file)
@@ -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];