/
str.go
194 lines (167 loc) · 4.9 KB
/
str.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
// Copyright 2019 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package vm
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/gentee/gentee/core"
)
// AssignºStrBool assigns boolean to string
func AssignºStrBool(ptr *string, value interface{}) (string, error) {
*ptr = strºBool(value.(int64))
return *ptr, nil
}
// AssignºStrInt assigns integer to string
func AssignºStrInt(ptr *string, value interface{}) (string, error) {
*ptr = fmt.Sprint(value)
return *ptr, nil
}
// AssignAddºStrStr appends one string to another
func AssignAddºStrStr(ptr *string, value interface{}) (string, error) {
*ptr += value.(string)
return *ptr, nil
}
// boolºStr converts string value to bool
func boolºStr(val string) int64 {
if len(val) != 0 && val != `0` && strings.ToLower(val) != `false` {
return 1
}
return 0
}
// FindºStrStr returns the index of the first instance of substr
func FindºStrStr(s, substr string) (off int64) {
off = int64(strings.Index(s, substr))
if off > 0 {
off = int64(len([]rune(s[:off])))
}
return
}
// floatºStr converts string value to float
func floatºStr(val string) (ret float64, err error) {
ret, err = strconv.ParseFloat(val, 64)
if err != nil {
err = errors.New(ErrorText(ErrStrToFloat))
}
return
}
// FormatºStr formats according to a format specifier and returns the resulting string
func FormatºStr(pattern string, pars ...interface{}) string {
return fmt.Sprintf(pattern, pars...)
}
// HasPrefixºStrStr returns true if the string s begins with prefix
func HasPrefixºStrStr(s, prefix string) int64 {
if strings.HasPrefix(s, prefix) {
return 1
}
return 0
}
// HasSuffixºStrStr returns true if the string s ends with suffix
func HasSuffixºStrStr(s, suffix string) int64 {
if strings.HasSuffix(s, suffix) {
return 1
}
return 0
}
// intºStr converts string value to int
func intºStr(val string) (ret int64, err error) {
ret, err = strconv.ParseInt(val, 0, 64)
if err != nil {
err = errors.New(ErrorText(ErrStrToInt))
}
return
}
// LeftºStrInt cuts the string.
func LeftºStrInt(s string, count int64) string {
r := []rune(s)
if int(count) > len(r) {
count = int64(len(r))
}
return string(r[:count])
}
// LinesºStr splits a string to a array of strings
func LinesºStr(in string) *core.Array {
out := core.NewArray()
items := strings.Split(in, "\n")
for _, item := range items {
out.Data = append(out.Data, strings.Trim(item, "\r"))
}
return out
}
// LowerºStr converts a copy of the string to their lower case and returns it.
func LowerºStr(s string) string {
return strings.ToLower(s)
}
// RepeatºStrInt returns a new string consisting of count copies of the specified string.
func RepeatºStrInt(input string, count int64) string {
return strings.Repeat(input, int(count))
}
// ReplaceºStrStrStr replaces strings in a string
func ReplaceºStrStrStr(in, old, new string) string {
return strings.Replace(in, old, new, -1)
}
// RightºStrInt returns the right substring of the string.
func RightºStrInt(s string, count int64) string {
r := []rune(s)
off := len(r) - int(count)
if off < 0 {
off = 0
}
return string(r[off:])
}
// ShiftºStr trims white spaces characters in the each line of the string.
func ShiftºStr(par string) string {
lines := strings.Split(par, "\n")
for i, v := range lines {
lines[i] = strings.TrimSpace(v)
}
return strings.Join(lines, "\n")
}
// SplitºStrStr splits a string to a array of strings
func SplitºStrStr(in, sep string) *core.Array {
out := core.NewArray()
items := strings.Split(in, sep)
for _, item := range items {
out.Data = append(out.Data, item)
}
return out
}
// SubstrºStrIntInt returns a substring with the specified offset and length
func SubstrºStrIntInt(in string, off, length int64) (string, error) {
var rin []rune
rin = []rune(in)
rlen := int64(len(rin))
if length < 0 {
length = -length
off -= length
}
if off < 0 || off >= rlen || off+length > rlen {
return ``, fmt.Errorf(ErrorText(ErrInvalidParam))
}
if length == 0 {
length = rlen - off
}
return string(rin[off : off+length]), nil
}
// TrimºStr returns a substring of the string with all leading and trailing characters in set removed.
func TrimºStr(in string, set string) string {
return strings.Trim(in, set)
}
// TrimLeftºStr returns a substring of the string with all leading characters in set removed.
func TrimLeftºStr(in string, set string) string {
return strings.TrimLeft(in, set)
}
// TrimRightºStr returns a substring of the string with all trailing characters in set removed.
func TrimRightºStr(in string, set string) string {
return strings.TrimRight(in, set)
}
// TrimSpaceºStr trims white space in a string
func TrimSpaceºStr(in string) string {
return strings.TrimSpace(in)
}
// UpperºStr converts a copy of the string to their upper case and returns it.
func UpperºStr(s string) string {
return strings.ToUpper(s)
}