Skip to content

Commit

Permalink
Move mbstring compat stuff out into vendor
Browse files Browse the repository at this point in the history
Let's trust more symfony than our half baked 15 lines function.
  • Loading branch information
ozh committed Sep 7, 2017
1 parent a2acd65 commit 373e53e
Show file tree
Hide file tree
Showing 12 changed files with 3,082 additions and 107 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
"pomo/pomo" : "1.3.0",
"geoip2/geoip2" : "2.5.0",
"aura/sql": "~2.",
"jakeasmith/http_build_url": "1.0.1"
"jakeasmith/http_build_url": "1.0.1",
"symfony/polyfill-mbstring" : "1.5.0"
},
"config": {
"vendor-dir": "includes/vendor"
},
"autoload": {
"psr-4": {"YOURLS\\": "includes/YOURLS"}
},
"suggest": {
"ext-mbstring": "For best performance",
"ext-curl": "For best performance",
"ext-lulz": "For the lulz"
}
}
61 changes: 60 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 0 additions & 105 deletions includes/functions-compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,109 +107,4 @@ function bcpow( $base, $power ) {
}
}

/**
* mb_substr compatibility function. Stolen from WP
*
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
* The behavior of this function for invalid inputs is undefined.
*
* @since 1.7.1
*/
if ( ! function_exists( 'mb_substr' ) ) :
function mb_substr( $str, $start, $length = null, $encoding = null ) {
return yourls_mb_substr( $str, $start, $length, $encoding );
}
endif;
function yourls_mb_substr( $str, $start, $length = null, $encoding = null ) {
if ( null === $encoding ) {
$encoding = 'UTF-8';
}
// The solution below works only for UTF-8,
// so in case of a different charset just use built-in substr()
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
}
if ( yourls_supports_pcre_u() ) {
// Use the regex unicode support to separate the UTF-8 characters into an array
preg_match_all( '/./us', $str, $match );
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
return implode( '', $chars );
}
$regex = '/(
[\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xE1-\xEC][\x80-\xBF]{2}
| \xED[\x80-\x9F][\x80-\xBF]
| [\xEE-\xEF][\x80-\xBF]{2}
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
| [\xF1-\xF3][\x80-\xBF]{3}
| \xF4[\x80-\x8F][\x80-\xBF]{2}
)/x';
$chars = array( '' ); // Start with 1 element instead of 0 since the first thing we do is pop
do {
// We had some string left over from the last round, but we counted it in that last round.
array_pop( $chars );
// Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
$chars = array_merge( $chars, $pieces );
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
return join( '', array_slice( $chars, $start, $length ) );
}

/**
* mb_strlen compatibility function. Stolen from WP
*
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
* The behavior of this function for invalid inputs is undefined.
*
* @since 1.7.1
*/
if ( ! function_exists( 'mb_strlen' ) ) :
function mb_strlen( $str, $encoding = null ) {
return yourls_mb_strlen( $str, $encoding );
}
endif;
function yourls_mb_strlen( $str, $encoding = null ) {
if ( null === $encoding ) {
$encoding = 'UTF-8';
}
// The solution below works only for UTF-8,
// so in case of a different charset just use built-in strlen()
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
return strlen( $str );
}
if ( yourls_supports_pcre_u() ) {
// Use the regex unicode support to separate the UTF-8 characters into an array
preg_match_all( '/./us', $str, $match );
return count( $match[0] );
}
$regex = '/(?:
[\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xE1-\xEC][\x80-\xBF]{2}
| \xED[\x80-\x9F][\x80-\xBF]
| [\xEE-\xEF][\x80-\xBF]{2}
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
| [\xF1-\xF3][\x80-\xBF]{3}
| \xF4[\x80-\x8F][\x80-\xBF]{2}
)/x';
$count = 1; // Start at 1 instead of 0 since the first thing we do is decrement
do {
// We had some string left over from the last round, but we counted it in that last round.
$count--;
// Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
$pieces = preg_split( $regex, $str, 1000 );
// Increment
$count += count( $pieces );
} while ( $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
// Fencepost: preg_split() always returns one extra item in the array
return --$count;
}

// @codeCoverageIgnoreEnd


1 change: 1 addition & 0 deletions includes/vendor/composer/autoload_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@

return array(
'b45b351e6b6f7487d819961fef2fda77' => $vendorDir . '/jakeasmith/http_build_url/src/http_build_url.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => $vendorDir . '/symfony/polyfill-mbstring/bootstrap.php',
);
1 change: 1 addition & 0 deletions includes/vendor/composer/autoload_psr4.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

return array(
'YOURLS\\' => array($baseDir . '/includes/YOURLS'),
'Symfony\\Polyfill\\Mbstring\\' => array($vendorDir . '/symfony/polyfill-mbstring'),
'POMO\\' => array($vendorDir . '/pomo/pomo/src'),
'MaxMind\\WebService\\' => array($vendorDir . '/maxmind/web-service-common/src/WebService'),
'MaxMind\\Exception\\' => array($vendorDir . '/maxmind/web-service-common/src/Exception'),
Expand Down
9 changes: 9 additions & 0 deletions includes/vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ class ComposerStaticInit2d6d15a8f6cc4bfbfd4a2943a6c4df59
{
public static $files = array (
'b45b351e6b6f7487d819961fef2fda77' => __DIR__ . '/..' . '/jakeasmith/http_build_url/src/http_build_url.php',
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
);

public static $prefixLengthsPsr4 = array (
'Y' =>
array (
'YOURLS\\' => 7,
),
'S' =>
array (
'Symfony\\Polyfill\\Mbstring\\' => 26,
),
'P' =>
array (
'POMO\\' => 5,
Expand Down Expand Up @@ -44,6 +49,10 @@ class ComposerStaticInit2d6d15a8f6cc4bfbfd4a2943a6c4df59
array (
0 => __DIR__ . '/../../..' . '/includes/YOURLS',
),
'Symfony\\Polyfill\\Mbstring\\' =>
array (
0 => __DIR__ . '/..' . '/symfony/polyfill-mbstring',
),
'POMO\\' =>
array (
0 => __DIR__ . '/..' . '/pomo/pomo/src',
Expand Down
61 changes: 61 additions & 0 deletions includes/vendor/composer/installed.json
Original file line number Diff line number Diff line change
Expand Up @@ -505,5 +505,66 @@
}
],
"description": "Provides functionality for http_build_url() to environments without pecl_http."
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.5.0",
"version_normalized": "1.5.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/7c8fae0ac1d216eb54349e6a8baa57d515fe8803",
"reference": "7c8fae0ac1d216eb54349e6a8baa57d515fe8803",
"shasum": ""
},
"require": {
"php": ">=5.3.3"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"time": "2017-06-14T15:44:48+00:00",
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.5-dev"
}
},
"installation-source": "dist",
"autoload": {
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
},
"files": [
"bootstrap.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "[email protected]"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
]
}
]
Loading

0 comments on commit 373e53e

Please sign in to comment.