From 00861d50abdf4a3577d3c3198c0efd061edd4554 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 25 Sep 2026 07:33:47 +0200 Subject: [PATCH] Fix split trailing newline --- CHANGELOG | 2 ++ src/Extension/CoreExtension.php | 8 ++++---- .../filters/characters_trailing_newline.test | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/Fixtures/filters/characters_trailing_newline.test diff --git a/CHANGELOG b/CHANGELOG index ecc59c5a4..ef8f11937 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,7 @@ # 3.30.0 (2026-XX-XX) + * Fix `split`, `random`, and `shuffle` merging a trailing newline into the last character of a string + * Speed up splitting a string into characters in `split`, `random`, and `shuffle` * Speed up adding extensions to an environment * Fix the escaping safe analysis retaining every compiled template node for the lifetime of the environment * Speed up loading a template that the environment has already loaded diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index d62f733b2..f5f5e3952 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1263,19 +1263,19 @@ final class CoreExtension extends AbstractExtension } /** - * Unicode version of str_split(): splits at every position except after the start and before the end. + * Unicode version of str_split(), an empty string giving a single empty character. * - * @return list + * @return non-empty-list * * @throws RuntimeError When the string cannot be split into characters */ private static function splitIntoCharacters(string $string, string $name): array { - if (false === $characters = preg_split('/(? "ab\n"] +--EXPECT-- +"\nba" +["a","b","\n"] +3 +1