summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/paulrosania/go-charset/charset/ascii.go
blob: ccf3a35bda80f6b3ea10d71116f20216c38310f8 (plain) (blame)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package charset

import (
	"bytes"
	"fmt"
	"unicode/utf8"
)

func init() {
	registerClass("ascii", fromASCII, toASCII)
}

const errorByte = '?'

type translateFromASCII bool

type codePointError struct {
	i       int
	cp      rune
	charset string
}

func (e *codePointError) Error() string {
	return fmt.Sprintf("Parse error at index %n: Code point %n is undefined in %s", e.i, e.cp, e.charset)
}

func (strict translateFromASCII) Translate(data []byte, eof bool) (int, []byte, error) {
	buf := bytes.NewBuffer(make([]byte, 0, len(data)))
	for i, c := range data {
		if c > 0 && c < 128 {
			buf.WriteByte(c)
			if c < 32 && c != 10 && c != 13 && c != 9 {
				// badly formed
			}
		} else {
			if strict {
				return 0, nil, &codePointError{i, rune(c), "US-ASCII"}
			}
			buf.WriteRune(utf8.RuneError)
		}
	}
	return len(data), buf.Bytes(), nil
}

type translateToASCII bool

func (strict translateToASCII) Translate(data []byte, eof bool) (int, []byte, error) {
	buf := bytes.NewBuffer(make([]byte, 0, len(data)))
	for _, c := range data {
		if c > 0 && c < 128 {
			buf.WriteByte(c)
		} else {
			buf.WriteByte(errorByte)
		}
	}
	return len(data), buf.Bytes(), nil
}

func fromASCII(arg string) (Translator, error) {
	return new(translateFromASCII), nil
}

func toASCII(arg string) (Translator, error) {
	return new(translateToASCII), nil
}