@@ -34,23 +34,32 @@ class UnicodeString extends AbstractUnicodeString
34
34
{
35
35
public function __construct (string $ string = '' )
36
36
{
37
- $ this ->string = normalizer_is_normalized ($ string ) ? $ string : normalizer_normalize ($ string );
37
+ if ('' === $ string || normalizer_is_normalized ($ this ->string = $ string )) {
38
+ return ;
39
+ }
38
40
39
- if (false === $ this -> string ) {
41
+ if (false === $ string = normalizer_normalize ( $ string ) ) {
40
42
throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
41
43
}
44
+
45
+ $ this ->string = $ string ;
42
46
}
43
47
44
48
public function append (string ...$ suffix ): static
45
49
{
46
50
$ str = clone $ this ;
47
51
$ str ->string = $ this ->string .(1 >= \count ($ suffix ) ? ($ suffix [0 ] ?? '' ) : implode ('' , $ suffix ));
48
- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
49
52
50
- if (false === $ str ->string ) {
53
+ if (normalizer_is_normalized ($ str ->string )) {
54
+ return $ str ;
55
+ }
56
+
57
+ if (false === $ string = normalizer_normalize ($ str ->string )) {
51
58
throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
52
59
}
53
60
61
+ $ str ->string = $ string ;
62
+
54
63
return $ str ;
55
64
}
56
65
@@ -209,12 +218,17 @@ public function prepend(string ...$prefix): static
209
218
{
210
219
$ str = clone $ this ;
211
220
$ str ->string = (1 >= \count ($ prefix ) ? ($ prefix [0 ] ?? '' ) : implode ('' , $ prefix )).$ this ->string ;
212
- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
213
221
214
- if (false === $ str ->string ) {
222
+ if (normalizer_is_normalized ($ str ->string )) {
223
+ return $ str ;
224
+ }
225
+
226
+ if (false === $ string = normalizer_normalize ($ str ->string )) {
215
227
throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
216
228
}
217
229
230
+ $ str ->string = $ string ;
231
+
218
232
return $ str ;
219
233
}
220
234
@@ -235,11 +249,16 @@ public function replace(string $from, string $to): static
235
249
}
236
250
237
251
$ str ->string = $ result .$ tail ;
238
- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
239
252
240
- if (false === $ str ->string ) {
253
+ if (normalizer_is_normalized ($ str ->string )) {
254
+ return $ str ;
255
+ }
256
+
257
+ if (false === $ string = normalizer_normalize ($ str ->string )) {
241
258
throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
242
259
}
260
+
261
+ $ str ->string = $ string ;
243
262
}
244
263
245
264
return $ str ;
@@ -269,12 +288,17 @@ public function splice(string $replacement, int $start = 0, int $length = null):
269
288
$ start = $ start ? \strlen (grapheme_substr ($ this ->string , 0 , $ start )) : 0 ;
270
289
$ length = $ length ? \strlen (grapheme_substr ($ this ->string , $ start , $ length ?? 2147483647 )) : $ length ;
271
290
$ str ->string = substr_replace ($ this ->string , $ replacement , $ start , $ length ?? 2147483647 );
272
- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
273
291
274
- if (false === $ str ->string ) {
292
+ if (normalizer_is_normalized ($ str ->string )) {
293
+ return $ str ;
294
+ }
295
+
296
+ if (false === $ string = normalizer_normalize ($ str ->string )) {
275
297
throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
276
298
}
277
299
300
+ $ str ->string = $ string ;
301
+
278
302
return $ str ;
279
303
}
280
304
0 commit comments