mirror of
https://github.com/nextcloud/server.git
synced 2026-05-19 08:25:56 -04:00
add method to shorten a string to given byte size to Utils
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
parent
df961fa2f3
commit
c853206e8a
2 changed files with 31 additions and 0 deletions
|
|
@ -513,4 +513,28 @@ class Util {
|
|||
}
|
||||
return self::$needUpgradeCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sometimes a string has to be shortened to fit within a certain maximum
|
||||
* data length in bytes. substr() you may break multibyte characters,
|
||||
* because it operates on single byte level. mb_substr() operates on
|
||||
* characters, so does not ensure that the shortend string satisfies the
|
||||
* max length in bytes.
|
||||
*
|
||||
* For example, json_encode is messing with multibyte characters a lot,
|
||||
* replacing them with something along "\u1234".
|
||||
*
|
||||
* This function shortens the string with by $accurancy (-5) from
|
||||
* $dataLength characters, until it fits within $dataLength bytes.
|
||||
*
|
||||
* @since 23.0.0
|
||||
*/
|
||||
public static function shortenMultibyteString(string $subject, int $dataLength, int $accuracy = 5): string {
|
||||
$temp = mb_substr($subject, 0, $dataLength);
|
||||
// json encodes encapsulates the string in double quotes, they need to be substracted
|
||||
while ((strlen(json_encode($temp)) - 2) > $dataLength) {
|
||||
$temp = mb_substr($temp, 0, -$accuracy);
|
||||
}
|
||||
return $temp;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -310,4 +310,11 @@ class UtilTest extends \Test\TestCase {
|
|||
'myApp/vendor/myFancyCSSFile2',
|
||||
], \OC_Util::$styles);
|
||||
}
|
||||
|
||||
public function testShortenMultibyteString() {
|
||||
$this->assertEquals('Short nuff', \OCP\Util::shortenMultibyteString('Short nuff', 255));
|
||||
$this->assertEquals('ABC', \OCP\Util::shortenMultibyteString('ABCDEF', 3));
|
||||
// each of the characters is 12 bytes
|
||||
$this->assertEquals('🙈', \OCP\Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue