diff --git a/assets/comments2/strings_result.hjson b/assets/comments2/strings_result.hjson index 26967cd..8e4c898 100644 --- a/assets/comments2/strings_result.hjson +++ b/assets/comments2/strings_result.hjson @@ -7,6 +7,32 @@ text4a: " untrimmed " text4b: " untrimmed" text4c: "untrimmed " + text4d: " u" + text4d2: "u " + text4e: "\u0085u" + text4e2: "u\u0085" + text4f: " u" + text4f2: "u " + text4g: " u" + text4g2: "u " + text4h: " u" + text4h2: "u " + text4i: " u" + text4i2: "u " + text4j: "\u2028u" + text4j2: "u\u2028" + text4k: "\u2029u" + text4k2: "u\u2029" + text4l: "\u202fu" + text4l2: "u\u202f" + text4m: " u" + text4m2: "u " + text4n: " u" + text4n2: "u " + text4o: ​u + text4o2: u​ + text4p: ᠎u + text4p2: u᠎ notml1: "\n" notml2: " \n" notml3: "\n \n \n \n" diff --git a/assets/comments3/strings_result.hjson b/assets/comments3/strings_result.hjson index efd44c0..8d7633f 100644 --- a/assets/comments3/strings_result.hjson +++ b/assets/comments3/strings_result.hjson @@ -9,6 +9,32 @@ text4a: " untrimmed " text4b: " untrimmed" text4c: "untrimmed " + text4d: " u" + text4d2: "u " + text4e: "\u0085u" + text4e2: "u\u0085" + text4f: " u" + text4f2: "u " + text4g: " u" + text4g2: "u " + text4h: " u" + text4h2: "u " + text4i: " u" + text4i2: "u " + text4j: "\u2028u" + text4j2: "u\u2028" + text4k: "\u2029u" + text4k2: "u\u2029" + text4l: "\u202fu" + text4l2: "u\u202f" + text4m: " u" + text4m2: "u " + text4n: " u" + text4n2: "u " + text4o: ​u + text4o2: u​ + text4p: ᠎u + text4p2: u᠎ notml1: "\n" notml2: " \n" diff --git a/assets/sorted/strings_result.hjson b/assets/sorted/strings_result.hjson index fe8608b..dce614c 100644 --- a/assets/sorted/strings_result.hjson +++ b/assets/sorted/strings_result.hjson @@ -80,4 +80,30 @@ text4a: " untrimmed " text4b: " untrimmed" text4c: "untrimmed " + text4d: " u" + text4d2: "u " + text4e: "\u0085u" + text4e2: "u\u0085" + text4f: " u" + text4f2: "u " + text4g: " u" + text4g2: "u " + text4h: " u" + text4h2: "u " + text4i: " u" + text4i2: "u " + text4j: "\u2028u" + text4j2: "u\u2028" + text4k: "\u2029u" + text4k2: "u\u2029" + text4l: "\u202fu" + text4l2: "u\u202f" + text4m: " u" + text4m2: "u " + text4n: " u" + text4n2: "u " + text4o: ​u + text4o2: u​ + text4p: ᠎u + text4p2: u᠎ } diff --git a/assets/sorted/strings_result.json b/assets/sorted/strings_result.json index 910a2c3..aed744e 100644 --- a/assets/sorted/strings_result.json +++ b/assets/sorted/strings_result.json @@ -63,5 +63,31 @@ "text3": "You need quotes\tfor escapes", "text4a": " untrimmed ", "text4b": " untrimmed", - "text4c": "untrimmed " + "text4c": "untrimmed ", + "text4d": " u", + "text4d2": "u ", + "text4e": "…u", + "text4e2": "u…", + "text4f": " u", + "text4f2": "u ", + "text4g": " u", + "text4g2": "u ", + "text4h": " u", + "text4h2": "u ", + "text4i": " u", + "text4i2": "u ", + "text4j": "\u2028u", + "text4j2": "u\u2028", + "text4k": "\u2029u", + "text4k2": "u\u2029", + "text4l": " u", + "text4l2": "u ", + "text4m": " u", + "text4m2": "u ", + "text4n": " u", + "text4n2": "u ", + "text4o": "​u", + "text4o2": "u​", + "text4p": "᠎u", + "text4p2": "u᠎" } diff --git a/assets/strings_result.hjson b/assets/strings_result.hjson index 22f9e97..4365434 100644 --- a/assets/strings_result.hjson +++ b/assets/strings_result.hjson @@ -5,6 +5,32 @@ text4a: " untrimmed " text4b: " untrimmed" text4c: "untrimmed " + text4d: " u" + text4d2: "u " + text4e: "\u0085u" + text4e2: "u\u0085" + text4f: " u" + text4f2: "u " + text4g: " u" + text4g2: "u " + text4h: " u" + text4h2: "u " + text4i: " u" + text4i2: "u " + text4j: "\u2028u" + text4j2: "u\u2028" + text4k: "\u2029u" + text4k2: "u\u2029" + text4l: "\u202fu" + text4l2: "u\u202f" + text4m: " u" + text4m2: "u " + text4n: " u" + text4n2: "u " + text4o: ​u + text4o2: u​ + text4p: ᠎u + text4p2: u᠎ notml1: "\n" notml2: " \n" notml3: "\n \n \n \n" diff --git a/assets/strings_result.json b/assets/strings_result.json index d236b63..a3539aa 100644 --- a/assets/strings_result.json +++ b/assets/strings_result.json @@ -5,6 +5,32 @@ "text4a": " untrimmed ", "text4b": " untrimmed", "text4c": "untrimmed ", + "text4d": " u", + "text4d2": "u ", + "text4e": "…u", + "text4e2": "u…", + "text4f": " u", + "text4f2": "u ", + "text4g": " u", + "text4g2": "u ", + "text4h": " u", + "text4h2": "u ", + "text4i": " u", + "text4i2": "u ", + "text4j": "\u2028u", + "text4j2": "u\u2028", + "text4k": "\u2029u", + "text4k2": "u\u2029", + "text4l": " u", + "text4l2": "u ", + "text4m": " u", + "text4m2": "u ", + "text4n": " u", + "text4n2": "u ", + "text4o": "​u", + "text4o2": "u​", + "text4p": "᠎u", + "text4p2": "u᠎", "notml1": "\n", "notml2": " \n", "notml3": "\n \n \n \n", diff --git a/assets/strings_test.hjson b/assets/strings_test.hjson index 3c042fe..00ae3fb 100644 --- a/assets/strings_test.hjson +++ b/assets/strings_test.hjson @@ -9,6 +9,32 @@ text4a: " untrimmed " text4b: " untrimmed" text4c: "untrimmed " + text4d: "\u0020u" + text4d2: "u\u0020" + text4e: "\u0085u" + text4e2: "u\u0085" + text4f: "\u00a0u" + text4f2: "u\u00a0" + text4g: "\u1680u" + text4g2: "u\u1680" + text4h: "\u2000u" + text4h2: "u\u2000" + text4i: "\u200au" + text4i2: "u\u200a" + text4j: "\u2028u" + text4j2: "u\u2028" + text4k: "\u2029u" + text4k2: "u\u2029" + text4l: "\u202fu" + text4l2: "u\u202f" + text4m: "\u205fu" + text4m2: "u\u205f" + text4n: "\u3000u" + text4n2: "u\u3000" + text4o: "\u200bu" + text4o2: "u\u200b" + text4p: "\u180eu" + text4p2: "u\u180e" notml1: "\n" notml2: " \n" diff --git a/encode.go b/encode.go index 3345a0b..492069f 100644 --- a/encode.go +++ b/encode.go @@ -12,6 +12,7 @@ import ( "sort" "strconv" "strings" + "unicode" "unicode/utf8" ) @@ -126,14 +127,21 @@ func (e *hjsonEncoder) quoteForComment(cmStr string) bool { func (e *hjsonEncoder) quote(value string, separator string, isRootObject bool, keyComment string, hasCommentAfter bool) { + if len(value) == 0 { + e.WriteString(separator + `""`) + return + } + // Check if we can insert this string without quotes // see hjson syntax (must not parse as true, false, null or number) - if len(value) == 0 { - e.WriteString(separator + `""`) - } else if e.QuoteAlways || + firstRune, _ := utf8.DecodeRuneInString(value) + lastRune, _ := utf8.DecodeLastRuneInString(value) + if e.QuoteAlways || hasCommentAfter || needsQuotes.MatchString(value) || + unicode.IsSpace(firstRune) || + unicode.IsSpace(lastRune) || (e.QuoteAmbiguousStrings && (startsWithNumber([]byte(value)) || startsWithKeyword.MatchString(value))) {