| @@ -45,15 +45,24 @@ | ||
| 45 | 45 | * Read characters until we reach the given character $char. |
| 46 | 46 | * By default, discard that final matching character and return the rest. |
| 47 | 47 | * |
| 48 | 48 | * @param string $char |
| 49 | + * @param bool $discard_char | |
| 49 | 50 | * @return string |
| 50 | 51 | */ |
| 51 | - public function read_until( $char ) { | |
| 52 | + public function read_until( $char, $discard_char = true ) { | |
| 52 | 53 | $value = ''; |
| 53 | - while ( $this->pos <= $this->max && ( $one = $this->string[ $this->pos++ ] ) !== $char ) { | |
| 54 | - $value .= $one; | |
| 54 | + | |
| 55 | + while ( null !== ( $one = $this->read_one() ) ) { | |
| 56 | + if ( $one !== $char || ! $discard_char ) { | |
| 57 | + $value .= $one; | |
| 58 | + } | |
| 59 | + | |
| 60 | + if ( $one === $char ) { | |
| 61 | + break; | |
| 62 | + } | |
| 55 | 63 | } |
| 64 | + | |
| 56 | 65 | return $value; |
| 57 | 66 | } |
| 58 | 67 | |
| 59 | 68 | /** |
| @@ -66,39 +75,49 @@ | ||
| 66 | 75 | */ |
| 67 | 76 | public function read( $count ) { |
| 68 | 77 | $value = ''; |
| 69 | 78 | |
| 70 | - while ( $count > 0 && $this->pos <= $this->max && ( ( $one = $this->string[ $this->pos ] ) || '0' === $one ) ) { | |
| 71 | - $value .= $one; | |
| 72 | - $this->pos += 1; | |
| 79 | + while ( $count > 0 && ! is_null( $one = $this->read_one() ) ) { | |
| 80 | + $value .= $one; | |
| 73 | 81 | --$count; |
| 74 | 82 | } |
| 75 | 83 | |
| 76 | - /** | |
| 77 | - * Remove a single set of double-quotes from around a string. | |
| 78 | - * | |
| 79 | - * Examples: | |
| 80 | - * abc => abc | |
| 81 | - * "abc" => abc | |
| 82 | - * ""abc"" => "abc" | |
| 83 | - */ | |
| 84 | - if ( strlen( $value ) < 2 || substr( $value, 0, 1 ) !== '"' || substr( $value, -1, 1 ) !== '"' ) { | |
| 85 | - // Only remove exactly one quote from the start and the end and then only if there is one at each end. | |
| 86 | - // Too short, or does not start or end with a quote. | |
| 87 | - return $value; | |
| 84 | + return $this->strip_quotes( $value ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * Read the next character from the supplied string. | |
| 89 | + * Return null when we have run out of characters. | |
| 90 | + * | |
| 91 | + * @return string|null | |
| 92 | + */ | |
| 93 | + private function read_one() { | |
| 94 | + if ( $this->pos <= $this->max ) { | |
| 95 | + $value = $this->string[ $this->pos ]; | |
| 96 | + $this->pos += 1; | |
| 97 | + } else { | |
| 98 | + $value = null; | |
| 88 | 99 | } |
| 89 | - | |
| 90 | - // Return the middle of the string, from the second character to the second-but-last. | |
| 91 | - return substr( $value, 1, -1 ); | |
| 100 | + return $value; | |
| 92 | 101 | } |
| 93 | 102 | |
| 94 | 103 | /** |
| 95 | - * Shift the position. This is used to skip a character that we don't need to read. | |
| 104 | + * Remove a single set of double-quotes from around a string. | |
| 105 | + * abc => abc | |
| 106 | + * "abc" => abc | |
| 107 | + * ""abc"" => "abc" | |
| 96 | 108 | * |
| 97 | - * @since 6.9.1 This was added as an optimization. | |
| 98 | - * | |
| 99 | - * @return void | |
| 109 | + * @param string $string | |
| 110 | + * @return string | |
| 100 | 111 | */ |
| 101 | - public function skip_next_character() { | |
| 102 | - $this->pos += 1; | |
| 112 | + private function strip_quotes( $string ) { | |
| 113 | + // Only remove exactly one quote from the start and the end and then only if there is one at each end. | |
| 114 | + | |
| 115 | + if ( strlen( $string ) < 2 || substr( $string, 0, 1 ) !== '"' || substr( $string, -1, 1 ) !== '"' ) { | |
| 116 | + // Too short, or does not start or end with a quote. | |
| 117 | + return $string; | |
| 118 | + } | |
| 119 | + | |
| 120 | + // Return the middle of the string, from the second character to the second-but-last. | |
| 121 | + return substr( $string, 1, -1 ); | |
| 103 | 122 | } |
| 104 | 123 | } |