Skip to content

Commit

Permalink
[jan] Use grapheme functions in Horde_String if intl extension is ava…
Browse files Browse the repository at this point in the history
…ilable.
  • Loading branch information
yunosh committed Jan 15, 2015
1 parent 481c891 commit 1522ef3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
56 changes: 48 additions & 8 deletions framework/Util/lib/Horde/String.php
Expand Up @@ -329,6 +329,24 @@ public static function substr($string, $start, $length = null,
}
}

/* Try intl. */
if (Horde_Util::extensionExists('intl')) {
$ret = self::convertCharset(
@grapheme_substr(
self::convertCharset($string, $charset, 'UTF-8'),
$start,
$length
),
'UTF-8',
$charset
);

/* grapheme_substr() returns false on failure. */
if ($ret !== false) {
return $ret;
}
}

return substr($string, $start, $length);
}

Expand All @@ -355,6 +373,11 @@ public static function length($string, $charset = 'UTF-8')
return $ret;
}
}
if (Horde_Util::extensionExists('intl')) {
return grapheme_strlen(
self::convertCharset($string, $charset, 'UTF-8')
);
}

return strlen($string);
}
Expand All @@ -374,7 +397,7 @@ public static function pos(
$haystack, $needle, $offset = 0, $charset = 'UTF-8'
)
{
return self::_pos($haystack, $needle, $offset, $charset, 'mb_strpos', 'strpos');
return self::_pos($haystack, $needle, $offset, $charset, 'strpos');
}

/**
Expand All @@ -394,7 +417,7 @@ public static function ipos(
$haystack, $needle, $offset = 0, $charset = 'UTF-8'
)
{
return self::_pos($haystack, $needle, $offset, $charset, 'mb_stripos', 'stripos');
return self::_pos($haystack, $needle, $offset, $charset, 'stripos');
}

/**
Expand All @@ -412,7 +435,7 @@ public static function rpos(
$haystack, $needle, $offset = 0, $charset = 'UTF-8'
)
{
return self::_pos($haystack, $needle, $offset, $charset, 'mb_strrpos', 'strrpos');
return self::_pos($haystack, $needle, $offset, $charset, 'strrpos');
}

/**
Expand All @@ -432,7 +455,7 @@ public static function ripos(
$haystack, $needle, $offset = 0, $charset = 'UTF-8'
)
{
return self::_pos($haystack, $needle, $offset, $charset, 'mb_strripos', 'strripos');
return self::_pos($haystack, $needle, $offset, $charset, 'strripos');
}

/**
Expand All @@ -442,19 +465,36 @@ public static function ripos(
* @param string $needle The string to search for.
* @param integer $offset Character in $haystack to start searching at.
* @param string $charset Charset of $needle.
* @param string $mb_func mb_* function to use.
* @param string $func PHP string fallback function to use.
* @param string $func Function to use.
*
* @return integer The position of occurrence.
*
*/
protected static function _pos(
$haystack, $needle, $offset, $charset, $mb_func, $func
$haystack, $needle, $offset, $charset, $func
)
{
if (Horde_Util::extensionExists('mbstring')) {
$track_errors = ini_set('track_errors', 1);
$ret = @$mb_func($haystack, $needle, $offset, self::_mbstringCharset($charset));
$ret = @call_user_func('mb_' . $func, $haystack, $needle, $offset, self::_mbstringCharset($charset));
ini_set('track_errors', $track_errors);
if (!isset($php_errormsg)) {
return $ret;
}
}

if (Horde_Util::extensionExists('intl')) {
$track_errors = ini_set('track_errors', 1);
$ret = self::convertCharset(
@call_user_func(
'grapheme_' . $func,
self::convertCharset($haystack, $charset, 'UTF-8'),
self::convertCharset($needle, $charset, 'UTF-8'),
$offset
),
'UTF-8',
$charset
);
ini_set('track_errors', $track_errors);
if (!isset($php_errormsg)) {
return $ret;
Expand Down
4 changes: 2 additions & 2 deletions framework/Util/package.xml
Expand Up @@ -34,7 +34,7 @@
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Use grapheme functions in Horde_String if intl extension is available.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand Down Expand Up @@ -724,7 +724,7 @@ Converted to package.xml 2.0 for pear.horde.org
<date>2014-12-29</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [jan] Use grapheme functions in Horde_String if intl extension is available.
</notes>
</release>
</changelog>
Expand Down

0 comments on commit 1522ef3

Please sign in to comment.