@@ -13,9 +13,41 @@ class SimpleStringBuilder
13
13
{
14
14
15
15
/**
16
- * @var array
16
+ * @var string
17
17
*/
18
- private $ strings = array ();
18
+ private $ string ;
19
+
20
+ /**
21
+ * SimpleStringBuilder constructor.
22
+ *
23
+ * @param null $string
24
+ */
25
+ public function __construct ($ string = null )
26
+ {
27
+ if (!is_null ($ string )) {
28
+ if (!is_scalar ($ string )) {
29
+ $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
30
+ throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
31
+ }
32
+ $ this ->string = (string )$ string ;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * @param int $position
38
+ * @return string
39
+ */
40
+ public function charAt ($ position )
41
+ {
42
+ if (!is_int ($ position )) {
43
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
44
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
45
+ }
46
+ if ($ position > $ this ->length ()) {
47
+ return null ;
48
+ }
49
+ return mb_substr ($ this ->string , $ position , 1 );
50
+ }
19
51
20
52
/**
21
53
* @param string $string
@@ -27,7 +59,7 @@ public function append($string)
27
59
$ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
28
60
throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
29
61
}
30
- $ this ->strings [] = (string )$ string ;
62
+ $ this ->string . = (string )$ string ;
31
63
return $ this ;
32
64
}
33
65
@@ -41,7 +73,7 @@ public function prepend($string)
41
73
$ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
42
74
throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
43
75
}
44
- array_unshift ( $ this ->strings , (string )$ string) ;
76
+ $ this ->string = $ this -> string . (string )$ string ;
45
77
return $ this ;
46
78
}
47
79
@@ -50,29 +82,118 @@ public function prepend($string)
50
82
* @param string $string
51
83
* @return $this
52
84
*/
53
- public function replace ($ position , $ string )
85
+ public function insert ($ position , $ string )
54
86
{
87
+ if (!is_int ($ position )) {
88
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
89
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
90
+ }
55
91
if (!is_scalar ($ string )) {
56
92
$ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
57
93
throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
58
94
}
59
- if (! isset ( $ this ->strings [ $ position ] )) {
60
- throw new \ InvalidArgumentException ( ' Position ' . ( string ) $ position . ' invalid. ' );
95
+ if ($ position > $ this ->length ( )) {
96
+ $ position = $ this -> length ( );
61
97
}
62
- array_splice ($ this ->strings , $ position, 1 , (string )$ string );
98
+ $ this -> string = mb_substr ($ this ->string , 0 , $ position) . (string )$ string . mb_substr ( $ this -> string , $ position );
63
99
return $ this ;
64
100
}
65
101
66
102
/**
67
103
* @param int $position
104
+ * @param int $length
105
+ * @param string $string
68
106
* @return $this
69
107
*/
70
- public function remove ($ position )
108
+ public function replace ($ position, $ length , $ string )
71
109
{
72
- if (!isset ($ this ->strings [$ position ])) {
73
- throw new \InvalidArgumentException ('Position ' . (string )$ position . ' invalid. ' );
110
+ if (!is_int ($ position )) {
111
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
112
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
113
+ }
114
+ if (!is_int ($ length )) {
115
+ $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
116
+ throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
74
117
}
75
- array_splice ($ this ->strings , $ position , 1 );
118
+ if (!is_scalar ($ string )) {
119
+ $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
120
+ throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
121
+ }
122
+ if ($ position > $ this ->length ()) {
123
+ $ position = $ this ->length ();
124
+ }
125
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + $ length );
126
+ return $ this ;
127
+ }
128
+
129
+ /**
130
+ * @param int $position
131
+ * @param string $string
132
+ * @return $this
133
+ */
134
+ public function setCharAt ($ position , $ string )
135
+ {
136
+ if (!is_int ($ position )) {
137
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
138
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
139
+ }
140
+ if (!is_scalar ($ string )) {
141
+ $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
142
+ throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
143
+ }
144
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . (string )$ string . mb_substr ($ this ->string , $ position + 1 );
145
+ return $ this ;
146
+ }
147
+
148
+ /**
149
+ * @return $this
150
+ */
151
+ public function reverse ()
152
+ {
153
+ $ this ->string = strrev ($ this ->string );
154
+ return $ this ;
155
+ }
156
+
157
+ /**
158
+ * @param int $position
159
+ * @param int $length
160
+ * @return $this
161
+ */
162
+ public function delete ($ position , $ length = null )
163
+ {
164
+ if (!is_int ($ position )) {
165
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
166
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
167
+ }
168
+ if (!is_int ($ length ) && !is_null ($ length )) {
169
+ $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
170
+ throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
171
+ }
172
+ if ($ position > $ this ->length ()) {
173
+ return $ this ;
174
+ }
175
+ if (is_null ($ length )) {
176
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position );
177
+ } else {
178
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . mb_substr ($ this ->string , $ position + $ length );
179
+ }
180
+ return $ this ;
181
+ }
182
+
183
+ /**
184
+ * @param int $position
185
+ * @return $this
186
+ */
187
+ public function deleteCharAt ($ position )
188
+ {
189
+ if (!is_int ($ position )) {
190
+ $ type = is_object ($ position ) ? get_class ($ position ) : gettype ($ position );
191
+ throw new \InvalidArgumentException ('Position invalid. Expected integer. Got ' . $ type . '. ' );
192
+ }
193
+ if ($ position > $ this ->length ()) {
194
+ return $ this ;
195
+ }
196
+ $ this ->string = mb_substr ($ this ->string , 0 , $ position ) . mb_substr ($ this ->string , $ position + 1 );
76
197
return $ this ;
77
198
}
78
199
@@ -86,66 +207,88 @@ public function contains($string)
86
207
$ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
87
208
throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
88
209
}
89
- for ($ i = 0 , $ n = $ this ->size (); $ i < $ n ; $ i ++) {
90
- if ($ this ->strings [$ i ] === (string )$ string ) {
91
- return true ;
92
- }
210
+ return strpos ($ this ->string , (string )$ string ) !== false ;
211
+ }
212
+
213
+ /**
214
+ * @param string $string
215
+ * @param int $offset
216
+ * @return int
217
+ */
218
+ public function indexOf ($ string , $ offset = 0 )
219
+ {
220
+ if (!is_scalar ($ string )) {
221
+ $ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
222
+ throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
93
223
}
94
- return false ;
224
+ if (!is_int ($ offset )) {
225
+ $ type = is_object ($ offset ) ? get_class ($ offset ) : gettype ($ offset );
226
+ throw new \InvalidArgumentException ('Offset invalid. Expected integer. Got ' . $ type . '. ' );
227
+ }
228
+ return strpos ($ this ->string , (string )$ string , $ offset );
95
229
}
96
230
97
231
/**
98
232
* @param string $string
99
- * @return bool
233
+ * @param int $offset
234
+ * @return int
100
235
*/
101
- public function stringContains ($ string )
236
+ public function lastIndexOf ($ string, $ offset = 0 )
102
237
{
103
238
if (!is_scalar ($ string )) {
104
239
$ type = is_object ($ string ) ? get_class ($ string ) : gettype ($ string );
105
240
throw new \InvalidArgumentException ('Expected a scalar value. Got ' . $ type . '. ' );
106
241
}
107
- return strpos ($ this ->build (), (string )$ string ) !== false ;
242
+ if (!is_int ($ offset )) {
243
+ $ type = is_object ($ offset ) ? get_class ($ offset ) : gettype ($ offset );
244
+ throw new \InvalidArgumentException ('Offset invalid. Expected integer. Got ' . $ type . '. ' );
245
+ }
246
+ return strrpos ($ this ->string , (string )$ string , $ offset );
108
247
}
109
248
110
249
/**
111
250
* @return int
112
251
*/
113
252
public function size ()
114
253
{
115
- return count ($ this ->strings );
254
+ return strlen ($ this ->string );
116
255
}
117
256
118
257
/**
119
258
* @return int
120
259
*/
121
260
public function length ()
122
261
{
123
- return mb_strlen ($ this ->build () );
262
+ return mb_strlen ($ this ->string );
124
263
}
125
264
126
265
/**
127
266
* @param int $startPosition
128
- * @param int $size
267
+ * @param int $length
129
268
* @return string
130
269
*/
131
- public function buildSubstring ($ startPosition , $ size = null )
270
+ public function buildSubstring ($ startPosition , $ length = null )
132
271
{
133
- if (! isset ( $ this ->strings [ $ startPosition]) ) {
272
+ if ($ this ->length () > $ startPosition ) {
134
273
throw new \InvalidArgumentException ('Start position ' . (string )$ startPosition . ' invalid. ' );
135
274
}
136
- if (!is_int ($ size ) && !is_null ($ size )) {
137
- $ type = is_object ($ size ) ? get_class ($ size ) : gettype ($ size );
275
+ if (!is_int ($ length ) && !is_null ($ length )) {
276
+ $ type = is_object ($ length ) ? get_class ($ length ) : gettype ($ length );
138
277
throw new \InvalidArgumentException ('Length invalid. Expected integer. Got ' . $ type . '. ' );
139
278
}
140
- return implode ('' , array_slice ($ this ->strings , $ startPosition , $ size ));
279
+ if (is_null ($ length )) {
280
+ return mb_substr ($ this ->string , $ startPosition );
281
+ } else {
282
+ return mb_substr ($ this ->string , $ startPosition , $ length );
283
+ }
141
284
}
142
285
143
286
/**
144
287
* @return string
145
288
*/
146
289
public function build ()
147
290
{
148
- return implode ( '' , $ this ->strings ) ;
291
+ return $ this ->string ;
149
292
}
150
293
151
294
}
0 commit comments