@@ -67,6 +67,7 @@ public static function encode($hostname) {
6767 foreach ($ parts as &$ part ) {
6868 $ part = self ::to_ascii ($ part );
6969 }
70+
7071 return implode ('. ' , $ parts );
7172 }
7273
@@ -174,39 +175,31 @@ protected static function utf8_to_codepoints($input) {
174175 for ($ position = 0 ; $ position < $ strlen ; $ position ++) {
175176 $ value = ord ($ input [$ position ]);
176177
177- // One byte sequence:
178- if ((~$ value & 0x80 ) === 0x80 ) {
178+ if ((~$ value & 0x80 ) === 0x80 ) { // One byte sequence:
179179 $ character = $ value ;
180180 $ length = 1 ;
181181 $ remaining = 0 ;
182- }
183- // Two byte sequence:
184- elseif (($ value & 0xE0 ) === 0xC0 ) {
182+ } elseif (($ value & 0xE0 ) === 0xC0 ) { // Two byte sequence:
185183 $ character = ($ value & 0x1F ) << 6 ;
186184 $ length = 2 ;
187185 $ remaining = 1 ;
188- }
189- // Three byte sequence:
190- elseif (($ value & 0xF0 ) === 0xE0 ) {
186+ } elseif (($ value & 0xF0 ) === 0xE0 ) { // Three byte sequence:
191187 $ character = ($ value & 0x0F ) << 12 ;
192188 $ length = 3 ;
193189 $ remaining = 2 ;
194- }
195- // Four byte sequence:
196- elseif (($ value & 0xF8 ) === 0xF0 ) {
190+ } elseif (($ value & 0xF8 ) === 0xF0 ) { // Four byte sequence:
197191 $ character = ($ value & 0x07 ) << 18 ;
198192 $ length = 4 ;
199193 $ remaining = 3 ;
200- }
201- // Invalid byte:
202- else {
194+ } else { // Invalid byte:
203195 throw new Exception ('Invalid Unicode codepoint ' , 'idna.invalidcodepoint ' , $ value );
204196 }
205197
206198 if ($ remaining > 0 ) {
207199 if ($ position + $ length > $ strlen ) {
208200 throw new Exception ('Invalid Unicode codepoint ' , 'idna.invalidcodepoint ' , $ character );
209201 }
202+
210203 for ($ position ++; $ remaining > 0 ; $ position ++) {
211204 $ value = ord ($ input [$ position ]);
212205
@@ -218,6 +211,7 @@ protected static function utf8_to_codepoints($input) {
218211 --$ remaining ;
219212 $ character |= ($ value & 0x3F ) << ($ remaining * 6 );
220213 }
214+
221215 $ position --;
222216 }
223217
@@ -277,25 +271,26 @@ public static function punycode_encode($input) {
277271 // TODO: this should also check if it's valid for a URL
278272 $ output .= chr ($ char );
279273 $ h ++;
280- }
281- // Check if the character is non-ASCII, but below initial n
282- // This never occurs for Punycode, so ignore in coverage
283- // @codeCoverageIgnoreStart
284- elseif ($ char < $ n ) {
274+
275+ // Check if the character is non-ASCII, but below initial n
276+ // This never occurs for Punycode, so ignore in coverage
277+ // @codeCoverageIgnoreStart
278+ } elseif ($ char < $ n ) {
285279 throw new Exception ('Invalid character ' , 'idna.character_outside_domain ' , $ char );
286- }
287- // @codeCoverageIgnoreEnd
288- else {
280+ // @codeCoverageIgnoreEnd
281+ } else {
289282 $ extended [$ char ] = true ;
290283 }
291284 }
285+
292286 $ extended = array_keys ($ extended );
293287 sort ($ extended );
294288 $ b = $ h ;
295289 // [copy them] followed by a delimiter if b > 0
296290 if (strlen ($ output ) > 0 ) {
297291 $ output .= '- ' ;
298292 }
293+
299294 // {if the input contains a non-basic code point < n then fail}
300295 // while h < length(input) do begin
301296 $ codepointcount = count ($ codepoints );
@@ -313,9 +308,7 @@ public static function punycode_encode($input) {
313308 // if c < n then increment delta, fail on overflow
314309 if ($ c < $ n ) {
315310 $ delta ++;
316- }
317- // if c == n then begin
318- elseif ($ c === $ n ) {
311+ } elseif ($ c === $ n ) { // if c == n then begin
319312 // let q = delta
320313 $ q = $ delta ;
321314 // for k = base to infinity in steps of base do begin
@@ -324,17 +317,17 @@ public static function punycode_encode($input) {
324317 // tmax if k >= bias + tmax, or k - bias otherwise
325318 if ($ k <= ($ bias + self ::BOOTSTRAP_TMIN )) {
326319 $ t = self ::BOOTSTRAP_TMIN ;
327- }
328- elseif ($ k >= ($ bias + self ::BOOTSTRAP_TMAX )) {
320+ } elseif ($ k >= ($ bias + self ::BOOTSTRAP_TMAX )) {
329321 $ t = self ::BOOTSTRAP_TMAX ;
330- }
331- else {
322+ } else {
332323 $ t = $ k - $ bias ;
333324 }
325+
334326 // if q < t then break
335327 if ($ q < $ t ) {
336328 break ;
337329 }
330+
338331 // output the code point for digit t + ((q - t) mod (base - t))
339332 $ digit = $ t + (($ q - $ t ) % (self ::BOOTSTRAP_BASE - $ t ));
340333 $ output .= self ::digit_to_char ($ digit );
@@ -375,6 +368,7 @@ protected static function digit_to_char($digit) {
375368 if ($ digit < 0 || $ digit > 35 ) {
376369 throw new Exception (sprintf ('Invalid digit %d ' , $ digit ), 'idna.invalid_digit ' , $ digit );
377370 }
371+
378372 // @codeCoverageIgnoreEnd
379373 $ digits = 'abcdefghijklmnopqrstuvwxyz0123456789 ' ;
380374 return substr ($ digits , $ digit , 1 );
@@ -395,11 +389,11 @@ protected static function adapt($delta, $numpoints, $firsttime) {
395389 // if firsttime then let delta = delta div damp
396390 if ($ firsttime ) {
397391 $ delta = floor ($ delta / self ::BOOTSTRAP_DAMP );
398- }
399- // else let delta = delta div 2
400- else {
392+ } else {
393+ // else let delta = delta div 2
401394 $ delta = floor ($ delta / 2 );
402395 }
396+
403397 // let delta = delta + (delta div numpoints)
404398 $ delta += floor ($ delta / $ numpoints );
405399 // let k = 0
0 commit comments