forked from candid82/joker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_native.go
180 lines (164 loc) · 3.5 KB
/
string_native.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
package string
import (
"bytes"
"regexp"
"strings"
"unicode"
"unicode/utf8"
. "github.com/candid82/joker/core"
)
var newLine *regexp.Regexp
func padRight(s, pad string, n int) string {
toAdd := n - utf8.RuneCountInString(s)
if toAdd <= 0 {
return s
}
c := utf8.RuneCountInString(pad)
d := toAdd / c
r := toAdd % c
for i := 0; i < d; i++ {
s += pad
}
if r > 0 {
s += string([]rune(pad)[:r])
}
return s
}
func padLeft(s, pad string, n int) string {
toAdd := n - utf8.RuneCountInString(s)
if toAdd <= 0 {
return s
}
c := utf8.RuneCountInString(pad)
d := toAdd / c
r := toAdd % c
for i := 0; i < d; i++ {
s = pad + s
}
if r > 0 {
s = string([]rune(pad)[c-r:]) + s
}
return s
}
func split(s string, r *regexp.Regexp, n int) Object {
indexes := r.FindAllStringIndex(s, n-1)
lastStart := 0
result := EmptyVector
for _, el := range indexes {
result = result.Conjoin(String{S: s[lastStart:el[0]]})
lastStart = el[1]
}
result = result.Conjoin(String{S: s[lastStart:]})
return result
}
func join(sep string, seqable Seqable) string {
seq := seqable.Seq()
var b bytes.Buffer
for !seq.IsEmpty() {
b.WriteString(seq.First().ToString(false))
seq = seq.Rest()
if !seq.IsEmpty() {
b.WriteString(sep)
}
}
return b.String()
}
func isBlank(s Object) bool {
if s.Equals(NIL) {
return true
}
str := AssertString(s, "").S
for _, r := range str {
if !unicode.IsSpace(r) {
return false
}
}
return true
}
func capitalize(s string) string {
if len(s) < 2 {
return strings.ToUpper(s)
}
return strings.ToUpper(string([]rune(s)[:1])) + strings.ToLower(string([]rune(s)[1:]))
}
func escape(s string, cmap Callable) string {
var b bytes.Buffer
for _, r := range s {
if obj := cmap.Call([]Object{Char{Ch: r}}); !obj.Equals(NIL) {
b.WriteString(obj.ToString(false))
} else {
b.WriteRune(r)
}
}
return b.String()
}
func indexOf(s string, value Object, from int) Object {
var res int
if from != 0 {
s = string([]rune(s)[from:])
}
switch value := value.(type) {
case Char:
res = strings.IndexRune(s, value.Ch)
case String:
res = strings.Index(s, value.S)
default:
panic(RT.NewArgTypeError(1, value, "String or Char"))
}
if res == -1 {
return NIL
}
return MakeInt(utf8.RuneCountInString(s[:res]) + from)
}
func lastIndexOf(s string, value Object, from int) Object {
var res int
if from != 0 {
s = string([]rune(s)[:from])
}
switch value := value.(type) {
case Char:
res = strings.LastIndex(s, string(value.Ch))
case String:
res = strings.LastIndex(s, value.S)
default:
panic(RT.NewArgTypeError(1, value, "String or Char"))
}
if res == -1 {
return NIL
}
return MakeInt(utf8.RuneCountInString(s[:res]))
}
func replace(s string, match Object, repl string) string {
switch match := match.(type) {
case String:
return strings.Replace(s, match.S, repl, -1)
case Regex:
return match.R.ReplaceAllString(s, repl)
default:
panic(RT.NewArgTypeError(1, match, "String or Regex"))
}
}
func replaceFirst(s string, match Object, repl string) string {
switch match := match.(type) {
case String:
return strings.Replace(s, match.S, repl, 1)
case Regex:
m := match.R.FindStringIndex(s)
if m == nil {
return s
}
return s[:m[0]] + repl + s[m[1]:]
default:
panic(RT.NewArgTypeError(1, match, "String or Regex"))
}
}
func reverse(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}
func init() {
newLine, _ = regexp.Compile("\r?\n")
}