mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-24 00:52:18 +01:00
Have wuffsfmt put underscores in long numbers
This commit is contained in:
1 parent
22be1c0da0
commit
b40671ec84
11 files changed
+636
-592
No files matched your search
+45
-1
@@ -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"
|
||||
|
||||
Reference in new issue
Block a user