Skip to content

Commit

Permalink
Add upper/lower fallbacks using PCRE (only for ASCII letters)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWeinert committed Apr 5, 2018
1 parent 00cdef7 commit 29d907d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/system/Papaya/Util/String/Utf8.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ public static function toLowerCase($string) {
case self::EXT_MBSTRING :
return mb_strtolower($string, 'utf-8');
}
return $string;
return preg_replace_callback(
'([A-Z]+)u',
function($match) {
return strtolower($match[0]);
},
$string
);
}

public static function toUpperCase($string) {
Expand All @@ -240,7 +246,13 @@ public static function toUpperCase($string) {
case self::EXT_MBSTRING :
return mb_strtoupper($string, 'utf-8');
}
return $string;
return preg_replace_callback(
'([a-z]+)u',
function($match) {
return strtoupper($match[0]);
},
$string
);
}

/**
Expand Down Expand Up @@ -274,4 +286,4 @@ public static function getExtension() {
public static function setExtension($extension) {
self::$extension = (int)$extension;
}
}
}
28 changes: 27 additions & 1 deletion tests/system/Papaya/Util/String/Utf8Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,32 @@ public function testCopyUsingPcreFallback($expected, $haystack, $start, $length
}

/**
* @param string $expected
* @param string $input
* @covers PapayaUtilStringUtf8::toLowerCase
* @testWith
* ["abc", "ABC"]
* ["abcÄdef", "ABCÄDEF"]
*/
public function testLowerCaseUsingPcreFallback($expected, $input) {
PapayaUtilStringUtf8::setExtension(PapayaUtilStringUtf8::EXT_PCRE);
$this->assertEquals($expected, PapayaUtilStringUtf8::toLowerCase($input));
}

/**
* @param string $expected
* @param string $input
* @covers PapayaUtilStringUtf8::toUpperCase
* @testWith
* ["ABC", "abc"]
* ["ABCäDEF", "abcädef"]
*/
public function testUpperCaseUsingPcreFallback($expected, $input) {
PapayaUtilStringUtf8::setExtension(PapayaUtilStringUtf8::EXT_PCRE);
$this->assertEquals($expected, PapayaUtilStringUtf8::toUpperCase($input));
}

/**
* @covers PapayaUtilStringUtf8::getExtension
*/
public function testGetExtension() {
Expand Down Expand Up @@ -205,4 +231,4 @@ public static function provideCopySamples() {
'ascii 10, 10' => array('', 'ascii', 10, 10)
);
}
}
}

0 comments on commit 29d907d

Please sign in to comment.