-
Notifications
You must be signed in to change notification settings - Fork 16
/
substr.go
45 lines (39 loc) · 1.11 KB
/
substr.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
package dynaml
import ()
func func_substr(arguments []interface{}, binding Binding) (interface{}, EvaluationInfo, bool) {
info := DefaultInfo()
if len(arguments) > 3 || len(arguments) < 2 {
return info.Error("substr takes two to three arguments")
}
str, ok := arguments[0].(string)
if !ok {
return info.Error("first argument for substr must be a string")
}
start, ok := arguments[1].(int64)
if !ok {
return info.Error("second argument for substr must be an integer")
}
if start < 0 {
start = int64(len(str)) + start
}
var end int64 = int64(len(str))
if len(arguments) >= 3 {
end, ok = arguments[2].(int64)
if !ok {
return info.Error("third argument for substr must be an integer")
}
if end < 0 {
end = int64(len(str)) + end
}
}
if int64(len(str)) < end {
return info.Error("substr effective end index (%d) exceeds string length (%d)", end, len(str))
}
if start < 0 {
return info.Error("negative substr effective start index (%d)", start)
}
if start > end {
return info.Error("substr start index (%d) aftsre end index (%d) ", start, end)
}
return str[start:end], info, true
}