Merge branch 'MDL-73433-master' of https://github.com/marinaglancy/moodle

This commit is contained in:
Jun Pataleta
2022-10-04 17:53:27 +08:00
2 changed files with 85 additions and 12 deletions
+23 -12
View File
@@ -94,7 +94,7 @@ class core_text {
* @return string normalised lowercase charset name
*/
public static function parse_charset($charset) {
$charset = strtolower($charset);
$charset = strtolower($charset ?? '');
if ($charset === 'utf8' or $charset === 'utf-8') {
return 'utf-8';
@@ -181,9 +181,9 @@ class core_text {
// Check whether the charset is supported by mbstring. CP1250 is not supported. Fall back to iconv.
if (self::is_charset_supported($charset)) {
$result = mb_substr($text, $start, $len, $charset);
$result = mb_substr($text ?? '', $start, $len, $charset);
} else {
$result = iconv_substr($text, $start, $len, $charset);
$result = (string)iconv_substr($text ?? '', $start, $len, $charset);
}
return $result;
@@ -199,7 +199,7 @@ class core_text {
* @since Moodle 3.1
*/
public static function str_max_bytes($string, $bytes) {
return mb_strcut($string, 0, $bytes, 'UTF-8');
return mb_strcut($string ?? '', 0, $bytes, 'UTF-8');
}
/**
@@ -213,6 +213,10 @@ class core_text {
* @since Moodle 2.4.6, 2.5.2, 2.6
*/
public static function strrchr($haystack, $needle, $part = false) {
if (is_null($haystack)) {
// Compatibility with behavior in PHP before version 8.1.
return false;
}
return mb_strrchr($haystack, $needle, $part, 'UTF-8');
}
@@ -227,10 +231,10 @@ class core_text {
$charset = self::parse_charset($charset);
if (self::is_charset_supported($charset)) {
return mb_strlen($text, $charset);
return mb_strlen($text ?? '', $charset);
}
return iconv_strlen($text, $charset);
return iconv_strlen($text ?? '', $charset);
}
/**
@@ -245,7 +249,7 @@ class core_text {
// Confirm mbstring can handle the charset.
if (self::is_charset_supported($charset)) {
return mb_strtolower($text, $charset);
return mb_strtolower($text ?? '', $charset);
}
// The mbstring extension cannot handle the charset. Convert to UTF-8.
@@ -267,7 +271,7 @@ class core_text {
// Confirm mbstring can handle the charset.
if (self::is_charset_supported($charset)) {
return mb_strtoupper($text, $charset);
return mb_strtoupper($text ?? '', $charset);
}
// The mbstring extension cannot handle the charset. Convert to UTF-8.
@@ -287,7 +291,7 @@ class core_text {
* @return int the numeric position of the first occurrence of needle in haystack.
*/
public static function strpos($haystack, $needle, $offset=0) {
return mb_strpos($haystack, $needle, $offset, 'UTF-8');
return mb_strpos($haystack ?? '', $needle, $offset, 'UTF-8');
}
/**
@@ -299,7 +303,11 @@ class core_text {
* @return int the numeric position of the last occurrence of needle in haystack
*/
public static function strrpos($haystack, $needle) {
return mb_strrpos($haystack, $needle, null, 'UTF-8');
if (is_null($haystack)) {
// Compatibility with behavior in PHP before version 8.1.
return false;
}
return mb_strrpos($haystack, $needle, 0, 'UTF-8');
}
/**
@@ -310,7 +318,7 @@ class core_text {
* @return string the reversed multi byte string
*/
public static function strrev($str) {
preg_match_all('/./us', $str, $ar);
preg_match_all('/./us', $str ?? '', $ar);
return join('', array_reverse($ar[0]));
}
@@ -513,7 +521,7 @@ class core_text {
$str = self::entities_to_utf8($str, true);
}
$result = mb_strtolower(mb_encode_numericentity($str, [0xa0, 0xffff, 0, 0xffff], 'UTF-8', true));
$result = mb_strtolower(mb_encode_numericentity($str ?? '', [0xa0, 0xffff, 0, 0xffff], 'UTF-8', true));
// We cannot use the decimal equivalent of the above call due to the unit test and our allowance for
// entities to be entered within the provided $str. Refer to the correspond unit test for examples.
@@ -536,6 +544,9 @@ class core_text {
* @return string
*/
public static function trim_utf8_bom($str) {
if (is_null($str)) {
return null;
}
$bom = self::UTF8_BOM;
if (strpos($str, $bom) === 0) {
return substr($str, strlen($bom));
+62
View File
@@ -106,6 +106,9 @@ class text_test extends advanced_testcase {
$utf8 = "A æ Übérmensch på høyeste nivå! И я люблю PHP! есть. アクセシビリティ. fi";
$this->assertSame("A ae Ubermensch pa hoyeste niva! I a lublu PHP! est'. akuseshibiriti. fi",
core_text::convert($utf8, 'utf-8', 'ascii'));
// Check that null argument is allowed.
$this->assertSame('', core_text::convert(null, 'utf-8', 'ascii'));
}
/**
@@ -150,6 +153,12 @@ class text_test extends advanced_testcase {
$str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
$s = pack("H*", "cce5"); // GB18030
$this->assertSame($s, core_text::substr($str, 1, 1, 'GB18030'));
// Check that null argument is allowed.
$this->assertSame('', core_text::substr(null, 1, 1));
$this->assertSame('', core_text::substr(null, 1));
$this->assertSame('', core_text::substr(null, 1, 1, 'cp1250'));
$this->assertSame('', core_text::substr(null, 1, null, 'cp1250'));
}
/**
@@ -191,6 +200,10 @@ class text_test extends advanced_testcase {
$this->assertSame(1, core_text::strlen($str, 'GB18030'));
$str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
$this->assertSame(4, core_text::strlen($str, 'GB18030'));
// Check that null argument is allowed.
$this->assertSame(0, core_text::strlen(null));
$this->assertSame(0, core_text::strlen(null, 'cp1250'));
}
/**
@@ -243,6 +256,9 @@ class text_test extends advanced_testcase {
$conv = core_text::str_max_bytes($str, 0);
$this->assertEquals(0, strlen($conv));
$this->assertSame('', $conv);
// Check that null argument is allowed.
$this->assertSame('', core_text::str_max_bytes(null, 1));
}
/**
@@ -281,6 +297,10 @@ class text_test extends advanced_testcase {
$str = 1309528800;
$this->assertSame((string)$str, core_text::strtolower($str));
// Check that null argument is allowed.
$this->assertSame('', core_text::strtolower(null));
$this->assertSame('', core_text::strtolower(null, 'cp1250'));
}
/**
@@ -316,6 +336,10 @@ class text_test extends advanced_testcase {
$str = pack("H*", "bcf2cce5d6d0cec4"); // GB18030
$this->assertSame($str, core_text::strtoupper($str, 'GB18030'));
// Check that null argument is allowed.
$this->assertSame('', core_text::strtoupper(null));
$this->assertSame('', core_text::strtoupper(null, 'cp1250'));
}
/**
@@ -338,6 +362,9 @@ class text_test extends advanced_testcase {
// Reverse it twice to be doubly sure.
$this->assertSame($after, core_text::strrev(core_text::strrev($after)));
}
// Check that null argument is allowed.
$this->assertSame('', core_text::strrev(null));
}
/**
@@ -348,6 +375,9 @@ class text_test extends advanced_testcase {
public function test_strpos() {
$str = "Žluťoučký koníček";
$this->assertSame(10, core_text::strpos($str, 'koníč'));
// Check that null argument is allowed.
$this->assertSame(false, core_text::strpos(null, 'a'));
}
/**
@@ -358,6 +388,9 @@ class text_test extends advanced_testcase {
public function test_strrpos() {
$str = "Žluťoučký koníček";
$this->assertSame(11, core_text::strrpos($str, 'o'));
// Check that null argument is allowed.
$this->assertSame(false, core_text::strrpos(null, 'o'));
}
/**
@@ -382,6 +415,10 @@ class text_test extends advanced_testcase {
$utf8 = 'キャンパス Αλφαβητικός Κατάλογος Лорем ипсум долор сит амет';
$this->assertSame('kyanpasu Alphabetikos Katalogos Lorem ipsum dolor sit amet', core_text::specialtoascii($utf8));
// Check that null argument is allowed.
$this->assertSame('', core_text::specialtoascii(null));
$this->assertSame('', core_text::specialtoascii(null, 'ascii'));
}
/**
@@ -423,6 +460,9 @@ class text_test extends advanced_testcase {
=?utf-8?B?0Ywg0LLQuNC90LTQvtGD0Lci?=";
$this->assertSame($encodedlongstr, $mailer->encodeHeader($longstr));
$this->assertSame('"' . $encodedlongstr . '"', $mailer->encodeHeader($longstr, 'phrase'));
// Check that null argument is allowed.
$this->assertSame('', core_text::encode_mimeheader(null));
}
/**
@@ -433,6 +473,9 @@ class text_test extends advanced_testcase {
public function test_entities_to_utf8() {
$str = "Žluťoučký koníček©"&<>§«";
$this->assertSame("Žluťoučký koníček©\"&<>§«", core_text::entities_to_utf8($str));
// Check that null argument is allowed.
$this->assertSame('', core_text::entities_to_utf8(null));
}
/**
@@ -448,6 +491,10 @@ class text_test extends advanced_testcase {
$str = "&#381;luťoučký kon&iacute;ček&copy;&quot;&amp;&lt;&gt;&sect;&laquo;";
$this->assertSame("&#x17d;lu&#x165;ou&#x10d;k&#xfd; kon&#xed;&#x10d;ek&#xa9;\"&<>&#xa7;&#xab;", core_text::utf8_to_entities($str, false, true));
$this->assertSame("&#381;lu&#357;ou&#269;k&#253; kon&#237;&#269;ek&#169;\"&<>&#167;&#171;", core_text::utf8_to_entities($str, true, true));
// Check that null argument is allowed.
$this->assertSame('', core_text::utf8_to_entities(null));
$this->assertSame('', core_text::utf8_to_entities(null, true));
}
/**
@@ -459,6 +506,9 @@ class text_test extends advanced_testcase {
$bom = "\xef\xbb\xbf";
$str = "Žluťoučký koníček";
$this->assertSame($str.$bom, core_text::trim_utf8_bom($bom.$str.$bom));
// Check that null argument is allowed.
$this->assertSame(null, core_text::trim_utf8_bom(null));
}
/**
@@ -482,6 +532,9 @@ class text_test extends advanced_testcase {
// If you only have a non-character, you get empty string.
$example = html_entity_decode('&#xfffe;');
$this->assertSame('', core_text::remove_unicode_non_characters($example));
// Check that null argument is allowed.
$this->assertSame(null, core_text::trim_utf8_bom(null));
}
/**
@@ -517,6 +570,9 @@ class text_test extends advanced_testcase {
$this->assertSame(0x0439, core_text::utf8ord('й'));
$this->assertSame(0x2FA1F, core_text::utf8ord('𯨟'));
$this->assertSame(381, core_text::utf8ord('Ž'));
// Check that null argument is allowed.
$this->assertSame(ord(''), core_text::utf8ord(null));
}
/**
@@ -527,6 +583,9 @@ class text_test extends advanced_testcase {
public function test_strtotitle() {
$str = "žluťoučký koníček";
$this->assertSame("Žluťoučký Koníček", core_text::strtotitle($str));
// Check that null argument is allowed.
$this->assertSame(null, core_text::strtotitle(null));
}
/**
@@ -540,6 +599,9 @@ class text_test extends advanced_testcase {
$this->assertSame('Žluťoučký ', core_text::strrchr($str, 'koní', true));
$this->assertFalse(core_text::strrchr($str, 'A'));
$this->assertFalse(core_text::strrchr($str, 'ç', true));
// Check that null argument is allowed.
$this->assertSame(false, core_text::strrchr(null, 'o'));
}
/**