@@ -58,8 +58,8 @@ protected function __construct(string $bencoded)
5858 {
5959 $ this ->bencoded = $ bencoded ;
6060 $ this ->len = strlen ($ bencoded );
61+ $ this ->max = $ this ->getSafeBoundary ();
6162
62- $ this ->computeSafeBoundary ();
6363 $ this ->checkBoundary ();
6464 }
6565
@@ -118,28 +118,6 @@ protected function complianceError(string $message, int $offset): void
118118 throw new ComplianceError ($ message , $ offset );
119119 }
120120
121- /**
122- * Adjust the rightmost boundary to the last safe character that can start a value
123- *
124- * Will rewind the boundary to skip the rightmost digits, optionally preceded by "i" or "i-"
125- */
126- protected function computeSafeBoundary (): void
127- {
128- $ boundary = $ this ->len - 1 ;
129- do
130- {
131- $ c = substr ($ this ->bencoded , $ boundary , 1 );
132- }
133- while (str_contains ('0123456789 ' , $ c ) && --$ boundary >= 0 );
134-
135- $ this ->max = match ($ c )
136- {
137- '- ' => $ boundary - 2 ,
138- 'i ' => $ boundary - 1 ,
139- default => $ boundary
140- };
141- }
142-
143121 protected static function convertTypeError (TypeError $ e , int $ offset ): Throwable
144122 {
145123 // A type error can occur in decodeString() if the string length exceeds an int
@@ -268,6 +246,28 @@ protected function dictionaryComplianceError(string $key, string $lastKey): void
268246 $ this ->complianceError ($ msg . " dictionary entry ' " . $ key . "' " , $ offset );
269247 }
270248
249+ /**
250+ * Return the rightmost boundary to the last safe character that can start a value
251+ *
252+ * Will rewind the boundary to skip the rightmost digits, optionally preceded by "i" or "i-"
253+ */
254+ protected function getSafeBoundary (): int
255+ {
256+ $ boundary = $ this ->len - 1 ;
257+ do
258+ {
259+ $ c = substr ($ this ->bencoded , $ boundary , 1 );
260+ }
261+ while (str_contains ('0123456789 ' , $ c ) && --$ boundary >= 0 );
262+
263+ return match ($ c )
264+ {
265+ '- ' => $ boundary - 2 ,
266+ 'i ' => $ boundary - 1 ,
267+ default => $ boundary
268+ };
269+ }
270+
271271 protected function readDigits (string $ terminator ): string
272272 {
273273 // Digits sorted by decreasing frequency as observed on a random sample of torrent files
0 commit comments