Mercurial > repos > tabletprog
comparison modules/parser.tp @ 243:5b830147c1cd
Use a lightweight substring object in a few places in the parser to improve performance for large files.
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 05 Jan 2014 23:07:26 -0800 |
parents | 0e7982adc76b |
children | ae5188be523e |
comparison
equal
deleted
inserted
replaced
242:0e7982adc76b | 243:5b830147c1cd |
---|---|
1 { | 1 { |
2 light:from:withLength <- :_base :_start :_len { | |
3 if: (not: (_base isBasicString?)) { | |
4 _start <- _start + (_base start) | |
5 _base <- _base base | |
6 } | |
7 _needsflat? <- true | |
8 _flat <- false | |
9 #{ | |
10 //TODO: UTF-8 support | |
11 length <- { _len } | |
12 byte_length <- { _len } | |
13 string <- { | |
14 if: _needsflat? { | |
15 _flat <- _base from: _start withLength: _len | |
16 } | |
17 _flat | |
18 } | |
19 from:withLength <- :s :l { | |
20 if: (l + s) > _len { | |
21 l <- _len - s | |
22 } | |
23 _base from: (_start + s) withLength: l | |
24 } | |
25 from <- :s { | |
26 from: s withLength: (_len - s) | |
27 } | |
28 byte <- :index { | |
29 _base byte: (index + _start) | |
30 } | |
31 = <- :other { | |
32 if: (other length) = _len { | |
33 ostart <- 0 | |
34 if: (not: (other isBasicString?)) { | |
35 ostart <- other start | |
36 other <- other _base | |
37 } | |
38 res <- _base compareSub: other _start ostart _len | |
39 res = 0 | |
40 } | |
41 } | |
42 . <- :other { | |
43 (string: self) . other | |
44 } | |
45 int32 <- { | |
46 (string: self) int32 | |
47 } | |
48 splitOn <- :delim { | |
49 (string: self) splitOn: delim | |
50 } | |
51 isString? <- { true } | |
52 isBasicString? <- { false } | |
53 base <- { _base } | |
54 start <- { _start } | |
55 } | |
56 } | |
57 | |
58 light:from <- :base :start { | |
59 light: base from: start withLength: (base length) - start | |
60 } | |
2 _applyMatch <- :fun tomatch { | 61 _applyMatch <- :fun tomatch { |
3 fun: tomatch | 62 fun: tomatch |
4 } | 63 } |
5 _matchString <- :str tomatch { | 64 _matchString <- :str tomatch { |
6 if: (tomatch isString?) { | 65 if: (tomatch isString?) { |
8 false | 67 false |
9 } else: { | 68 } else: { |
10 if: (tomatch length) > (str length) { | 69 if: (tomatch length) > (str length) { |
11 tomatch <- tomatch from: 0 withLength: (str length) | 70 tomatch <- tomatch from: 0 withLength: (str length) |
12 } | 71 } |
13 if: str = tomatch { | 72 if: tomatch = str { |
14 #{ | 73 #{ |
15 if <- :self trueblock { | 74 if <- :self trueblock { |
16 trueblock: | 75 trueblock: |
17 } | 76 } |
18 ifnot <- :self falseblock { | 77 ifnot <- :self falseblock { |
60 valid? <- { true } | 119 valid? <- { true } |
61 matchcall <- quote: (_applyMatch: :tomatch { | 120 matchcall <- quote: (_applyMatch: :tomatch { |
62 lm <- left | 121 lm <- left |
63 if: lm { | 122 if: lm { |
64 orig <- tomatch | 123 orig <- tomatch |
65 tomatch <- tomatch from: (lm matchlen) | 124 tomatch <- light: tomatch from: (lm matchlen) |
66 rm <- right | 125 rm <- right |
67 if: rm { | 126 if: rm { |
68 total <- (rm matchlen) + (lm matchlen) | 127 total <- (rm matchlen) + (lm matchlen) |
69 #{ | 128 #{ |
70 if <- :self trueblock { | 129 if <- :self trueblock { |
121 while: { _match && cur < n } do: { | 180 while: { _match && cur < n } do: { |
122 res <- mcall | 181 res <- mcall |
123 _match <- if: res { | 182 _match <- if: res { |
124 count <- count + 1 | 183 count <- count + 1 |
125 //TODO: Use some kind of lightweight substring wrapper here | 184 //TODO: Use some kind of lightweight substring wrapper here |
126 tomatch <- tomatch from: (res matchlen) | 185 tomatch <- light: tomatch from: (res matchlen) |
127 if: allBasic? { | 186 if: allBasic? { |
128 ifnot: (res basicYield?) { | 187 ifnot: (res basicYield?) { |
129 allBasic? <- false | 188 allBasic? <- false |
130 if: cur > 0 { | 189 if: cur > 0 { |
131 yieldvals <- (orig from: 0 withLength: cur) | yieldvals | 190 yieldvals <- (orig from: 0 withLength: cur) | yieldvals |