Have wuffsfmt put underscores in long numbers

This commit is contained in:
Nigel Tao committed 2020-02-08 18:21:29 +11:00
1 parent 22be1c0da0
commit b40671ec84
11 files changed
+636 -592

No files matched your search

+45 -1
View File
@@ -152,7 +152,11 @@ func Render(w io.Writer, tm *t.Map, src []t.Token, comments []string) (err error
}
}
buf = append(buf, tm.ByID(tok.ID)...)
if s := tm.ByID(tok.ID); (s == "") || (s[0] < '0') || ('9' < s[0]) {
buf = append(buf, s...)
} else {
buf = appendNum(buf, s)
}
if tok.ID == t.IDOpenCurly {
if indent == maxIndent {
@@ -231,6 +235,46 @@ func appendComment(buf []byte, comments []string, line uint32, indent int, other
return buf
}
func appendNum(buf []byte, s string) []byte {
groupLen := uint32(6)
if (len(s) >= 2) && (s[0] == '0') && ((s[1] == 'X') || (s[1] == 'x')) {
buf = append(buf, "0x"...)
s = s[2:]
groupLen = 4
}
nonUnderscores := uint32(0)
for i := 0; i < len(s); i++ {
c := s[i]
if c == '_' {
continue
}
nonUnderscores++
}
digitsUntilGroup := nonUnderscores % groupLen
if digitsUntilGroup == 0 {
digitsUntilGroup = groupLen
}
for i := 0; i < len(s); i++ {
c := s[i]
if c == '_' {
continue
} else if 'a' <= c {
c -= 'a' - 'A'
}
if digitsUntilGroup > 0 {
digitsUntilGroup--
} else {
digitsUntilGroup = groupLen - 1
buf = append(buf, '_')
}
buf = append(buf, c)
}
return buf
}
func appendTabs(buf []byte, n int) []byte {
if n > 0 {
const tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"