forked from shawnfeng/sutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbol.go
121 lines (86 loc) · 1.69 KB
/
symbol.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2014 The sutil Author. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stext
import (
"fmt"
"io/ioutil"
"bytes"
"unicode/utf8"
)
type SymbolList struct {
list map[rune] []byte
}
func NewSymbolList(file string) (*SymbolList, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
m := &SymbolList {
list: make(map[rune] []byte),
}
items := bytes.Split(data, []byte("\n"))
for _, it := range(items) {
r, size := utf8.DecodeRune(it)
if size == len(it) {
_, ok := m.list[r]
if ok {
//slog.Warnf(context.TODO(), "same %c %d %s %v", r, size, it, it)
} else {
m.list[r] = it
}
} else {
//slog.Warnf(context.TODO(), "illigal %s %d %s %v", r, size, it, it)
}
}
for i := 0; i < 256; i++ {
if i >= 'A' && i <= 'Z' {
continue
}
if i >= 'a' && i <= 'z' {
continue
}
if i >= '0' && i <= '9' {
continue
}
m.list[rune(i)] = []byte{byte(i)}
}
return m, nil
}
func (m *SymbolList)Is(c rune) bool {
_, ok := m.list[c]
return ok
}
func (m *SymbolList) Print() {
s := ""
for _, v := range(m.list) {
if len(s) == 0 {
s = fmt.Sprintf("%s", v)
} else {
s = fmt.Sprintf("%s,%s", s, v)
}
}
fmt.Println(s)
}
func BytesToRunesNoSymb(s *SymbolList, b []byte) []rune {
rv := make([]rune, 0)
for len(b) > 0 {
r, size := utf8.DecodeRune(b)
b = b[size:]
if s.Is(r) {
continue
}
rv = append(rv, r)
}
return rv
}
func BytesToRunes(b []byte) []rune {
rv := make([]rune, 0)
for len(b) > 0 {
r, size := utf8.DecodeRune(b)
rv = append(rv, r)
b = b[size:]
rv = append(rv, r)
}
return rv
}