-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.php
308 lines (236 loc) · 7.71 KB
/
String.php
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
<?php
class Phuture_String implements ArrayAccess, Countable
{
private $_value = '';
function __construct($value = '') {
$this->_value = $value;
}
function getValue() {
return $this->_value;
}
function setValue($value) {
$this->_value = strval($value);
}
function __toString() {
return $this->_value;
}
function isEmpty() {
return empty($this->_value);
}
function length() {
return strlen($this->_value);
}
function clear() {
$this->_value = '';
}
// Access
private function _offset($index) {
return $index >= 0 ? $index : $this->length() + $index;
}
function get($index, $to = null) {
if ($to === null) {
// using substr() we can avoid using _offset (slower)
return new self(substr($this->_value, $index, 1));
} else {
$from = $this->_offset($index);
$to = $this->_offset($to);
if ($to > $from) {
return new self(substr($this->_value, $from,
$to - $from + 1));
} else {
return new self('');
}
}
}
function set($index, $value) {
$this->_value[$this->_offset($index)] = $value;
}
function delete($index) {
$i = $this->_offset($index);
return new self(substr($this->_value, 0, $i) .
substr($this->_value, $i + 1));
}
function getByte($index) {
return ord($this->_value[$this->_offset($index)]);
}
function setByte($index, $value) {
$this->_value[$this->_offset($index)] = chr($value);
}
function substring($from, $length = null) {
if ($length === null) {
return new self(substr($this->_value, $from));
} else {
return new self(substr($this->_value, $from, $length));
}
}
// ArrayAccess
function offsetExists($offset) {
return $offset >= -$this->length() && $offset < $this->length();
}
function offsetGet($offset) {
return $this->get($offset);
}
function offsetSet($offset, $value) {
$this->set($offset, $value);
}
function offsetUnset($offset) {
$this->_value = $this->delete($offset)->getValue();
}
// Countable interface
function count() {
return $this->length();
}
// Concatenation
function append($suffix) {
return new self($this->_value . $suffix);
}
function prepend($prefix) {
return new self($prefix . $this->_value);
}
// Case manipulation
function uppercase() {
return new self(strtoupper($this->_value));
}
function lowercase() {
return new self(strtolower($this->_value));
}
function capitalize() {
return new self(ucfirst($this->_value));
}
function capitalizeWords() {
return new self(ucwords($this->_value));
}
function swapCase() {
$result = '';
$length = strlen($this->_value);
for ($i = 0; $i < $length; $i++) {
$char = $this->_value[$i];
$result .= ctype_upper($char) ? strtolower($char)
: strtoupper($char);
}
return new self($result);
}
// String type
function isUppercase() {
// Shortcuts
if ($this->length() == 0) { return false; }
if ($this->length() == 1 || $this->isAlpha()) {
return ctype_upper($this->_value);
}
$cased = false;
$length = strlen($this->_value);
for ($i = 0; $i < $length; $i++) {
$char = $this->_value[$i];
if (ctype_lower($char)) {
return false;
} else if (!$cased && ctype_upper($char)) {
$cased = true;
}
}
return $cased;
}
function isLowercase() {
// Shortcuts
if ($this->length() == 0) { return false; }
if ($this->length() == 1 || $this->isAlpha()) {
return ctype_lower($this->_value);
}
$cased = false;
$length = strlen($this->_value);
for ($i = 0; $i < $length; $i++) {
$char = $this->_value[$i];
if (ctype_upper($char)) {
return false;
} else if (!$cased && ctype_lower($char)) {
$cased = true;
}
}
return $cased;
}
function isAlphanumeric() { return ctype_alnum($this->_value); }
function isAlpha() { return ctype_alpha($this->_value); }
function isNumeric() { return ctype_digit($this->_value); }
function isSpace() { return ctype_space($this->_value); }
// Trimming
function trim($chars = null) {
return new self($chars === null ? trim($this->_value)
: trim($this->_value, $chars));
}
function trimLeft($chars = null) {
return new self($chars === null ? ltrim($this->_value)
: ltrim($this->_value, $chars));
}
function trimRight($chars = null) {
return new self($chars === null ? rtrim($this->_value)
: rtrim($this->_value, $chars));
}
function chop($chars = null) {
return $this->trimRight($chars);
}
// String content
function startsWith($start) {
return strpos($this->_value, strval($start)) === 0;
}
function endsWith($end) {
return substr($this->_value, $this->length() - strlen($end)) == $end;
}
function find($substring, $offset = 0) {
return strpos($this->_value, strval($substring), $offset);
}
function caseFind($substring, $offset = 0) {
return stripos($this->_value, strval($substring), $offset);
}
function findRight($substring, $offset = 0) {
return strrpos($this->_value, strval($substring), $offset);
}
function caseFindRight($substring, $offset = 0) {
return strripos($this->_value, strval($substring), $offset);
}
// Replacement
function replace($old, $new) {
return new self(str_replace($old, $new, $this->_value));
}
function caseReplace($old, $new) {
return new self(str_ireplace($old, $new, $this->_value));
}
// Padding
function padLeft($length, $string = ' ') {
return new self(str_pad($this->_value, $length, $string,
STR_PAD_LEFT));
}
function padRight($length, $string = ' ') {
return new self(str_pad($this->_value, $length, $string,
STR_PAD_RIGHT));
}
function padBoth($length, $string = ' ') {
return new self(str_pad($this->_value, $length, $string,
STR_PAD_BOTH));
}
function center($length, $string = ' ') {
return $this->padBoth($length, $string);
}
// Other manipulation functions
function reverse() {
return new self(strrev($this->_value));
}
function repeat($times) {
return new self(str_repeat($this->_value, $times));
}
function insert($index, $string) {
return new self(substr($this->_value, 0, $index) . $string .
substr($this->_value, $index));
}
function join($pieces) {
return new self(implode($this->_value, $pieces));
}
// String comparison
function compareTo($other) {
$cmp = strcmp($this, $other);
return $cmp === 0 ? 0 : ($cmp < 0 ? -1 : 1);
}
function caseCompareTo($other) {
$cmp = strcasecmp($this, $other);
return $cmp === 0 ? 0 : ($cmp < 0 ? -1 : 1);
}
}
function s($string) { return new Phuture_String($string); }