sistemazione composer install
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
$config = (new PhpCsFixer\Config())
|
||||
->setIndent(' ')
|
||||
->setLineEnding("\n")
|
||||
->setCacheFile(__DIR__ . '/.php-cs-fixer.cache')
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
'@PHP71Migration' => true,
|
||||
'@PHP71Migration:risky' => true,
|
||||
'@PHP73Migration' => true,
|
||||
'@PHP74Migration' => true,
|
||||
'@PHP74Migration:risky' => true,
|
||||
'@PHP80Migration' => false,
|
||||
'@PHP80Migration:risky' => true,
|
||||
'@PHP81Migration' => false,
|
||||
'@PhpCsFixer' => true,
|
||||
'@PhpCsFixer:risky' => true,
|
||||
'@PSR12' => true,
|
||||
'@Symfony' => true,
|
||||
'@Symfony:risky' => true,
|
||||
'align_multiline_comment' => true,
|
||||
'array_indentation' => true,
|
||||
'array_syntax' => ['syntax' => 'short'],
|
||||
'combine_consecutive_issets' => true,
|
||||
'combine_consecutive_unsets' => true,
|
||||
'combine_nested_dirname' => true,
|
||||
'comment_to_phpdoc' => true,
|
||||
'compact_nullable_typehint' => true,
|
||||
'concat_space' => ['spacing' => 'one'],
|
||||
'echo_tag_syntax' => ['format' => 'short'],
|
||||
'escape_implicit_backslashes' => false,
|
||||
'fully_qualified_strict_types' => true,
|
||||
'linebreak_after_opening_tag' => true,
|
||||
'list_syntax' => ['syntax' => 'short'],
|
||||
'method_argument_space' => ['on_multiline' => 'ensure_fully_multiline'],
|
||||
'multiline_whitespace_before_semicolons' => ['strategy' => 'new_line_for_chained_calls'],
|
||||
'native_constant_invocation' => ['scope' => 'all'],
|
||||
'native_function_invocation' => ['scope' => 'all', 'include' => ['@compiler_optimized']],
|
||||
'native_function_type_declaration_casing' => true,
|
||||
'no_alternative_syntax' => true,
|
||||
'no_null_property_initialization' => true,
|
||||
'no_superfluous_elseif' => true,
|
||||
'no_unneeded_control_parentheses' => true,
|
||||
'no_useless_concat_operator' => false,
|
||||
'no_useless_else' => true,
|
||||
'no_useless_return' => true,
|
||||
'not_operator_with_space' => false,
|
||||
'not_operator_with_successor_space' => false,
|
||||
'ordered_class_elements' => true,
|
||||
'ordered_imports' => ['sort_algorithm' => 'alpha', 'imports_order' => ['class', 'const', 'function']],
|
||||
'ordered_interfaces' => true,
|
||||
'php_unit_set_up_tear_down_visibility' => true,
|
||||
'php_unit_strict' => true,
|
||||
'php_unit_test_class_requires_covers' => true,
|
||||
'phpdoc_add_missing_param_annotation' => true,
|
||||
'phpdoc_order' => true,
|
||||
'phpdoc_order_by_value' => ['annotations' => ['covers']],
|
||||
'phpdoc_to_comment' => false,
|
||||
'phpdoc_types_order' => true,
|
||||
'pow_to_exponentiation' => true,
|
||||
'random_api_migration' => true,
|
||||
'return_assignment' => false,
|
||||
'simple_to_complex_string_variable' => true,
|
||||
'single_line_comment_style' => true,
|
||||
'single_trait_insert_per_statement' => true,
|
||||
'static_lambda' => false,
|
||||
'strict_comparison' => false,
|
||||
'strict_param' => false,
|
||||
'string_line_ending' => true,
|
||||
'trailing_comma_in_multiline' => ['after_heredoc' => true, 'elements' => ['arrays', 'arguments']],
|
||||
'types_spaces' => ['space_multiple_catch' => 'single'],
|
||||
'yoda_style' => false,
|
||||
])
|
||||
->setFinder(
|
||||
PhpCsFixer\Finder::create()
|
||||
->exclude('libs')
|
||||
->exclude('tests/Fixtures')
|
||||
->exclude('var')
|
||||
->exclude('vendor')
|
||||
->in(__DIR__)
|
||||
)
|
||||
;
|
||||
|
||||
return $config;
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
|
||||
VERSION 1 INIT
|
||||
===============
|
||||
|
||||
Version 1.4 - Add MbString::strToChars()
|
||||
28/05/2020 03:25 1.4.2 allow PHP 8
|
||||
7f8bdde chore: update deps
|
||||
738b66b chore: Composer PHP constrain >=7.1.3
|
||||
04/04/2020 01:10 1.4.1 nits
|
||||
c6b73d9 chore: update deps
|
||||
66fc4ef docs: use markdown grammar badges in readme
|
||||
1396862 chore: $ composer fix
|
||||
9a7a15e chore: update deps
|
||||
feffd7b Add PHP 8.0.0-dev benchmark result
|
||||
2108041 Suppress annoying PhanTypePossiblyInvalidDimOffset errors
|
||||
157c102 Remove unused codes
|
||||
42b320a Update deps
|
||||
b19e65f Update .travis.yml to PHP 7.4 stable
|
||||
21/09/2019 10:17 1.4.0 initial release
|
||||
7a81573 Add MbString::strToChars()
|
||||
|
||||
Version 1.3 - Add MbString::toArraySplit()
|
||||
21/09/2019 10:02 1.3.2 Faster MbString::toArray()
|
||||
75785d5 nits
|
||||
4fe3304 Faster MbString::toArray()
|
||||
d68c92d Update deps
|
||||
0191a10 Update readme to use badges from shields.io
|
||||
715f7b1 Revise docs
|
||||
a509909 Output "Nums of Runs" in the benchmark
|
||||
11/07/2019 09:16 1.3.1 nits
|
||||
e5ddaaf Add benchmark results
|
||||
464c021 Remove wrong demo.php
|
||||
649e809 Update deps
|
||||
24c449a Add .gitattributes
|
||||
37957e2 $ composer fix
|
||||
0bd808f Update deps
|
||||
8d873b7 Update .travis.yml for 7.4snapshot
|
||||
492dd4d Add .editorconfig
|
||||
e554752 Update deps
|
||||
64f5b45 nits
|
||||
48d0a13 Use native namespace
|
||||
68e31aa Update deps
|
||||
22/01/2019 13:47 1.3.0 initial release
|
||||
5bcdcc5 Add MbString::toArraySplit()
|
||||
21dff79 year++
|
||||
8865407 Add tests for PHP 7.3
|
||||
6b89bec Update README
|
||||
9f2cef0 Update deps
|
||||
|
||||
Version 1.2 - init
|
||||
04/09/2018 18:58 1.2.4 php-cs-fixer
|
||||
0020f0d php-cs-fixer: enable native_constant_invocation and native_function_invocation
|
||||
f49dd29 Update deps
|
||||
cb21cf1 Rename CHANGELOG.md -> CHANGELOG
|
||||
cf74396 Update deps
|
||||
4bb0848 Update .travis.yml
|
||||
18/07/2018 08:28 1.2.3 Update deps
|
||||
4dc8711 Add composer.lock
|
||||
73a7dd6 Mark ext-iconv as a requirement
|
||||
04/07/2018 15:28 1.2.2 nits
|
||||
8de1ef9 Update .rmt.yml
|
||||
04/07/2018 14:57 1.2.1 initial release
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018-2023 Jack Cherng <jfcherng@gmail.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
# php-mb-string
|
||||
|
||||
[](https://github.com/jfcherng/php-mb-string/actions)
|
||||
[](https://packagist.org/packages/jfcherng/php-mb-string)
|
||||
[](https://packagist.org/packages/jfcherng/php-mb-string)
|
||||
[](https://github.com/jfcherng/php-mb-string/blob/master/LICENSE)
|
||||
[](https://github.com/jfcherng/php-mb-string/stargazers)
|
||||
[](https://www.paypal.me/jfcherng/5usd)
|
||||
|
||||
A high performance multibyte sting implementation for frequently reading/writing operations.
|
||||
|
||||
## Why I Write This Package?
|
||||
|
||||
Consider that you have a **LONG** multibyte string and
|
||||
you want to do lots of following operations on it.
|
||||
|
||||
- Random reading/writing such as `$char = $str[5];` or `$str[5] = '許';`.
|
||||
- Replacement such as `str_replace($search, $replace, $str);`.
|
||||
- Insertion such as `substr_replace($insert, $str, $position, 0);`.
|
||||
- Get substring such as `substr($str, $start, $length);`.
|
||||
|
||||
Because strings in PHP are not UTF-8, to do operations above safely,
|
||||
you have to either use `mb_*()` functions or calculate the index by yourself.
|
||||
Using `mb_*()` functions frequently can be a performance loss because it has
|
||||
to re-decode the source string basing on the given encoding every time when you call it.
|
||||
The longer the string is, the severer the problem becomes.
|
||||
|
||||
Instead, this class internally stores the string in its UTF-32 form,
|
||||
which is fixed-width (1 char always occupies 4 bytes) so we are able to
|
||||
perform speedy random accesses. With the power of random access, we could
|
||||
use `str_*()` functions to do the job internally.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
composer require jfcherng/php-mb-string
|
||||
```
|
||||
|
||||
## Example
|
||||
|
||||
See [tests/MbStringTest.php](https://github.com/jfcherng/php-mb-string/blob/master/tests/MbStringTest.php).
|
||||
|
||||
## Benchmark
|
||||
|
||||
See [benchmark/\_results.txt](https://github.com/jfcherng/php-mb-string/blob/master/benchmark/_results.txt).
|
||||
|
||||
## What Are You Doing With This Package?
|
||||
|
||||
I develop this for a PHP diff package, [jfcherng/php-diff](https://github.com/jfcherng/php-diff).
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
{
|
||||
"name": "jfcherng/php-mb-string",
|
||||
"description": "A high performance multibytes sting implementation for frequently reading/writing operations.",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Jack Cherng",
|
||||
"email": "jfcherng@gmail.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "beta",
|
||||
"prefer-stable": true,
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Jfcherng\\Utility\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Jfcherng\\Utility\\Test\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"platform": {
|
||||
"php": "8.1.99"
|
||||
},
|
||||
"sort-packages": true
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"ext-iconv": "*"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3",
|
||||
"phan/phan": "^5",
|
||||
"phpunit/phpunit": "^9 || ^10"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-iconv": "Either \"ext-iconv\" or \"ext-mbstring\" is requried.",
|
||||
"ext-mbstring": "Either \"ext-iconv\" or \"ext-mbstring\" is requried."
|
||||
},
|
||||
"scripts": {
|
||||
"analyze": [
|
||||
"phan --color"
|
||||
],
|
||||
"fix": [
|
||||
"php-cs-fixer fix --verbose"
|
||||
],
|
||||
"test": [
|
||||
"phpunit"
|
||||
]
|
||||
}
|
||||
}
|
||||
+4267
File diff suppressed because it is too large
Load Diff
+400
@@ -0,0 +1,400 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Jfcherng\Utility;
|
||||
|
||||
/**
|
||||
* An internal UTF-32 multi-bytes string class.
|
||||
*
|
||||
* Because UTF-8 is varied-width, mb_*() is kinda O(n) when doing decoding.
|
||||
* Using iconv() to make it UTF-32 and work with str*() can be possibly faster.
|
||||
*
|
||||
* UTF-32 is a fix-width encoding (1 char = 4 bytes).
|
||||
* Note that the first 4 bytes in a UTF-32 string is the header (endian bytes).
|
||||
*
|
||||
* @author Jack Cherng <jfcherng@gmail.com>
|
||||
*/
|
||||
class MbString extends \ArrayObject implements \Stringable
|
||||
{
|
||||
public const MBSTRING_CONVMETHOD_ICONV = 1;
|
||||
public const MBSTRING_CONVMETHOD_MBSTRING = 2;
|
||||
|
||||
/**
|
||||
* The way to convert text encoding.
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public static $convMethod;
|
||||
|
||||
/**
|
||||
* UTF-32 string without endian bytes.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $str;
|
||||
|
||||
/**
|
||||
* The original encoding.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $encoding;
|
||||
|
||||
/**
|
||||
* The endian bytes for UTF-32.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected static $utf32Header;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param string $str the string
|
||||
* @param string $encoding the encoding
|
||||
*/
|
||||
public function __construct(string $str = '', string $encoding = 'UTF-8')
|
||||
{
|
||||
static::$convMethod ??= static::detectConvEncoding();
|
||||
static::$utf32Header ??= static::getUtf32Header();
|
||||
|
||||
$this->encoding = $encoding;
|
||||
$this->set($str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the object.
|
||||
*
|
||||
* @return string string representation of the object
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* The string setter.
|
||||
*
|
||||
* @param string $str the string
|
||||
*/
|
||||
public function set(string $str): self
|
||||
{
|
||||
$this->str = $this->inputConv($str);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setAt(int $idx, string $char): self
|
||||
{
|
||||
$char = $this->inputConv($char);
|
||||
if (\strlen($char) > 4) {
|
||||
$char = substr($char, 0, 4);
|
||||
}
|
||||
|
||||
$spacesPrepend = $idx - $this->strlen();
|
||||
// set index (out of bound)
|
||||
if ($spacesPrepend > 0) {
|
||||
$this->str .= $this->inputConv(str_repeat(' ', $spacesPrepend)) . $char;
|
||||
}
|
||||
// set index (in bound)
|
||||
else {
|
||||
$this->str = substr_replace($this->str, $char, $idx << 2, 4);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The string getter.
|
||||
*/
|
||||
public function get(): string
|
||||
{
|
||||
return $this->outputConv($this->str);
|
||||
}
|
||||
|
||||
/**
|
||||
* The raw string getter.
|
||||
*
|
||||
* @return string the UTF-32-encoded raw string
|
||||
*/
|
||||
public function getRaw(): string
|
||||
{
|
||||
return $this->str;
|
||||
}
|
||||
|
||||
public function getAt(int $idx): string
|
||||
{
|
||||
return $this->outputConv(substr($this->str, $idx << 2, 4));
|
||||
}
|
||||
|
||||
public function getAtRaw(int $idx): string
|
||||
{
|
||||
return substr($this->str, $idx << 2, 4);
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return self::strToChars($this->get());
|
||||
}
|
||||
|
||||
public function toArraySplit(string $regex, int $limit = -1, $flags = 0): array
|
||||
{
|
||||
if ($this->str === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return preg_split($regex, $this->get(), $limit, $flags);
|
||||
}
|
||||
|
||||
public function toArrayRaw(): array
|
||||
{
|
||||
if ($this->str === '') {
|
||||
return [];
|
||||
}
|
||||
|
||||
return str_split($this->str, 4);
|
||||
}
|
||||
|
||||
public static function strToChars(string $str): array
|
||||
{
|
||||
return preg_split('//uS', $str, -1, \PREG_SPLIT_NO_EMPTY) ?: [];
|
||||
}
|
||||
|
||||
// /////////////////////////////////
|
||||
// string manipulation functions //
|
||||
// /////////////////////////////////
|
||||
|
||||
public function stripos(string $needle, int $offset = 0)
|
||||
{
|
||||
$needle = $this->inputConv($needle);
|
||||
$pos = stripos($this->str, $needle, $offset << 2);
|
||||
|
||||
return \is_bool($pos) ? $pos : $pos >> 2;
|
||||
}
|
||||
|
||||
public function strlen(): int
|
||||
{
|
||||
return \strlen($this->str) >> 2;
|
||||
}
|
||||
|
||||
public function strpos(string $needle, int $offset = 0)
|
||||
{
|
||||
$needle = $this->inputConv($needle);
|
||||
$pos = strpos($this->str, $needle, $offset << 2);
|
||||
|
||||
return \is_bool($pos) ? $pos : $pos >> 2;
|
||||
}
|
||||
|
||||
public function substr(int $start = 0, ?int $length = null): string
|
||||
{
|
||||
return $this->outputConv(
|
||||
isset($length)
|
||||
? substr($this->str, $start << 2, $length << 2)
|
||||
: substr($this->str, $start << 2),
|
||||
);
|
||||
}
|
||||
|
||||
public function substr_replace(string $replacement, int $start = 0, ?int $length = null): string
|
||||
{
|
||||
$replacement = $this->inputConv($replacement);
|
||||
|
||||
return $this->outputConv(
|
||||
isset($length)
|
||||
? substr_replace($this->str, $replacement, $start << 2, $length << 2)
|
||||
: substr_replace($this->str, $replacement, $start << 2),
|
||||
);
|
||||
}
|
||||
|
||||
public function strtolower(): string
|
||||
{
|
||||
return strtolower($this->get());
|
||||
}
|
||||
|
||||
public function strtoupper(): string
|
||||
{
|
||||
return strtoupper($this->get());
|
||||
}
|
||||
|
||||
// //////////////////////////////
|
||||
// non-manipulative functions //
|
||||
// //////////////////////////////
|
||||
|
||||
public function has(string $needle): bool
|
||||
{
|
||||
$needle = $this->inputConv($needle);
|
||||
|
||||
return str_contains($this->str, $needle);
|
||||
}
|
||||
|
||||
public function startsWith(string $needle): bool
|
||||
{
|
||||
$needle = $this->inputConv($needle);
|
||||
|
||||
return $needle === substr($this->str, 0, \strlen($needle));
|
||||
}
|
||||
|
||||
public function endsWith(string $needle): bool
|
||||
{
|
||||
$needle = $this->inputConv($needle);
|
||||
$length = \strlen($needle);
|
||||
|
||||
return $length === 0 ? true : $needle === substr($this->str, -$length);
|
||||
}
|
||||
|
||||
// ///////////////////////////////////////////
|
||||
// those functions will not return a value //
|
||||
// ///////////////////////////////////////////
|
||||
|
||||
public function str_insert_i(string $insert, int $position): self
|
||||
{
|
||||
$insert = $this->inputConv($insert);
|
||||
$this->str = substr_replace($this->str, $insert, $position << 2, 0);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function str_enclose_i(array $closures, int $start = 0, ?int $length = null): self
|
||||
{
|
||||
// ex: $closures = array('{', '}');
|
||||
foreach ($closures as &$closure) {
|
||||
$closure = $this->inputConv($closure);
|
||||
}
|
||||
unset($closure);
|
||||
|
||||
if (\count($closures) < 2) {
|
||||
$closures[0] = $closures[1] = reset($closures);
|
||||
}
|
||||
|
||||
if (isset($length)) {
|
||||
$replacement = $closures[0] . substr($this->str, $start << 2, $length << 2) . $closures[1];
|
||||
$this->str = substr_replace($this->str, $replacement, $start << 2, $length << 2);
|
||||
} else {
|
||||
$replacement = $closures[0] . substr($this->str, $start << 2) . $closures[1];
|
||||
$this->str = substr_replace($this->str, $replacement, $start << 2);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function str_replace_i(string $search, string $replace): self
|
||||
{
|
||||
$search = $this->inputConv($search);
|
||||
$replace = $this->inputConv($replace);
|
||||
$this->str = str_replace($search, $replace, $this->str);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function substr_replace_i(string $replacement, int $start = 0, ?int $length = null): self
|
||||
{
|
||||
$replacement = $this->inputConv($replacement);
|
||||
$this->str = (
|
||||
isset($length)
|
||||
? substr_replace($this->str, $replacement, $start << 2, $length << 2)
|
||||
: substr_replace($this->str, $replacement, $start << 2)
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
// ///////////////
|
||||
// ArrayObject //
|
||||
// ///////////////
|
||||
|
||||
public function offsetSet(mixed $idx, mixed $char): void
|
||||
{
|
||||
$this->setAt($idx, $char);
|
||||
}
|
||||
|
||||
public function offsetGet(mixed $idx): string
|
||||
{
|
||||
return $this->getAt($idx);
|
||||
}
|
||||
|
||||
public function offsetExists(mixed $idx): bool
|
||||
{
|
||||
return \is_int($idx) ? $this->strlen() > $idx : false;
|
||||
}
|
||||
|
||||
public function append(mixed $str): void
|
||||
{
|
||||
$this->str .= $this->inputConv($str);
|
||||
}
|
||||
|
||||
public function count(): int
|
||||
{
|
||||
return $this->strlen();
|
||||
}
|
||||
|
||||
// //////////////////
|
||||
// misc functions //
|
||||
// //////////////////
|
||||
|
||||
/**
|
||||
* Gets the utf 32 header.
|
||||
*
|
||||
* @return string the UTF-32 header or empty string
|
||||
*/
|
||||
protected static function getUtf32Header(): string
|
||||
{
|
||||
// just use any string to get the endian header, here we use "A"
|
||||
$tmp = self::convEncoding('A', 'UTF-8', 'UTF-32');
|
||||
// some distributions like "php alpine" docker image won't generate the header
|
||||
return $tmp && \strlen($tmp) > 4 ? substr($tmp, 0, 4) : '';
|
||||
}
|
||||
|
||||
protected static function detectConvEncoding(): int
|
||||
{
|
||||
if (\function_exists('iconv') && iconv('UTF-8', 'UTF-32', 'A') !== false) {
|
||||
return static::MBSTRING_CONVMETHOD_ICONV;
|
||||
}
|
||||
|
||||
if (\function_exists('mb_convert_encoding') && mb_convert_encoding('A', 'UTF-32', 'UTF-8') !== false) {
|
||||
return static::MBSTRING_CONVMETHOD_MBSTRING;
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Either "iconv" or "mbstring" extension is required.');
|
||||
}
|
||||
|
||||
protected static function convEncoding(string $str, string $from, string $to): string
|
||||
{
|
||||
if (static::$convMethod === static::MBSTRING_CONVMETHOD_ICONV) {
|
||||
return iconv($from, $to, $str);
|
||||
}
|
||||
|
||||
if (static::$convMethod === static::MBSTRING_CONVMETHOD_MBSTRING) {
|
||||
return mb_convert_encoding($str, $to, $from);
|
||||
}
|
||||
|
||||
throw new \RuntimeException('Unknown conversion method.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the output string to its original encoding.
|
||||
*
|
||||
* @param string $str The string
|
||||
*/
|
||||
protected function outputConv(string $str): string
|
||||
{
|
||||
if ($str === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return static::convEncoding(static::$utf32Header . $str, 'UTF-32', $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the input string to UTF-32 without header.
|
||||
*
|
||||
* @param string $str The string
|
||||
*/
|
||||
protected function inputConv(string $str): string
|
||||
{
|
||||
if ($str === '') {
|
||||
return '';
|
||||
}
|
||||
|
||||
return substr(static::convEncoding($str, $this->encoding, 'UTF-32'), \strlen(static::$utf32Header));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user