| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Given a string, this class will read through the string using |
| 9 |
* one of a number of terminating rules: |
| 10 |
* - One character. |
| 11 |
* - A specified number of characters. |
| 12 |
* - Until a matching character is found. |
| 13 |
* |
| 14 |
* @since 6.2 |
| 15 |
*/ |
| 16 |
class FrmStringReaderHelper { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
private $pos = 0; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var int |
| 25 |
*/ |
| 26 |
private $max = 0; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $string; |
| 32 |
|
| 33 |
/** |
| 34 |
* @param string $string |
| 35 |
*/ |
| 36 |
public function __construct( $string ) { |
| 37 |
// Split the string up into an array of UTF-8 characters. |
| 38 |
// As an array we can read through it one character at a time. |
| 39 |
|
| 40 |
$this->string = $string; |
| 41 |
$this->max = strlen( $this->string ) - 1; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Read characters until we reach the given character $char. |
| 46 |
* By default, discard that final matching character and return the rest. |
| 47 |
* |
| 48 |
* @param string $char |
| 49 |
* |
| 50 |
* @return string |
| 51 |
*/ |
| 52 |
public function read_until( $char ) { |
| 53 |
$value = ''; |
| 54 |
|
| 55 |
while ( $this->pos <= $this->max && ( $one = $this->string[ $this->pos++ ] ) !== $char ) { |
| 56 |
$value .= $one; |
| 57 |
} |
| 58 |
return $value; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Read $count characters, or until we have reached the end, |
| 63 |
* whichever comes first. |
| 64 |
* By default, remove enclosing double-quotes from the result. |
| 65 |
* |
| 66 |
* @param int $count |
| 67 |
* |
| 68 |
* @return string |
| 69 |
*/ |
| 70 |
public function read( $count ) { |
| 71 |
$value = ''; |
| 72 |
|
| 73 |
while ( $count > 0 && $this->pos <= $this->max && ( ( $one = $this->string[ $this->pos ] ) || '0' === $one ) ) { |
| 74 |
$value .= $one; |
| 75 |
$this->pos += 1; |
| 76 |
--$count; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Remove a single set of double-quotes from around a string. |
| 81 |
* |
| 82 |
* Examples: |
| 83 |
* abc => abc |
| 84 |
* "abc" => abc |
| 85 |
* ""abc"" => "abc" |
| 86 |
*/ |
| 87 |
if ( strlen( $value ) < 2 || substr( $value, 0, 1 ) !== '"' || substr( $value, -1, 1 ) !== '"' ) { |
| 88 |
// Only remove exactly one quote from the start and the end and then only if there is one at each end. |
| 89 |
// Too short, or does not start or end with a quote. |
| 90 |
return $value; |
| 91 |
} |
| 92 |
|
| 93 |
// Return the middle of the string, from the second character to the second-but-last. |
| 94 |
return substr( $value, 1, -1 ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Shift the position. This is used to skip a character that we don't need to read. |
| 99 |
* |
| 100 |
* @since 6.9.1 This was added as an optimization. |
| 101 |
* |
| 102 |
* @return void |
| 103 |
*/ |
| 104 |
public function skip_next_character() { |
| 105 |
$this->pos += 1; |
| 106 |
} |
| 107 |
} |
| 108 |
|