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