| 1 | package mahonia |
| 2 | |
| 3 | import "unicode/utf8" |
| 4 | |
| 5 | func init() { |
| 6 | RegisterCharset(&Charset{ |
| 7 | Name: "UTF-8", |
| 8 | NewDecoder: func() Decoder { return decodeUTF8Rune }, |
| 9 | NewEncoder: func() Encoder { return encodeUTF8Rune }, |
| 10 | }) |
| 11 | } |
| 12 | |
| 13 | func decodeUTF8Rune(p []byte) (c rune, size int, status Status) { |
| 14 | if len(p) == 0 { |
| 15 | status = NO_ROOM |
| 16 | return |
| 17 | } |
| 18 | |
| 19 | if p[0] < 128 { |
| 20 | return rune(p[0]), 1, SUCCESS |
| 21 | } |
| 22 | |
| 23 | c, size = utf8.DecodeRune(p) |
| 24 | |
| 25 | if c == 0xfffd { |
| 26 | if utf8.FullRune(p) { |
| 27 | status = INVALID_CHAR |
| 28 | return |
| 29 | } |
| 30 | |
| 31 | return 0, 0, NO_ROOM |
| 32 | } |
| 33 | |
| 34 | status = SUCCESS |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | func encodeUTF8Rune(p []byte, c rune) (size int, status Status) { |
| 39 | size = utf8.RuneLen(c) |
| 40 | if size > len(p) { |
| 41 | return 0, NO_ROOM |
| 42 | } |
| 43 | |
| 44 | return utf8.EncodeRune(p, c), SUCCESS |
| 45 | } |