| 1 | package markup |
| 2 | |
| 3 | import "strings" |
| 4 | |
| 5 | // scan is one text's tables for the link tests, each built in one pass |
| 6 | // on first use: next(stop, at) is the first index at or after at where |
| 7 | // the stop holds, or -1. With them a link test costs the same wherever |
| 8 | // it starts. Scanning from each [ for its ] or ]] was quadratic: a line |
| 9 | // of 40,000 "[a" took over a minute to draw (review M6). |
| 10 | // editor/markup.mjs's Scan is the same, and gate L4 holds them together. |
| 11 | type scan struct { |
| 12 | text string |
| 13 | tables [stopCount][]int32 |
| 14 | } |
| 15 | |
| 16 | type stop int |
| 17 | |
| 18 | const ( |
| 19 | // A web link's text ends at the first ] or line end, its address at |
| 20 | // the first ), space or line end: [^\]\n]+ and [^)\s]+. |
| 21 | stopLabel stop = iota |
| 22 | stopAddress |
| 23 | // A wiki link ends at the first ]], unless a [ or a line end is |
| 24 | // first. |
| 25 | stopClose |
| 26 | stopOpen |
| 27 | stopCount |
| 28 | ) |
| 29 | |
| 30 | func newScan(text string) *scan { return &scan{text: text} } |
| 31 | |
| 32 | // holds is whether the stop is at text[at]. Every stop is ASCII, so |
| 33 | // testing bytes is testing runes. |
| 34 | func (scanned *scan) holds(which stop, at int) bool { |
| 35 | text := scanned.text |
| 36 | switch which { |
| 37 | case stopLabel: |
| 38 | return text[at] == ']' || text[at] == '\n' |
| 39 | case stopAddress: |
| 40 | return strings.IndexByte(")\t\n\f\r ", text[at]) >= 0 |
| 41 | case stopClose: |
| 42 | return text[at] == ']' && at+1 < len(text) && text[at+1] == ']' |
| 43 | } |
| 44 | return text[at] == '[' || text[at] == '\n' |
| 45 | } |
| 46 | |
| 47 | func (scanned *scan) next(which stop, at int) int { |
| 48 | if at >= len(scanned.text) { |
| 49 | return -1 |
| 50 | } |
| 51 | if scanned.tables[which] == nil { |
| 52 | table := make([]int32, len(scanned.text)) |
| 53 | next := int32(-1) |
| 54 | for index := len(scanned.text) - 1; index >= 0; index-- { |
| 55 | if scanned.holds(which, index) { |
| 56 | next = int32(index) |
| 57 | } |
| 58 | table[index] = next |
| 59 | } |
| 60 | scanned.tables[which] = table |
| 61 | } |
| 62 | return int(scanned.tables[which][at]) |
| 63 | } |
| 64 | |
| 65 | // webLink is ^\[([^\]\n]+)\]\(([^)\s]+)\) at index: neither class |
| 66 | // holds the character that ends it, so there is one way to match. |
| 67 | func (scanned *scan) webLink(index int) (label, address string, |
| 68 | ok bool) { |
| 69 | text := scanned.text |
| 70 | end := scanned.next(stopLabel, index+1) |
| 71 | if end <= index+1 || text[end] != ']' || end+1 >= len(text) || |
| 72 | text[end+1] != '(' { |
| 73 | return "", "", false |
| 74 | } |
| 75 | closer := scanned.next(stopAddress, end+2) |
| 76 | if closer <= end+2 || text[closer] != ')' { |
| 77 | return "", "", false |
| 78 | } |
| 79 | return text[index+1 : end], text[end+2 : closer], true |
| 80 | } |
| 81 | |
| 82 | // closing is how far from at the first "]]" is, or -1 when there is |
| 83 | // none, or a [ or a line end comes before it: a link on its own line. |
| 84 | func (scanned *scan) closing(at int) int { |
| 85 | end := scanned.next(stopClose, at) |
| 86 | if end < 0 { |
| 87 | return -1 |
| 88 | } |
| 89 | if open := scanned.next(stopOpen, at); open >= 0 && open < end { |
| 90 | return -1 |
| 91 | } |
| 92 | return end - at |
| 93 | } |
| 94 | |