| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Tokens; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 8 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 9 |
use Wikimedia\Parsoid\Utils\PHPUtils; |
| 10 |
|
| 11 |
/** |
| 12 |
* Represents a source offset range. |
| 13 |
*/ |
| 14 |
class SourceRange implements JsonCodecable { |
| 15 |
use JsonCodecableTrait; |
| 16 |
|
| 17 |
/** |
| 18 |
* Offset of the first character (range start is inclusive). |
| 19 |
* @var ?int |
| 20 |
*/ |
| 21 |
public $start; |
| 22 |
|
| 23 |
/** |
| 24 |
* Offset just past the last character (range end is exclusive). |
| 25 |
* @var ?int |
| 26 |
*/ |
| 27 |
public $end; |
| 28 |
|
| 29 |
/** |
| 30 |
* Create a new source offset range. |
| 31 |
* @param ?int $start The starting index (UTF-8 byte count, inclusive) |
| 32 |
* @param ?int $end The ending index (UTF-8 byte count, exclusive) |
| 33 |
*/ |
| 34 |
public function __construct( ?int $start, ?int $end ) { |
| 35 |
$this->start = $start; |
| 36 |
$this->end = $end; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Return a KVSourceRange where this SourceRange is the key, |
| 41 |
* and the value has zero length. |
| 42 |
* @return KVSourceRange |
| 43 |
*/ |
| 44 |
public function expandTsrK(): KVSourceRange { |
| 45 |
return new KVSourceRange( |
| 46 |
$this->start, $this->end, $this->end, $this->end |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Return a KVSourceRange where this SourceRange is the value, |
| 52 |
* and the key has zero length. |
| 53 |
* @return KVSourceRange |
| 54 |
*/ |
| 55 |
public function expandTsrV(): KVSourceRange { |
| 56 |
return new KVSourceRange( |
| 57 |
$this->start, $this->start, $this->start, $this->end |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Return a KVSourceRange by using this SourceRange for the key |
| 63 |
* and the given SourceRange parameter for the value. |
| 64 |
* @param SourceRange $value |
| 65 |
* @return KVSourceRange |
| 66 |
*/ |
| 67 |
public function join( SourceRange $value ): KVSourceRange { |
| 68 |
return new KVSourceRange( |
| 69 |
$this->start, $this->end, $value->start, $value->end |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Return the substring of the given string corresponding to this |
| 75 |
* range. |
| 76 |
* @param string $str The source text string |
| 77 |
* @return string |
| 78 |
*/ |
| 79 |
public function substr( string $str ): string { |
| 80 |
$start = $this->start; |
| 81 |
$length = $this->length(); |
| 82 |
Assert::invariant( ( $start ?? -1 ) >= 0, "Bad SourceRange start" ); |
| 83 |
// @phan-suppress-next-line PhanCoalescingNeverNull |
| 84 |
Assert::invariant( ( $length ?? -1 ) >= 0, "Bad SourceRange length" ); |
| 85 |
return PHPUtils::safeSubstr( $str, $start, $length ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Return a new source range shifted by $amount. |
| 90 |
* @param int $amount The amount to shift by |
| 91 |
* @return SourceRange |
| 92 |
*/ |
| 93 |
public function offset( int $amount ): SourceRange { |
| 94 |
return new SourceRange( $this->start + $amount, $this->end + $amount ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Return a range from the end of this range to the start of the given |
| 99 |
* range. |
| 100 |
* @param SourceRange $sr |
| 101 |
* @return SourceRange |
| 102 |
*/ |
| 103 |
public function to( SourceRange $sr ): SourceRange { |
| 104 |
return new SourceRange( $this->end, $sr->start ); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Return the length of this source range. |
| 109 |
* @return int |
| 110 |
*/ |
| 111 |
public function length(): int { |
| 112 |
return $this->end - $this->start; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Create a new source offset range from an array of |
| 117 |
* integers (such as created during JSON serialization). |
| 118 |
* @param int[] $sr |
| 119 |
* @return SourceRange |
| 120 |
* @deprecated |
| 121 |
*/ |
| 122 |
public static function fromArray( array $sr ): SourceRange { |
| 123 |
// Dynamic dispatch (DomSourceRange subclasses this) |
| 124 |
return static::newFromJsonArray( $sr ); |
| 125 |
} |
| 126 |
|
| 127 |
/** @inheritDoc */ |
| 128 |
public function toJsonArray(): array { |
| 129 |
return [ $this->start, $this->end ]; |
| 130 |
} |
| 131 |
|
| 132 |
/** @inheritDoc */ |
| 133 |
public static function newFromJsonArray( array $json ): SourceRange { |
| 134 |
Assert::invariant( |
| 135 |
count( $json ) === 2, |
| 136 |
'Wrong # of elements in SourceRange array' |
| 137 |
); |
| 138 |
return new SourceRange( $json[0], $json[1] ); |
| 139 |
} |
| 140 |
} |