-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.go
More file actions
124 lines (118 loc) · 5.91 KB
/
strings.go
File metadata and controls
124 lines (118 loc) · 5.91 KB
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
package LibraryController
import (
"errors"
"fmt"
"reflect"
"github.com/Eclalang/Ecla/interpreter/eclaType"
"github.com/Eclalang/LibraryController/utils"
"github.com/Eclalang/strings"
)
type Strings struct {
functionMap map[string]interface{}
}
func NewStrings() *Strings {
return &Strings{
functionMap: map[string]interface{}{
"contains": nil,
"containsAny": nil,
"count": nil,
"cut": nil,
"hasPrefix": nil,
"hasSuffix": nil,
"indexOf": nil,
"join": nil,
"replace": nil,
"replaceAll": nil,
"split": nil,
"splitAfter": nil,
"splitAfterN": nil,
"splitN": nil,
"toLower": nil,
"toUpper": nil,
"trim": nil,
},
}
}
func (s *Strings) Call(name string, args []eclaType.Type) ([]eclaType.Type, error) {
newArgs := make([]any, len(args))
for k, arg := range args {
newArgs[k] = utils.EclaTypeToGo(arg)
}
if _, ok := s.functionMap[name]; !ok {
return nil, errors.New(fmt.Sprintf("Method %s not found in package strings", name))
}
switch name {
case "contains":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.Contains(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "containsAny":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.ContainsAny(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "count":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.Count(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "cut":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
before, after, found := strings.Cut(newArgs[0].(string), newArgs[1].(string))
return []eclaType.Type{utils.GoToEclaType(before), utils.GoToEclaType(after), utils.GoToEclaType(found)}, nil
}
case "hasPrefix":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.HasPrefix(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "hasSuffix":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.HasSuffix(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "indexOf":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.IndexOf(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "join":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.Slice && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.Join(newArgs[0].([]string), newArgs[1].(string)))}, nil
}
case "replace":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.String && reflect.TypeOf(newArgs[3]).Kind() == reflect.Int && len(newArgs) == 4 {
return []eclaType.Type{utils.GoToEclaType(strings.Replace(newArgs[0].(string), newArgs[1].(string), newArgs[2].(string), newArgs[3].(int)))}, nil
}
case "replaceAll":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.String && len(newArgs) == 3 {
return []eclaType.Type{utils.GoToEclaType(strings.ReplaceAll(newArgs[0].(string), newArgs[1].(string), newArgs[2].(string)))}, nil
}
case "split":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.Split(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "splitAfter":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.SplitAfter(newArgs[0].(string), newArgs[1].(string)))}, nil
}
case "splitAfterN":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.Int && len(newArgs) == 3 {
return []eclaType.Type{utils.GoToEclaType(strings.SplitAfterN(newArgs[0].(string), newArgs[1].(string), newArgs[2].(int)))}, nil
}
case "splitN":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && reflect.TypeOf(newArgs[2]).Kind() == reflect.Int && len(newArgs) == 3 {
return []eclaType.Type{utils.GoToEclaType(strings.SplitN(newArgs[0].(string), newArgs[1].(string), newArgs[2].(int)))}, nil
}
case "toLower":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(strings.ToLower(newArgs[0].(string)))}, nil
}
case "toUpper":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && len(newArgs) == 1 {
return []eclaType.Type{utils.GoToEclaType(strings.ToUpper(newArgs[0].(string)))}, nil
}
case "trim":
if reflect.TypeOf(newArgs[0]).Kind() == reflect.String && reflect.TypeOf(newArgs[1]).Kind() == reflect.String && len(newArgs) == 2 {
return []eclaType.Type{utils.GoToEclaType(strings.Trim(newArgs[0].(string), newArgs[1].(string)))}, nil
}
default:
return nil, errors.New(fmt.Sprintf("Method %s not found in package strings", name))
}
return []eclaType.Type{eclaType.Null{}}, nil
}