ComputePositionMap builds a PositionMap for the given text.
(text string)
| 38 | |
| 39 | // ComputePositionMap builds a PositionMap for the given text. |
| 40 | func ComputePositionMap(text string) *PositionMap { |
| 41 | pm := &PositionMap{} |
| 42 | delta := 0 |
| 43 | for i := 0; i < len(text); { |
| 44 | b := text[i] |
| 45 | if b < utf8.RuneSelf { |
| 46 | i++ |
| 47 | continue |
| 48 | } |
| 49 | r, size := stringutil.DecodeJSStringRune(text[i:]) |
| 50 | utf16Size := 1 |
| 51 | if r >= 0x10000 { |
| 52 | utf16Size = 2 |
| 53 | } |
| 54 | delta += size - utf16Size |
| 55 | pm.entries = append(pm.entries, positionMapEntry{utf8Pos: i + size, delta: delta}) |
| 56 | i += size |
| 57 | } |
| 58 | pm.asciiOnly = len(pm.entries) == 0 |
| 59 | return pm |
| 60 | } |
| 61 | |
| 62 | // IsAsciiOnly returns true if the text is ASCII-only, |
| 63 | // meaning UTF-8 and UTF-16 offsets are identical. |