| @@ -45,16 +45,22 @@ | ||
| 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 | - * | |
| 49 | + * @param bool $discard_char | |
| 50 | 50 | * @return string |
| 51 | 51 | */ |
| 52 | - public function read_until( $char ) { | |
| 52 | + public function read_until( $char, $discard_char = true ) { | |
| 53 | 53 | $value = ''; |
| 54 | 54 | |
| 55 | - while ( $this->pos <= $this->max && ( $one = $this->string[ $this->pos++ ] ) !== $char ) { | |
| 56 | - $value .= $one; | |
| 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 | + } | |
| 57 | 63 | } |
| 58 | 64 | |
| 59 | 65 | return $value; |
| 60 | 66 | } |
| @@ -64,45 +70,54 @@ | ||
| 64 | 70 | * whichever comes first. |
| 65 | 71 | * By default, remove enclosing double-quotes from the result. |
| 66 | 72 | * |
| 67 | 73 | * @param int $count |
| 68 | - * | |
| 69 | 74 | * @return string |
| 70 | 75 | */ |
| 71 | 76 | public function read( $count ) { |
| 72 | 77 | $value = ''; |
| 73 | 78 | |
| 74 | - while ( $count > 0 && $this->pos <= $this->max && ( ( $one = $this->string[ $this->pos ] ) || '0' === $one ) ) { | |
| 75 | - $value .= $one; | |
| 76 | - $this->pos += 1; | |
| 79 | + while ( $count > 0 && ! is_null( $one = $this->read_one() ) ) { | |
| 80 | + $value .= $one; | |
| 77 | 81 | --$count; |
| 78 | 82 | } |
| 79 | 83 | |
| 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; | |
| 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; | |
| 92 | 99 | } |
| 93 | - | |
| 94 | - // Return the middle of the string, from the second character to the second-but-last. | |
| 95 | - return substr( $value, 1, -1 ); | |
| 100 | + return $value; | |
| 96 | 101 | } |
| 97 | 102 | |
| 98 | 103 | /** |
| 99 | - * 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" | |
| 100 | 108 | * |
| 101 | - * @since 6.9.1 This was added as an optimization. | |
| 102 | - * | |
| 103 | - * @return void | |
| 109 | + * @param string string | |
| 110 | + * @return string | |
| 104 | 111 | */ |
| 105 | - public function skip_next_character() { | |
| 106 | - $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 ); | |
| 107 | 122 | } |
| 108 | 123 | } |