-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathstring.brs
138 lines (108 loc) · 4.61 KB
/
string.brs
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
'
' string.brs
'
'
function StringUtil() as Object
util = {
isString: function(value) as Boolean
return type(value) = "String" or type(value) = "roString"
end function
charAt: function(str as String, index=0 as Integer) as String
return str.mid(index, 1)
end function
startsWith: function(str as String, substr as String) as Boolean
return str.left(substr.len()) = substr
end function
endsWith: function(str as String, substr as String) as Boolean
return str.right(substr.len()) = substr
end function
contains: function(str as String, substr as String, position=0 as Integer) as Boolean
return m.indexOf(str, substr, position) >= 0
end function
indexOf: function(str as String, substr as String, position=0 as Integer) as Integer
return str.instr(position, substr)
end function
match: function(str as String, regex as String, flag="" as String) as Object
regexObj = createObject("roRegex", regex, flag)
return regexObj.match(str)
end function
replace: function(str as String, pattern as String, replacement as String) as String
regexObj = createObject("roRegex", pattern, "")
return regexObj.replaceAll(str, replacement)
end function
truncate: function(str as String, length as Integer, ellipsis="" as String) as String
truncated = str
if truncated.len() > length then
truncated = truncated.left(length) + ellipsis
end if
return truncated
end function
repeat: function(str as String, count as Integer) as String
return string(count, str)
end function
_padToLength: function(str as String, targetLength as Integer) as String
strLength = str.len()
if targetLength = 0 or strLength = 0 then return ""
repeatCount = int(targetLength / strLength) + 1
return m.repeat(str, repeatCount).left(targetLength)
end function
padStart: function(str as String, targetLength as Integer, padString=" " as String) as String
strLength = str.len()
if targetLength <= strLength then return str
padLength = targetLength - strLength
return m._padToLength(padString, padLength) + str
end function
padEnd: function(str as String, targetLength as Integer, padString=" " as String) as String
strLength = str.len()
if targetLength <= strLength then return str
padLength = targetLength - strLength
return str + m._padToLength(padString, padLength)
end function
concat: function(str as String, value) as String
return str + m.toString(value)
end function
toString: function(value) as String
valueType = type(value)
if valueType = "<uninitialized>" then
value = "<uninitialized>"
else if valueType = "roSGNode" then
value = value.id
else if valueType = "roAssociativeArray" then
items = ""
for each key in value
items += key.toStr() + ": " + m.toString(value[key]) + ", "
end for
value = "{ " + items.left(items.len()-2) + " }"
else if valueType = "roList" or valueType = "roArray" then
items = ""
for each item in value
items = m.concat(items, item) + ", "
end for
value = "[" + items.left(items.len()-2) + "]"
else if valueType = "roDateTime" then
value = value.asSeconds()
end if
return box(value).toStr()
end function
toMD5: function(str as String) as String
return m._hash(str, "md5")
end function
toSHA1: function(str as String) as String
return m._hash(str, "sha1")
end function
toSHA256: function(str as String) as String
return m._hash(str, "sha256")
end function
toSHA512: function(str as String) as String
return m._hash(str, "sha512")
end function
_hash: function(msg as String, algorithm="md5" as String) as String
ba = createObject("roByteArray")
ba.fromAsciiString(msg)
digest = createObject("roEVPDigest")
digest.setup(algorithm)
return digest.process(ba)
end function
}
return util
end function