From 0c6fc86ecce6ed85afe45063aa6cc59ccbe9debc Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Fri, 26 Jul 2024 13:19:57 +0200 Subject: [PATCH] fix(testing): Make Testing TextProcessing providers unicode safe Signed-off-by: Marcel Klehr --- .../lib/Provider/FakeTextProcessingProvider.php | 16 +++++++++++++++- .../Provider/FakeTextProcessingProviderSync.php | 16 +++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/apps/testing/lib/Provider/FakeTextProcessingProvider.php b/apps/testing/lib/Provider/FakeTextProcessingProvider.php index 7a318196ab3d2..25bf49e74a995 100644 --- a/apps/testing/lib/Provider/FakeTextProcessingProvider.php +++ b/apps/testing/lib/Provider/FakeTextProcessingProvider.php @@ -18,10 +18,24 @@ public function getName(): string { } public function process(string $prompt): string { - return strrev($prompt) . ' (done with FakeTextProcessingProvider)'; + return $this->mb_strrev($prompt) . ' (done with FakeTextProcessingProvider)'; } public function getTaskType(): string { return FreePromptTaskType::class; } + + /** + * Reverse a miltibyte string. + * + * @param string $string The string to be reversed. + * @param string|null $encoding The character encoding. If it is omitted, the internal character encoding value + * will be used. + * @return string The reversed string + */ + private function mb_strrev(string $string, string $encoding = null): string + { + $chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding()); + return implode('', array_reverse($chars)); + } } diff --git a/apps/testing/lib/Provider/FakeTextProcessingProviderSync.php b/apps/testing/lib/Provider/FakeTextProcessingProviderSync.php index f6fe63c0f8275..5a672d25900d5 100644 --- a/apps/testing/lib/Provider/FakeTextProcessingProviderSync.php +++ b/apps/testing/lib/Provider/FakeTextProcessingProviderSync.php @@ -20,7 +20,7 @@ public function getName(): string { } public function process(string $prompt): string { - return strrev($prompt) . ' (done with FakeTextProcessingProviderSync)'; + return $this->mb_strrev($prompt) . ' (done with FakeTextProcessingProviderSync)'; } public function getTaskType(): string { @@ -30,4 +30,18 @@ public function getTaskType(): string { public function getExpectedRuntime(): int { return 1; } + + /** + * Reverse a miltibyte string. + * + * @param string $string The string to be reversed. + * @param string|null $encoding The character encoding. If it is omitted, the internal character encoding value + * will be used. + * @return string The reversed string + */ + private function mb_strrev(string $string, string $encoding = null): string + { + $chars = mb_str_split($string, 1, $encoding ?: mb_internal_encoding()); + return implode('', array_reverse($chars)); + } }