-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrings.php
More file actions
280 lines (260 loc) Β· 7.67 KB
/
Strings.php
File metadata and controls
280 lines (260 loc) Β· 7.67 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
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
<?php
namespace Vector\Lib;
use Vector\Core\Module;
use Vector\Core\Curry;
class Strings
{
use Module;
/**
* String Concatenation
*
* Concatenates the first argument to the second argument, provided both arguments
* are strings. Defers to the PHP built-in concatenation.
*
* @param String $addition Thing to append
* @param String $original Thing to append to
* @return String Concatenated strings
* @example
* Strings::concat('World', $concat('ello', 'H')); // 'HelloWorld'
*
* @type String -> String -> String
*
* @example
* Strings::concat('as', 'df'); // 'dfas'
*
*/
#[Curry]
protected static function concat(string $addition, string $original): string
{
return $original . $addition;
}
/**
* String Splitting
*
* Split a string into parts based on a delimiter. Operates similar to php `explode`,
* but is more consistent. Can split on empty delimiters, and trims out empty strings
* after exploding.
*
* @param String $on Split delimiter
* @param String $string Thing to split into pieces
* @return array List of chunks from splitting the string
* @example
* Strings::split('-', 'Hello-World'); // ['Hello', 'World']
*
* @example
* Strings::split('', 'asdf'); // ['a', 's', 'd', 'f']
*
* @example
* Strings::split('-', 'foo-bar-'); ['foo', 'bar']
*
* @type String -> String -> [String]
*
*/
#[Curry]
protected static function split(string $on, string $string): array
{
if ($on === '') {
return str_split($string);
}
return array_filter(explode($on, $string));
}
/**
* Substring Match
*
* Determines if a string starts with a specific substring. Returns true if the string
* matches the substring at its start, otherwise false.
*
* @param String $subStr Substring to test
* @param String $str String to run test on
* @return Bool Whether or not the string starts with the substring
* @example
* Strings::startsWith('foo', 'barfoo'); false
*
* @type String -> String -> Bool
*
* @example
* Strings::startsWith('as', 'asdf'); true
*
*/
#[Curry]
protected static function startsWith(string $subStr, string $str): bool
{
return substr($str, 0, strlen($subStr)) === $subStr;
}
/**
* Lowercase Conversion
*
* Converts a string to lowercase.
*
* @param String $str Original string
* @return String Lowercase string
* @example
* Strings::toLowercase('ASdf'); // 'asdf'
*
* @type String -> String
*
*/
#[Curry]
protected static function toLowercase(string $str): string
{
return strtolower($str);
}
/**
* Uppercase Conversion
*
* Converts a string to uppercase.
*
* @param String $str Original string
* @return String Uppercase string
* @example
* Strings::toUppercase('asdf'); // 'ASDF'
*
* @type String -> String
*
*/
#[Curry]
protected static function toUppercase(string $str): string
{
return strtoupper($str);
}
/**
* Trim Whitespace
*
* Removes all leading and trailing whitespace from a string. Defers to
* PHP trim.
*
* @param String $str string to trim
* @return string
* @example
* Strings::trim(' asdf '); // 'asdf'
*
* @type String -> String
*
*/
#[Curry]
protected static function trim(string $str): string
{
return trim($str);
}
/**
* Left Chomp
*
* Removes the specified substring from the left end of the target string. Unlike PHP's
* trim function, the substring to chomp is not a character mask -- rather it is a full
* substring. This function is case sensitive.
*
* @param String $toChomp string to chomp
* @param String $string string to be chomped
* @return string
* @example
* Strings::lchomp('He', 'Hello World'); // 'llo World'
* Strings::lchomp('Hi', 'Hello World'); // 'Hello World'
* Strings::lchomp('he', 'Hello World'); // 'Hello World'
*
* @type String -> String
*
*/
#[Curry]
protected static function lchomp(string $toChomp, string $string): string
{
$length = strlen($toChomp);
if (strcmp(substr($string, 0, $length), $toChomp) === 0) {
return substr($string, $length);
}
return $string;
}
/**
* Right Chomp
*
* Removes the specified substring from the right end of the target string. Unlike PHP's
* trim function, the substring to chomp is not a character mask -- rather it is a full
* substring. This function is case sensitive.
*
* @param String $toChomp string to chomp
* @param String $string string to be chomped
* @return string
* @example
* Strings::rchomp('ld', 'Hello World'); // 'Hello Wor'
* Strings::rchomp('li', 'Hello World'); // 'Hello World'
* Strings::rchomp('LD', 'Hello World'); // 'Hello World'
*
* @type String -> String
*
*/
#[Curry]
protected static function rchomp(string $toChomp, string $string): string
{
$length = strlen($toChomp);
if (strcmp(substr($string, -$length, $length), $toChomp) === 0) {
$string = substr($string, 0, -$length);
}
return $string;
}
/**
* Two-Sided Chomp
*
* Removes the specified substring from both ends of the target string. Unlike PHP's
* trim function, the substring to chomp is not a character mask -- rather it is a full
* substring. This function is case sensitive.
*
* @param String $toChomp string to chomp
* @param String $string string to be chomped
* @return string
* @example
* Strings::chomp('a', 'abccba'); // 'bccb'
* Strings::chomp('ab', 'abccba'); // 'abccba'
* Strings::chomp('A', 'abccba'); // 'abccba'
*
* @type String -> String
*
*/
#[Curry]
protected static function chomp(string $toChomp, string $string): string
{
$chomp = Lambda::compose(self::using('lchomp')($toChomp), self::using('rchomp')($toChomp));
return $chomp($string);
}
/**
* String Joining
*
* Joins an array of strings together with a given delimiter. Works similarly
* to PHP `implode`. The inverse of `split`.
*
* @param String $on Delimiter to join on
* @param array $string List of strings to join together
* @return String Joined string based on delimiter
* @example
* Strings::join('', ['a', 's', 'd', 'f']); // 'asdf'
*
* @type String -> [String] -> String
*
* @example
* Strings::join(' ', ['Hello', 'World']); // 'Hello World'
*
*/
#[Curry]
protected static function join(string $on, array $string): string
{
return implode($on, $string);
}
/**
* String Replace
*
* Replace all occurrences of the search string with the replacement string.
*
* @param String $substring Substring to find
* @param $replacement
* @param String $string Replacement string
* @return String $string Result after replacement
* @example
* Strings::replace('test', 'passes', 'this test']); // 'this passes'
*
* @internal param String $ -> String -> String
*
*/
#[Curry]
protected static function replace(string $substring, string $replacement, string $string): string
{
return str_replace($substring, $replacement, $string);
}
}