mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-06 01:51:51 +00:00
TextFormat: properly handle pcre errors in some cases
these would previously just hit type errors.
This commit is contained in:
parent
2622c34542
commit
41d7b8c0e4
@ -66,13 +66,19 @@ abstract class TextFormat{
|
|||||||
public const ITALIC = TextFormat::ESCAPE . "o";
|
public const ITALIC = TextFormat::ESCAPE . "o";
|
||||||
public const RESET = TextFormat::ESCAPE . "r";
|
public const RESET = TextFormat::ESCAPE . "r";
|
||||||
|
|
||||||
|
private static function makePcreError(string $info) : \InvalidArgumentException{
|
||||||
|
throw new \InvalidArgumentException("$info: Encountered PCRE error " . preg_last_error() . " during regex operation");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Splits the string by Format tokens
|
* Splits the string by Format tokens
|
||||||
*
|
*
|
||||||
* @return string[]
|
* @return string[]
|
||||||
*/
|
*/
|
||||||
public static function tokenize(string $string) : array{
|
public static function tokenize(string $string) : array{
|
||||||
return preg_split("/(" . TextFormat::ESCAPE . "[0-9a-fk-or])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
$result = preg_split("/(" . TextFormat::ESCAPE . "[0-9a-fk-or])/u", $string, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
|
||||||
|
if($result === false) throw self::makePcreError("Failed to tokenize string");
|
||||||
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,6 +89,7 @@ abstract class TextFormat{
|
|||||||
public static function clean(string $string, bool $removeFormat = true) : string{
|
public static function clean(string $string, bool $removeFormat = true) : string{
|
||||||
$string = mb_scrub($string, 'UTF-8');
|
$string = mb_scrub($string, 'UTF-8');
|
||||||
$string = preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
|
$string = preg_replace("/[\x{E000}-\x{F8FF}]/u", "", $string); //remove unicode private-use-area characters (they might break the console)
|
||||||
|
if($string === null) throw self::makePcreError("Failed to strip private-area characters");
|
||||||
if($removeFormat){
|
if($removeFormat){
|
||||||
$string = str_replace(TextFormat::ESCAPE, "", preg_replace("/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "", $string));
|
$string = str_replace(TextFormat::ESCAPE, "", preg_replace("/" . TextFormat::ESCAPE . "[0-9a-fk-or]/u", "", $string));
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user