From 64be5cef79469d32827901b27c506e8608bf52ff Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:00:19 +0100 Subject: [PATCH 1/2] Return null if string cannot be encoded as literal/folded --- lib/src/strings.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/strings.dart b/lib/src/strings.dart index dcb1b72..1b85641 100644 --- a/lib/src/strings.dart +++ b/lib/src/strings.dart @@ -96,7 +96,7 @@ String? _tryYamlEncodeSingleQuoted(String string) { String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) { // A string that starts with space or newline followed by space can't be // encoded in folded mode. - if (string.isEmpty || string.trimLeft().length != string.length) return null; + if (string.isEmpty || string.trim().length != string.length) return null; if (_hasUnprintableCharacters(string)) return null; @@ -164,7 +164,7 @@ String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) { /// See: https://yaml.org/spec/1.2.2/#812-literal-style String? _tryYamlEncodeLiteral( String string, int indentSize, String lineEnding) { - if (string.isEmpty || string.trimLeft().length != string.length) return null; + if (string.isEmpty || string.trim().length != string.length) return null; // A string that starts with space or newline followed by space can't be // encoded in literal mode. From 4d573da762c882ebe4f7090f0d9bc9a6571c7669 Mon Sep 17 00:00:00 2001 From: Kelvin Kavisi <68240897+kekavc24@users.noreply.github.com> Date: Mon, 1 Jul 2024 12:00:46 +0100 Subject: [PATCH 2/2] Add test variants for trailing line breaks --- test/string_test.dart | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/string_test.dart b/test/string_test.dart index b92c20a..5ea7e73 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -11,6 +11,11 @@ final _testStrings = [ 'whitespace\n after line breaks', 'whitespace\n \nbetween line breaks', '\n line break at the start', + 'whitespace and line breaks at end 1\n ', + 'whitespace and line breaks at end 2 \n \n', + 'whitespace and line breaks at end 3 \n\n', + 'whitespace and line breaks at end 4 \n\n ', + '\n\nline with multiple trailing line break \n\n\n\n\n', 'word', 'foo bar', 'foo\nbar',