@@ -18,31 +18,42 @@ public function __construct(array $config)
1818
1919 protected function decodeChain (array $ tokens ): array
2020 {
21- $ newTokens = [];
2221 $ previousByteTokens = [];
22+ $ newTokens = [];
2323
2424 foreach ($ tokens as $ token ) {
2525 $ bytes = null ;
26+
27+ // Check if the token is of the form <0xXX>
2628 if (strlen ($ token ) === 6 && str_starts_with ($ token , '<0x ' ) && str_ends_with ($ token , '> ' )) {
29+ // Extract the hexadecimal value from the token
2730 $ byte = hexdec (substr ($ token , 3 , 2 ));
2831 if (!is_nan ($ byte )) {
2932 $ bytes = $ byte ;
3033 }
3134 }
35+
3236 if ($ bytes !== null ) {
37+ // Add byte to previousByteTokens
3338 $ previousByteTokens [] = $ bytes ;
3439 } else {
35- if (count ($ previousByteTokens ) > 0 ) {
36- $ string = $ this ->bytesToString ($ previousByteTokens );
37- $ newTokens [] = $ string ;
38- $ previousByteTokens = [];
40+ // If we have accumulated byte tokens, decode them to a string
41+ if (!empty ($ previousByteTokens )) {
42+ $ string = pack ('C* ' , ...$ previousByteTokens ); // Convert bytes back to string
43+ $ newTokens [] = $ string ; // Add decoded string to newTokens
44+ $ previousByteTokens = []; // Reset byte accumulator
3945 }
46+ // Add the non-byte token to newTokens
4047 $ newTokens [] = $ token ;
4148 }
4249 }
43- if (count ($ previousByteTokens ) > 0 ) {
44- $ string = $ this ->bytesToString ($ previousByteTokens );
50+
51+
52+ // After the loop, if there are still byte tokens, decode them
53+ if (!empty ($ previousByteTokens )) {
54+ $ string = pack ('C* ' , ...$ previousByteTokens ); // Convert remaining bytes to string
4555 $ newTokens [] = $ string ;
56+ $ previousByteTokens = []; // Reset byte accumulator
4657 }
4758
4859 return $ newTokens ;
@@ -59,4 +70,4 @@ protected function bytesToString(array $bytes): string
5970 $ binaryString = pack ('C* ' , ...$ bytes );
6071 return mb_convert_encoding ($ binaryString , 'ISO-8859-1 ' );
6172 }
62- }
73+ }
0 commit comments