|
static | counterToBase64 (int $n) |
| Convert a counter to a Base64 encoded string.
|
|
static | jsonEncode ( $o) |
| FIXME: Core has FormatJson::encode that does a more comprehensive job.
|
|
static | jsonDecode (string $str, bool $assoc=true) |
| FIXME: Core has FormatJson::parse that does a more comprehensive job json_decode wrapper function.
|
|
static | makeSet (array $a) |
| Convert array to associative array usable as a read-only Set.
|
|
static | lastItem (array $a) |
| Helper to get last item of the array.
|
|
static | pushArray (array &$dest, array ... $sources) |
| Append an array to an accumulator using the most efficient method available.
|
|
static | safeSubstr (string $s, int $start, ?int $length=null, bool $checkEntireString=false) |
| Return a substring, asserting that it is valid UTF-8.
|
|
static | assertValidUTF8 (string $s) |
| Helper for verifying a valid UTF-8 encoding.
|
|
static | reStrip (string $re, ?string $newDelimiter=null) |
| Helper for joining pieces of regular expressions together.
|
|
static | encodeURIComponent (string $str) |
| JS-compatible encodeURIComponent function FIXME: See T221147 (for a post-port update)
|
|
static | sortArray (&$array) |
| Sort keys in an array, recursively, for better reproducibility.
|
|
static | iterable_to_array (iterable $iterable) |
| Convert an iterable to an array.
|
|
static | unreachable (string $reason="should never happen") |
| Indicate that the code which calls this function is intended to be unreachable.
|
|
static | stripPrefix ( $subject, $prefix) |
| If a string starts with a given prefix, remove the prefix.
|
|
static | stripSuffix ( $subject, $suffix) |
| If a string ends with a given suffix, remove the suffix.
|
|
static Wikimedia\Parsoid\Utils\PHPUtils::safeSubstr |
( |
string | $s, |
|
|
int | $start, |
|
|
?int | $length = null, |
|
|
bool | $checkEntireString = false ) |
|
static |
Return a substring, asserting that it is valid UTF-8.
By default we assume the full string was valid UTF-8, which allows us to look at the first and last bytes to make this check. You can check the entire string if you are feeling paranoid; it will take O(N) time (where N is the length of the substring) but so does the substring operation.
If the substring would start beyond the end of the string or end before the start of the string, then this function will return the empty string (as would JavaScript); note that the native substr
would return false
in this case.
Using this helper instead of native substr
is useful during the PHP port to verify that we don't break up Unicode codepoints by the switch from JavaScript UCS-2 offsets to PHP UTF-8 byte offsets.
- Parameters
-
string | $s | The (sub)string to check |
int | $start | The starting offset (in bytes). If negative, the offset is counted from the end of the string. |
?int | $length | (optional) The maximum length of the returned string. If negative, the end position is counted from the end of the string. |
bool | $checkEntireString | Whether to do a slower verification of the entire string, not just the edges. Defaults to false. |
- Returns
- string The checked substring
static Wikimedia\Parsoid\Utils\PHPUtils::stripPrefix |
( |
| $subject, |
|
|
| $prefix ) |
|
static |
If a string starts with a given prefix, remove the prefix.
Otherwise, return the original string. Like preg_replace( "/^$prefix/", '', $subject ) except about 1.14x faster in the replacement case and 2x faster in the no-op case.
Note: adding type declarations to the parameters adds an overhead of 3%. The benchmark above was without type declarations.
- Parameters
-
string | $subject | |
string | $prefix | |
- Returns
- string