File: utf8.go

package info (click to toggle)
golang-github-axgle-mahonia 0.0~git20180208.3358181-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,856 kB
  • sloc: makefile: 2
file content (45 lines) | stat: -rw-r--r-- 736 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package mahonia

import "unicode/utf8"

func init() {
	RegisterCharset(&Charset{
		Name:       "UTF-8",
		NewDecoder: func() Decoder { return decodeUTF8Rune },
		NewEncoder: func() Encoder { return encodeUTF8Rune },
	})
}

func decodeUTF8Rune(p []byte) (c rune, size int, status Status) {
	if len(p) == 0 {
		status = NO_ROOM
		return
	}

	if p[0] < 128 {
		return rune(p[0]), 1, SUCCESS
	}

	c, size = utf8.DecodeRune(p)

	if c == 0xfffd {
		if utf8.FullRune(p) {
			status = INVALID_CHAR
			return
		}

		return 0, 0, NO_ROOM
	}

	status = SUCCESS
	return
}

func encodeUTF8Rune(p []byte, c rune) (size int, status Status) {
	size = utf8.RuneLen(c)
	if size > len(p) {
		return 0, NO_ROOM
	}

	return utf8.EncodeRune(p, c), SUCCESS
}