Fix offset bug
[yt-mango.git] / common / escape.go
blobc17056b6d371ad218a4c81a1ad8b454f5f9566c6
1 package common
3 import "bytes"
5 // Markdown escape map (ASCII)
6 // Inspired by https://github.com/golang-commonmark/markdown/blob/master/escape.go
7 type EscapeMap [2]uint64
9 func (e *EscapeMap) Get(index uint) bool {
10 if index >= 128 { return false }
11 high, low := index / 64, index % 64
12 return e[high] & (1 << low) != 0
15 func (e *EscapeMap) Set(index uint, x bool) {
16 if index >= 128 { return }
17 high, low := index / 64, index % 64
18 if x {
19 e[high] = e[high] | 1 << low
20 } else {
21 e[high] = e[high] &^ 1 << low
25 func (e EscapeMap) ToBuffer(src string, dest *bytes.Buffer) (err error) {
26 for _, char := range src {
27 if char < 0x80 && e.Get(uint(char)) {
28 // Write backslash + char
29 _, err = dest.Write([]byte{0x5c, byte(char)})
30 } else {
31 _, err = dest.WriteRune(char)
34 return
37 func (e EscapeMap) ToString(src string) (string, error) {
38 var buffer bytes.Buffer
39 err := e.ToBuffer(src, &buffer)
40 if err != nil {
41 return "", err
42 } else {
43 return buffer.String(), nil