| 1 | package format |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "unicode/utf8" |
| 6 | ) |
| 7 | |
| 8 | // sectionMode classifies one ';'-separated format section so renderSection |
| 9 | // knows which tokenizer to run. |
| 10 | type sectionMode int |
| 11 | |
| 12 | const ( |
| 13 | modeLiteral sectionMode = iota // no placeholders: always the same text |
| 14 | modeNumeric |
| 15 | modeDate |
| 16 | ) |
| 17 | |
| 18 | // detectMode scans a section for bare (unquoted, unescaped) characters |
| 19 | // that decide its kind: any of yYmMdDhHsS means a date/time section, else |
| 20 | // any of 0# means a numeric section, else the section is pure literal |
| 21 | // text. This must run before tokenizing, because a date section and a |
| 22 | // numeric section use different rules for the same bytes (',' is a |
| 23 | // thousands separator in a numeric section, a literal in a date one). |
| 24 | func detectMode(section string) sectionMode { |
| 25 | sawDate := false |
| 26 | sawNumeric := false |
| 27 | walkBareRunes(section, func(r byte) bool { |
| 28 | switch { |
| 29 | case strings.IndexByte("yYmMdDhHsS", r) >= 0: |
| 30 | sawDate = true |
| 31 | return true // date wins outright — stop scanning early |
| 32 | case r == '0' || r == '#': |
| 33 | sawNumeric = true |
| 34 | } |
| 35 | return false |
| 36 | }) |
| 37 | if sawDate { |
| 38 | return modeDate |
| 39 | } |
| 40 | if sawNumeric { |
| 41 | return modeNumeric |
| 42 | } |
| 43 | return modeLiteral |
| 44 | } |
| 45 | |
| 46 | // walkBareRunes calls fn for every byte in section NOT inside a "quoted" |
| 47 | // literal and not the escaped character following a backslash. Stops |
| 48 | // early if fn returns true. |
| 49 | func walkBareRunes(section string, fn func(byte) bool) { |
| 50 | inQuote := false |
| 51 | for i := 0; i < len(section); i++ { |
| 52 | c := section[i] |
| 53 | switch { |
| 54 | case c == '\\' && i+1 < len(section): |
| 55 | i++ // skip the escaped character itself |
| 56 | case c == '"': |
| 57 | inQuote = !inQuote |
| 58 | case inQuote: |
| 59 | // inside a literal: not a bare rune |
| 60 | default: |
| 61 | if fn(c) { |
| 62 | return |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // rendersNumber reports whether section actually renders the number |
| 69 | // (via "General", numeric, or date placeholders) rather than being a |
| 70 | // fixed literal label — used to decide whether a single-section format |
| 71 | // gets an automatic "-" for a negative value. |
| 72 | func rendersNumber(section string) bool { |
| 73 | section = expandCurrency(section) |
| 74 | if strings.EqualFold(strings.TrimSpace(section), "General") { |
| 75 | return true |
| 76 | } |
| 77 | return detectMode(section) != modeLiteral |
| 78 | } |
| 79 | |
| 80 | // renderSection renders val (already sign-adjusted by the caller) |
| 81 | // through one format section. ok is false when the section uses a |
| 82 | // construct this package does not support, telling the caller to fall |
| 83 | // back to General. |
| 84 | func renderSection(section string, val float64) (string, bool) { |
| 85 | section = expandCurrency(section) |
| 86 | if strings.EqualFold(strings.TrimSpace(section), "General") { |
| 87 | return generalNumber(val), true |
| 88 | } |
| 89 | switch detectMode(section) { |
| 90 | case modeDate: |
| 91 | return renderDate(section, val) |
| 92 | case modeNumeric: |
| 93 | return renderNumeric(section, val) |
| 94 | default: |
| 95 | return renderLiteral(section) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // renderLiteral renders a section with no digit or date placeholders at |
| 100 | // all: the same text regardless of val (e.g. a section used purely to |
| 101 | // hide a value, or to show a fixed label like "-" or "n/a"). A bare |
| 102 | // ASCII letter is still rejected — Excel reserves unquoted letters for |
| 103 | // format tokens, so an unrecognized one (e.g. a stray "q") is treated |
| 104 | // as an unsupported construct rather than assumed to be a literal word. |
| 105 | func renderLiteral(section string) (string, bool) { |
| 106 | var sb strings.Builder |
| 107 | ok := true |
| 108 | walkLiteralAndPlaceholders(section, func(lit string) { sb.WriteString(lit) }, func(c byte) bool { |
| 109 | if isASCIILetter(c) || strings.IndexByte("?@_*", c) >= 0 { |
| 110 | ok = false |
| 111 | return true |
| 112 | } |
| 113 | sb.WriteByte(c) |
| 114 | return false |
| 115 | }) |
| 116 | return sb.String(), ok |
| 117 | } |
| 118 | |
| 119 | // renderTextSection renders the 4th (text) section: '@' inserts s |
| 120 | // verbatim, everything else is literal. |
| 121 | func renderTextSection(section, s string) (string, bool) { |
| 122 | var sb strings.Builder |
| 123 | ok := true |
| 124 | walkLiteralAndPlaceholders(section, func(lit string) { sb.WriteString(lit) }, func(c byte) bool { |
| 125 | if c == '@' { |
| 126 | sb.WriteString(s) |
| 127 | return false |
| 128 | } |
| 129 | ok = false |
| 130 | return true |
| 131 | }) |
| 132 | return sb.String(), ok |
| 133 | } |
| 134 | |
| 135 | // walkLiteralAndPlaceholders walks section, resolving quoted "..." runs |
| 136 | // and \-escaped characters into literal text passed to onLiteral, and |
| 137 | // passing every other bare byte to onOther. onOther returns true to |
| 138 | // abort (unsupported construct). |
| 139 | func walkLiteralAndPlaceholders(section string, onLiteral func(string), onOther func(byte) bool) { |
| 140 | i := 0 |
| 141 | for i < len(section) { |
| 142 | c := section[i] |
| 143 | switch { |
| 144 | case c == '\\' && i+1 < len(section): |
| 145 | _, size := utf8.DecodeRuneInString(section[i+1:]) |
| 146 | onLiteral(section[i+1 : i+1+size]) |
| 147 | i += 1 + size |
| 148 | case c == '"': |
| 149 | j := strings.IndexByte(section[i+1:], '"') |
| 150 | if j < 0 { |
| 151 | onLiteral(section[i+1:]) |
| 152 | i = len(section) |
| 153 | } else { |
| 154 | onLiteral(section[i+1 : i+1+j]) |
| 155 | i = i + 1 + j + 1 |
| 156 | } |
| 157 | case c >= utf8.RuneSelf: |
| 158 | _, size := utf8.DecodeRuneInString(section[i:]) |
| 159 | onLiteral(section[i : i+size]) |
| 160 | i += size |
| 161 | default: |
| 162 | if onOther(c) { |
| 163 | return |
| 164 | } |
| 165 | i++ |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |