File tree Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Expand file tree Collapse file tree 2 files changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -107,6 +107,26 @@ func ExampleEncodeRune() {
107
107
// 3
108
108
}
109
109
110
+ func ExampleEncodeRune_outOfRange () {
111
+ runes := []rune {
112
+ // Less than 0, out of range.
113
+ - 1 ,
114
+ // Greater than 0x10FFFF, out of range.
115
+ 0x110000 ,
116
+ // The Unicode replacement character.
117
+ utf8 .RuneError ,
118
+ }
119
+ for i , c := range runes {
120
+ buf := make ([]byte , 3 )
121
+ size := utf8 .EncodeRune (buf , c )
122
+ fmt .Printf ("%d: %d %[2]s %d\n " , i , buf , size )
123
+ }
124
+ // Output:
125
+ // 0: [239 191 189] � 3
126
+ // 1: [239 191 189] � 3
127
+ // 2: [239 191 189] � 3
128
+ }
129
+
110
130
func ExampleFullRune () {
111
131
buf := []byte {228 , 184 , 150 } // 世
112
132
fmt .Println (utf8 .FullRune (buf ))
Original file line number Diff line number Diff line change @@ -337,6 +337,7 @@ func RuneLen(r rune) int {
337
337
}
338
338
339
339
// EncodeRune writes into p (which must be large enough) the UTF-8 encoding of the rune.
340
+ // If the rune is out of range, it writes the encoding of RuneError.
340
341
// It returns the number of bytes written.
341
342
func EncodeRune (p []byte , r rune ) int {
342
343
// Negative values are erroneous. Making it unsigned addresses the problem.
You can’t perform that action at this time.
0 commit comments