Skip to content

Commit

Permalink
Fix condition check for encoding as literal/folded
Browse files Browse the repository at this point in the history
  • Loading branch information
kekavc24 committed Jun 17, 2024
1 parent 8c29754 commit d8849b6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/src/strings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,11 @@ String _getChompingIndicator(String string) {
///
/// See: https://yaml.org/spec/1.2.2/#813-folded-style
String? _tryYamlEncodeFolded(String string, int indentSize, String lineEnding) {
if (_hasUnprintableCharacters(string)) return null;
// A string that starts with space or newline followed by space can't be
// encoded in folded mode.
if (string.startsWith(' ') && string.startsWith('\n ')) return null;
if (string.isEmpty || string.trimLeft().length != string.length) return null;

if (_hasUnprintableCharacters(string)) return null;

// TODO: Are there other strings we can't encode in folded mode?

Expand Down Expand Up @@ -181,18 +182,19 @@ 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 (_hasUnprintableCharacters(string)) return null;
if (string.isEmpty || string.trimLeft().length != string.length) return null;

// A string that starts with space or newline followed by space can't be
// encoded in literal mode.
if (string.startsWith(' ') && string.startsWith('\n ')) return null;
if (_hasUnprintableCharacters(string)) return null;

// TODO: Are there other strings we can't encode in literal mode?

final indent = ' ' * indentSize;

/// Simplest block style.
/// * https://yaml.org/spec/1.2.2/#812-literal-style
return '|${string.endsWith('\n') ? '' : '-'}\n$indent'
return '|${_getChompingIndicator(string)}\n$indent'
'${string.replaceAll('\n', lineEnding + indent)}';
}

Expand Down

0 comments on commit d8849b6

Please sign in to comment.