Skip to content

Commit

Permalink
Marshal \r to quoted strings (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
trobro authored Jul 23, 2022
1 parent d950da4 commit fed36c4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
11 changes: 4 additions & 7 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ func init() {
needsEscape = regexp.MustCompile(`[\\\"\x00-\x1f` + commonRange + `]`)
// needsQuotes tests if the string can be written as a quoteless string (includes needsEscape but without \\ and \")
needsQuotes = regexp.MustCompile(`^\s|^"|^'|^#|^/\*|^//|^\{|^\}|^\[|^\]|^:|^,|\s$|[\x00-\x1f\x7f-\x9f\x{00ad}\x{0600}-\x{0604}\x{070f}\x{17b4}\x{17b5}\x{200c}-\x{200f}\x{2028}-\x{202f}\x{2060}-\x{206f}\x{feff}\x{fff0}-\x{ffff}]`)
// needsEscapeML tests if the string can be written as a multiline string (like needsEscape but without \n, \r, \\, \", \t)
var x08Or9 = `\x08` // `\x09` for the old behavior
needsEscapeML = regexp.MustCompile(`'''|^[\s]+$|[\x00-` + x08Or9 + `\x0b\x0c\x0e-\x1f` + commonRange + `]`)
// needsEscapeML tests if the string can be written as a multiline string (like needsEscape but without \n, \\, \", \t)
needsEscapeML = regexp.MustCompile(`'''|^[\s]+$|[\x00-\x08\x0b-\x1f` + commonRange + `]`)
// starts with a keyword and optionally is followed by a comment
startsWithKeyword = regexp.MustCompile(`^(true|false|null)\s*((,|\]|\}|#|//|/\*).*)?$`)
needsEscapeName = regexp.MustCompile(`[,\{\[\}\]\s:#"']|//|/\*`)
Expand Down Expand Up @@ -126,9 +125,7 @@ func (e *hjsonEncoder) quote(value string, separator string, isRootObject bool)
}

func (e *hjsonEncoder) mlString(value string, separator string) {
// wrap the string into the ''' (multiline) format

a := strings.Split(strings.Replace(value, "\r", "", -1), "\n")
a := strings.Split(value, "\n")

if len(a) == 1 {
// The string contains only a single line. We still use the multiline
Expand Down Expand Up @@ -213,7 +210,7 @@ func (e *hjsonEncoder) str(value reflect.Value, noIndent bool, separator string,
e.WriteString("null")
return nil
}
return e.str(value.Elem(),noIndent,separator,isRootObject)
return e.str(value.Elem(), noIndent, separator, isRootObject)
}

if value.Type().Implements(marshaler) {
Expand Down
45 changes: 45 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,48 @@ func TestQuoteAmbiguousStrings(t *testing.T) {
t.Error("Encode with QuoteAmbiguousStrings false, comparison:\n", string(buf), "\n", string(facit))
}
}

func marshalUnmarshal(t *testing.T, input string) {
buf, err := Marshal(input)
if err != nil {
t.Fatal(err)
}
var resultPlain string
err = Unmarshal(buf, &resultPlain)
if err != nil {
t.Fatal(err)
}
if resultPlain != input {
t.Fatalf("Expected: '%v' Got: '%v'\n", []byte(input), []byte(resultPlain))
}

type t_obj struct {
F string
}
obj := t_obj{
F: input,
}
buf, err = Marshal(obj)
if err != nil {
t.Fatal(err)
}
var out map[string]interface{}
err = Unmarshal(buf, &out)
if err != nil {
t.Fatal(err)
}
if out["F"] != input {
t.Fatalf("Expected: '%v' Got: '%v'\n", []byte(input), []byte(out["F"].(string)))
}
}

func TestMarshalUnmarshal(t *testing.T) {
marshalUnmarshal(t, "0\r'")
marshalUnmarshal(t, "0\r")
marshalUnmarshal(t, "0\n'")
marshalUnmarshal(t, "0\n")
marshalUnmarshal(t, "\t0\na\tb\t")
marshalUnmarshal(t, "\t0\n\tab")
marshalUnmarshal(t, "0\r\n'")
marshalUnmarshal(t, "0\r\n")
}
5 changes: 3 additions & 2 deletions hjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ func getContent(file string) []byte {
panic(err)
} else {
// The output from Marshal() always uses Unix EOL, but git might have
// converted files to Windows EOL on Windows, therefore we remove all "\r".
return bytes.Replace(data, []byte("\r"), []byte(""), -1)
// converted files to Windows EOL on Windows, therefore we convert all
// "\r\n" to "\n".
return bytes.Replace(data, []byte("\r\n"), []byte("\n"), -1)
}
}

Expand Down

0 comments on commit fed36c4

Please sign in to comment.