@@ -34,23 +34,32 @@ class UnicodeString extends AbstractUnicodeString
3434{
3535 public function __construct (string $ string = '' )
3636 {
37- $ this ->string = normalizer_is_normalized ($ string ) ? $ string : normalizer_normalize ($ string );
37+ if ('' === $ string || normalizer_is_normalized ($ this ->string = $ string )) {
38+ return ;
39+ }
3840
39- if (false === $ this -> string ) {
41+ if (false === $ string = normalizer_normalize ( $ string ) ) {
4042 throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
4143 }
44+
45+ $ this ->string = $ string ;
4246 }
4347
4448 public function append (string ...$ suffix ): static
4549 {
4650 $ str = clone $ this ;
4751 $ str ->string = $ this ->string .(1 >= \count ($ suffix ) ? ($ suffix [0 ] ?? '' ) : implode ('' , $ suffix ));
48- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
4952
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 )) {
5158 throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
5259 }
5360
61+ $ str ->string = $ string ;
62+
5463 return $ str ;
5564 }
5665
@@ -209,12 +218,17 @@ public function prepend(string ...$prefix): static
209218 {
210219 $ str = clone $ this ;
211220 $ str ->string = (1 >= \count ($ prefix ) ? ($ prefix [0 ] ?? '' ) : implode ('' , $ prefix )).$ this ->string ;
212- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
213221
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 )) {
215227 throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
216228 }
217229
230+ $ str ->string = $ string ;
231+
218232 return $ str ;
219233 }
220234
@@ -235,11 +249,16 @@ public function replace(string $from, string $to): static
235249 }
236250
237251 $ str ->string = $ result .$ tail ;
238- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
239252
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 )) {
241258 throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
242259 }
260+
261+ $ str ->string = $ string ;
243262 }
244263
245264 return $ str ;
@@ -269,12 +288,17 @@ public function splice(string $replacement, int $start = 0, int $length = null):
269288 $ start = $ start ? \strlen (grapheme_substr ($ this ->string , 0 , $ start )) : 0 ;
270289 $ length = $ length ? \strlen (grapheme_substr ($ this ->string , $ start , $ length ?? 2147483647 )) : $ length ;
271290 $ str ->string = substr_replace ($ this ->string , $ replacement , $ start , $ length ?? 2147483647 );
272- normalizer_is_normalized ($ str ->string ) ?: $ str ->string = normalizer_normalize ($ str ->string );
273291
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 )) {
275297 throw new InvalidArgumentException ('Invalid UTF-8 string. ' );
276298 }
277299
300+ $ str ->string = $ string ;
301+
278302 return $ str ;
279303 }
280304
0 commit comments