Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Fritz Duchardt <fritz@duchardt.net>
  • Loading branch information
fritzduchardt committed Jun 17, 2023
1 parent a5ad2b8 commit 53555ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
11 changes: 9 additions & 2 deletions pkg/yamlmeta/internal/yaml.v2/emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,7 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
followedByWhitespace = false
previousSpace = false
previousBreak = false
previousColon = false
)

emitter.scalarData.value = value
Expand Down Expand Up @@ -1061,7 +1062,11 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
if !isPrintable(value, i) || !isASCII(value, i) && !emitter.unicode {
specialCharacters = true
}
if isSpace(value, i) {
if isColon(value, i) {
previousColon = true
previousSpace = false
previousBreak = false
} else if isSpace(value, i) {
if i == 0 {
leadingSpace = true
}
Expand All @@ -1081,14 +1086,16 @@ func yamlEmitterAnalyzeScalar(emitter *yamlEmitterT, value []byte) bool {
if i+width(value[i]) == len(value) {
trailingBreak = true
}
if previousSpace {
if previousSpace && !previousColon {
spaceBreak = true
}
previousSpace = false
previousBreak = true
previousColon = false
} else {
previousSpace = false
previousBreak = false
previousColon = false
}

// [Go]: Why 'z'? Couldn't be the end of the string as that's the loop condition.
Expand Down
12 changes: 12 additions & 0 deletions pkg/yamlmeta/internal/yaml.v2/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ var marshalTests = []struct {
&marshalIntTest,
"123\n",
},
{
"a:\n b",
"|-\n a:\n b\n",
},
{
"a: \n b",
"|-\n a: \n b\n",
},
{
"a: \n b",
"|-\n a: \n b\n",
},

// Structures
{
Expand Down
5 changes: 5 additions & 0 deletions pkg/yamlmeta/internal/yaml.v2/yamlprivateh.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func isSpace(b []byte, i int) bool {
return b[i] == ' '
}

// Check if the character at the specified position is colon.
func isColon(b []byte, i int) bool {
return b[i] == ':'
}

// Check if the character at the specified position is tab.
func isTab(b []byte, i int) bool {
return b[i] == '\t'
Expand Down

0 comments on commit 53555ab

Please sign in to comment.