-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_String2.ahk
308 lines (280 loc) · 8.66 KB
/
class_String2.ahk
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* this class allows us to interact upon strings similar to objects.
* it gets initialized by String_Set
* for optimal use place this is the "\lib" folder and include via
* #include <String>
*
* @Remarks
* ?: - Ternary operator [v1.0.46+].
* This operator is a shorthand replacement for the if-else statement.
* It evaluates the condition on its left side to determine which of
* its two branches should become its final result.
* For example, var := x>y ? 2 : 3 stores 2 in Var if x is greater than y;
* otherwise it stores 3.
* To enhance performance, only the winning branch is evaluated.
*/
class String {
static init := ("".base.base := String)
static __Set := Func("String_Set")
/**
* Getter meta function
* this allows to interact on given object directly using keywords
*
* @Parameters
* @Key - [string][int] identifier to trigger different functions.routines
* @Key2 - [string][int] identifier to trigger different functions.routines
*
* @Return
* string
*/
__Get(key, key2:="") {
if (key == "isInt") {
; checks if the content is an integer
if this is integer
{
out := true
} else {
out := false
}
} else if (key == "capitalize" || key == "caps") {
; capitalizes first letter in the string
out := Format("{1:U}{2:L}", Substr(this, 1, 1), SubStr(this, 2))
} else if (key == "lower") {
; converts string to lowercase
out := Format("{:L}", this)
} else if (key == "title") {
; Applies Title Case to the String
out := Format("{:T}", this)
} else if (key == "upper") {
; CONVERTS STRING TO UPPERCASE
out := Format("{:U}", this)
} else if (key == "reverse") || (key = "rev") {
; reverses string from left->right to right->left
; tfel>-thgir ot thgir>-tfel morf gnirts sesrever
DllCall("msvcrt\_" (A_IsUnicode? "wcs":"str") "rev", "UInt",&this, "CDecl"), out:=this
} else if (key == "length") || (key == "len") {
; returns the length of the string
out := StrLen(this)
} else if (key == "isEmpty") {
; checks if the string is empty
out := StrLen(this)? False:True
} else if (key.isInt && key2.isInt) {
; returns key2 characters of the string starting from key
out := SubStr(this, key, key2)
} else if (key.isInt && key2 == "") {
; returns a 1 character of the string starting from key
out := SubStr(this, key, 1)
} else if (key = "toHex") {
; transforms a number into a hex value
out := Format("{:#x}", this)
} else if (key = "toDec") {
; transforms a hex value into a decimal value - needs to be prefixed with 0x; e.g. 0xFF
out := Format("{:#d}", this)
} else if (key = "isStr") {
; checks if variable is a string by converting it to hex.
; if the conversion returns 0 instead of 0x it has to be a string
out := SubStr(Format("{:#x}", this), 1, 2)="0x"? False:True
}
return, out
}
/**
* counts occurences of given needle inside the string
*
* @Parameters
* @needle - [int][string] this defines what we want to have counted
*
* @Return
* int
*/
count(needle) {
StrReplace(this, needle , needle, count)
return, count
}
/**
* performs a check if the given needle exists in the string
* and returns it's position, if found
*
* @Parameters
* @needle - [string][int] value to search for
* @case_sensitive - [boolean] makes the search case sensitive
* @starting_pos - [int] sets the starting position
*
* @Return
* int
*/
inStr(needle, case_sensitive:=false, starting_pos:=1) {
out := InStr(this, needle, case_sensitive, starting_pos)
return, out
}
/**
* keeps the lines of a multiline variable and returns it
*
* @Parameters
* @lines - [string][int] as a string use a lists like this
* 3,5,11
*
* @Return
* string
*
* @Remarks
* int will only consider the first value
*/
keepLines(lines) {
VarSetCapacity(out, n:=StrLen(this))
Loop, Parse, this, `n, `r
if A_Index in %lines%
out .= A_LoopField "`n"
return, SubStr(out, 1, -1)
}
/**
* returns the leftmost characters
*
* @Parameters
* @boundary - [int] this sets how many characters should be returned
*
* @Return
* string
*/
left(boundary:=1) {
StringLeft, out, this, boundary
return, out
}
/**
* returns the rightmost characters
*
* @Parameters
* @boundary - [int] this sets how many characters should be returned
*
* @Return
* string
*/
right(boundary:=1) {
StringRight, out, this, boundary
return, out
}
/**
* trims the left most characters until boundary is reached
*
* @Parameters
* @boundary - [int] number of characters until trim ends
*
* @Return
* string
*/
trimL(boundary:="") {
if boundary.is_Str
out := boundary? LTrim(this, boundary):LTrim(this)
else
StringTrimLeft, out, this, boundary
return, out
}
/**
* trims the right most characters until boundary is reached
*
* @Parameters
* @boundary - [int] number of characters until trim ends
*
* @Return
* string
*/
trimR(boundary:="") {
if boundary.is_Str
return, boundary? RTrim(this, boundary):RTrim(this)
else
StringTrimRight, out, this, boundary
return, out
}
/**
* searches for the needle and replaces all occurences
*
* @Parameters
* @needle - [string] defines what to search for
* @replacement - [string] defines the replacement
*
* @Return
* string
*/
replace(needle, replacement:="") {
StringReplace, out, this, %needle%, %replacement%, A
return, out
}
/**
* searches for the needle and replaces all occurences via Regular Expression
*
* @Parameters
* @needle - [string] defines which Regular Expression to search for
* @replacement - [string] defines the replacement
*
* @Return
* string
*/
regExReplace(needle, replacement:="") {
return, RegExReplace(this, needle, replacement)
}
/**
* repeats the string defined number of times
*
* @Parameters
* @count - [int] count of how many times string should be repeated
*
* @Return
* string
*/
times(count) {
VarSetCapacity(out, count)
Loop, %count%
out .= this
return, out
}
/**
* splits string by delimiters and saves values in an object
*
* @Parameters
* @delim - [string] character which should be split upon
* @omit - [string] list of characters to exclude from the
* beginning and end of each array element
*
* @Return
* object
*/
split(delim:="", omit:="") {
out := []
if (SubStr(delim, 1, 2) = "R)") {
this := this.Replace(omit), pos := 0, n:=start:=1
if ((needle:=SubStr(delim, 3))="") {
return, this.split(needle)
}
while, pos := RegExMatch(this, needle, match, start) {
out[n++] := SubStr(this, start, pos-start), start := pos+StrLen(match)
}
out[n] := SubStr(this, start)
} else {
Loop, Parse, this, %delim%, %omit%
out[A_Index] := A_LoopField
}
return, out
}
/**
* removes the lines of a multiline variable and returns it
*
* @Parameters
* @lines - [string][int] as a string use a lists like this
* 3,5,11
*
* @Return
* string
*
* @Remarks
* int will only consider the first value
*/
removeLines(lines) {
VarSetCapacity(out, n:=StrLen(this))
Loop, Parse, this, `n, `r
if A_Index not in %lines%
out .= A_LoopField "`n"
return, SubStr(out, 1, -1)
}
}
String_Set(byref this, key, value){
StringReplace, this, this, %key%, %value%, all
}