Issue #70719 by neclimdul, superspring, longwave, kscheirer, InternetDevels: Add Unicode::ucwords() and Unicode::lcfirst() implementation and make use of them in core.

8.0.x
Alex Pott 2014-04-05 06:38:16 +01:00
parent cdcbb9d516
commit f130cc3e7b
3 changed files with 104 additions and 11 deletions

View File

@ -260,7 +260,7 @@ EOD;
}
/**
* Uppercase a UTF-8 string.
* Converts a UTF-8 string to uppercase.
*
* @param string $text
* The string to run the operation on.
@ -282,7 +282,7 @@ EOD;
}
/**
* Lowercase a UTF-8 string.
* Converts a UTF-8 string to lowercase.
*
* @param string $text
* The string to run the operation on.
@ -304,18 +304,52 @@ EOD;
}
/**
* Capitalizes the first letter of a UTF-8 string.
* Capitalizes the first character of a UTF-8 string.
*
* @param string $text
* The string to convert.
*
* @return string
* The string with the first letter as uppercase.
* The string with the first character as uppercase.
*/
public static function ucfirst($text) {
return static::strtoupper(static::substr($text, 0, 1)) . static::substr($text, 1);
}
/**
* Converts the first character of a UTF-8 string to lowercase.
*
* @param string $text
* The string that will be converted.
*
* @return string
* The string with the first character as lowercase.
*
* @ingroup php_wrappers
*/
public static function lcfirst($text) {
// Note: no mbstring equivalent!
return static::strtolower(static::substr($text, 0, 1)) . static::substr($text, 1);
}
/**
* Capitalizes the first character of each word in a UTF-8 string.
*
* @param string $text
* The text that will be converted.
*
* @return string
* The input $text with each word capitalized.
*
* @ingroup php_wrappers
*/
public static function ucwords($text) {
$regex = '/(^|[' . static::PREG_CLASS_WORD_BOUNDARY . '])([^' . static::PREG_CLASS_WORD_BOUNDARY . '])/u';
return preg_replace_callback($regex, function(array $matches) {
return $matches[1] . Unicode::strtoupper($matches[2]);
}, $text);
}
/**
* Cuts off a piece of a string based on character indices and counts.
*

View File

@ -264,14 +264,9 @@ abstract class HandlerBase extends PluginBase {
case 'lower':
return drupal_strtolower($string);
case 'ucfirst':
return drupal_strtoupper(drupal_substr($string, 0, 1)) . drupal_substr($string, 1);
return Unicode::ucfirst($string);
case 'ucwords':
if (Unicode::getStatus() == Unicode::STATUS_MULTIBYTE) {
return mb_convert_case($string, MB_CASE_TITLE);
}
else {
return ucwords($string);
}
return Unicode::ucwords($string);
}
}

View File

@ -193,6 +193,70 @@ class UnicodeTest extends UnitTestCase {
);
}
/**
* Tests Unicode::lcfirst().
*
* @dataProvider providerLcfirst
*/
public function testLcfirst($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
Unicode::setStatus($status);
$this->assertEquals($expected, Unicode::lcfirst($text));
}
/**
* Data provider for testLcfirst().
*
* @see testLcfirst()
*
* @return array
* An array containing a string, its lowercase version and whether it should
* be processed as multibyte.
*/
public function providerLcfirst() {
return array(
array('tHe QUIcK bRoWn', 'tHe QUIcK bRoWn'),
array('FrançAIS is ÜBER-åwesome', 'françAIS is ÜBER-åwesome'),
array('Über', 'über'),
array('Åwesome', 'åwesome'),
// Add a multibyte string.
array('ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', 'αΒΓΔΕΖΗΘΙΚΛΜΝΞΟΣὨ', TRUE),
);
}
/**
* Tests Unicode::ucwords().
*
* @dataProvider providerUcwords
*/
public function testUcwords($text, $expected, $multibyte = FALSE) {
$status = $multibyte ? Unicode::STATUS_MULTIBYTE : Unicode::STATUS_SINGLEBYTE;
Unicode::setStatus($status);
$this->assertEquals($expected, Unicode::ucwords($text));
}
/**
* Data provider for testUcwords().
*
* @see testUcwords()
*
* @return array
* An array containing a string, its capitalized version and whether it should
* be processed as multibyte.
*/
public function providerUcwords() {
return array(
array('tHe QUIcK bRoWn', 'THe QUIcK BRoWn'),
array('françAIS', 'FrançAIS'),
array('über', 'Über'),
array('åwesome', 'Åwesome'),
// Make sure we don't mangle extra spaces.
array('frànçAIS is über-åwesome', 'FrànçAIS Is Über-Åwesome'),
// Add a multibyte string.
array('σion', 'Σion', TRUE),
);
}
/**
* Tests Unicode::strlen().
*