| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "amadan.net/oficina/sheets/internal/sheets/calc" |
| 7 | ) |
| 8 | |
| 9 | // MaxInputBytes caps one cell's raw input: what a person may type into |
| 10 | // a cell, and what import may write when it has to wrap text to keep |
| 11 | // it text. A formula or a label, not a document. |
| 12 | const MaxInputBytes = 8 << 10 |
| 13 | |
| 14 | // TextInput returns the raw input that stores s as the text s, and |
| 15 | // whether that took a wrapper. |
| 16 | // |
| 17 | // Raw input has one channel. contentNode reads "" as blank, a leading |
| 18 | // "=" as a formula, and anything strconv.ParseFloat or TRUE/FALSE |
| 19 | // accepts as a number or a boolean, so text such as "00123", "TRUE", |
| 20 | // "NaN", "=total" or "" cannot be written as itself without becoming |
| 21 | // something else. Those become a constant string expression, ="...", |
| 22 | // with every quote doubled: the existing parser reads it back as |
| 23 | // exactly s, its value is the text s, and an editor shows the |
| 24 | // expression rather than a number nobody typed. |
| 25 | // |
| 26 | // Nothing here changes how existing input is read. The wrapper is an |
| 27 | // ordinary formula any person could have typed; the decision whether |
| 28 | // a string needs one is contentNode's own, asked rather than restated, |
| 29 | // so the two cannot drift apart. |
| 30 | func TextInput(s string) (raw string, wrapped bool) { |
| 31 | node, blank, err := contentNode(s) |
| 32 | if err == nil && !blank { |
| 33 | if str, ok := node.(*calc.Str); ok && str.V == s { |
| 34 | return s, false |
| 35 | } |
| 36 | } |
| 37 | return `="` + strings.ReplaceAll(s, `"`, `""`) + `"`, true |
| 38 | } |
| 39 | |