EncodeRuneToString

R=rsc
DELTA=22  (22 added, 0 deleted, 0 changed)
OCL=26779
CL=26792
This commit is contained in:
Robert Griesemer 2009-03-26 16:05:30 -07:00
parent cc8e4fb485
commit b923b01665
2 changed files with 22 additions and 0 deletions

View File

@ -256,6 +256,17 @@ func EncodeRune(rune int, p []byte) int {
return 4; 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 // RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte. // encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int { func RuneCount(p []byte) int {

View 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) { func TestDecodeRune(t *testing.T) {
for i := 0; i < len(utf8map); i++ { for i := 0; i < len(utf8map); i++ {
m := utf8map[i]; m := utf8map[i];