Skip to content
This repository has been archived by the owner on Feb 10, 2025. It is now read-only.

Fix fold literal encoding with trailing line break #91

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions test/string_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down