-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstringutils.go
211 lines (175 loc) · 4.6 KB
/
stringutils.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Package goarabic contains utility functions for working with Arabic strings.
package goarabic
// Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
// SmartLength returns the length of the given string
// without considering the Arabic Vowels (Tashkeel).
func SmartLength(s *string) int {
// len() use int as return value, so we'd better follow for compatibility
length := 0
for _, value := range *s {
if tashkeel[value] {
continue
}
length++
}
return length
}
// RemoveTashkeel returns its argument as rune-wise string without Arabic vowels (Tashkeel).
func RemoveTashkeel(s string) string {
// var r []rune
// the capcity of the slice wont be greater than the length of the string itself
// hence, cap = len(s)
r := make([]rune, 0, len(s))
for _, value := range s {
if tashkeel[value] {
continue
}
r = append(r, value)
}
return string(r)
}
// RemoveTatweel returns its argument as rune-wise string without Arabic Tatweel character.
func RemoveTatweel(s string) string {
r := make([]rune, 0, len(s))
for _, value := range s {
if TATWEEL.equals(value) {
continue
}
r = append(r, value)
}
return string(r)
}
func getCharGlyph(previousChar, currentChar, nextChar rune) rune {
glyph := currentChar
previousIn := false // in the Arabic Alphabet or not
nextIn := false // in the Arabic Alphabet or not
for _, s := range alphabet {
if s.equals(previousChar) { // previousChar in the Arabic Alphabet ?
previousIn = true
}
if s.equals(nextChar) { // nextChar in the Arabic Alphabet ?
nextIn = true
}
}
for _, s := range alphabet {
if !s.equals(currentChar) { // currentChar in the Arabic Alphabet ?
continue
}
if previousIn && nextIn { // between two Arabic Alphabet, return the medium glyph
for s, _ := range beggining_after {
if s.equals(previousChar) {
return getHarf(currentChar).Beggining
}
}
return getHarf(currentChar).Medium
}
if nextIn { // beginning (because the previous is not in the Arabic Alphabet)
return getHarf(currentChar).Beggining
}
if previousIn { // final (because the next is not in the Arabic Alphabet)
for s, _ := range beggining_after {
if s.equals(previousChar) {
return getHarf(currentChar).Isolated
}
}
return getHarf(currentChar).Final
}
if !previousIn && !nextIn {
return getHarf(currentChar).Isolated
}
}
return glyph
}
// equals() return if true if the given Arabic char is alphabetically equal to
// the current Harf regardless its shape (Glyph)
func (c *Harf) equals(char rune) bool {
switch char {
case c.Unicode:
return true
case c.Beggining:
return true
case c.Isolated:
return true
case c.Medium:
return true
case c.Final:
return true
default:
return false
}
}
// getHarf gets the correspondent Harf for the given Arabic char
func getHarf(char rune) Harf {
for _, s := range alphabet {
if s.equals(char) {
return s
}
}
return Harf{Unicode: char, Isolated: char, Medium: char, Final: char}
}
//RemoveAllNonAlphabetChars deletes all characters which are not included in Arabic Alphabet
func RemoveAllNonArabicChars(text string) string {
runes := []rune(text)
newText := []rune{}
for _, current := range runes {
inAlphabet := false
for _, s := range alphabet {
if s.equals(current) {
inAlphabet = true
}
}
if inAlphabet {
newText = append(newText, current)
}
}
return string(newText)
}
// ToGlyph returns the glyph representation of the given text
func ToGlyph(text string) string {
var prev, next rune
runes := []rune(text)
length := len(runes)
newText := make([]rune, 0, length)
for i, current := range runes {
// get the previous char
if (i - 1) < 0 {
prev = 0
} else {
prev = runes[i-1]
}
// get the next char
if (i + 1) <= length-1 {
next = runes[i+1]
} else {
next = 0
}
// get the current char representation or return the same if unnecessary
glyph := getCharGlyph(prev, current, next)
// append the new char representation to the newText
newText = append(newText, glyph)
}
return string(newText)
}
// RemoveTashkeel returns its argument as rune-wise string without Arabic vowels (Tashkeel).
/*
func RemoveTashkeelExtended(s string) string {
r := []rune(s)
m := map[string]bool{"\u064e": true, "\u064b": true, "\u064f": true,
"\u064c": true, "\u0650": true, "\u064d": true,
"\u0651": true, "\u0652": true}
for key, value := range s {
if m[value] {
continue
}
r[key] = value
}
return string(r)
}
*/