changeset 237:dae093baf36c

Optimized implementation of character classes
author Mike Pavone <pavone@retrodev.com>
date Sun, 05 Jan 2014 17:00:33 -0800
parents c463a891ccd3
children 3bfc00e4f5e5
files modules/parser.tp
diffstat 1 files changed, 21 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/modules/parser.tp	Fri Jan 03 22:13:34 2014 -0800
+++ b/modules/parser.tp	Sun Jan 05 17:00:33 2014 -0800
@@ -174,29 +174,35 @@
 		eval: rawchars :chars {
 			orig <- chars
 			chars <- expandClass: chars
-			//TODO: Use a more sophisticated approach for large classes
+			charmap <- ""
+			char <- 0
+			while: { char < 256 } do: {
+				mchar <- 0
+				found <- false
+				while: { mchar < (chars byte_length)} do: {
+					if: (chars byte: mchar) = char {
+						found <- true
+						mchar <- chars byte_length
+					}
+					mchar <- mchar + 1
+				}
+				charmap <- charmap . (if: found { "t" } else: { "f" })
+				char <- char + 1
+			}
+			t <- "t" byte: 0
 			quote: :tomatch {
 				if: (tomatch isString?) {
-					check <- 0
-
-					nomatch <- true
-					while: { nomatch && check < (chars byte_length) } do: {
-						if: (tomatch byte: 0) = (chars byte: check) {
-							nomatch <- false
-						}
-						check <- check + 1
-					}
-					if: nomatch {
-						#{
-							matched? <- { false }
-						}
-					} else: {
+					if: (charmap byte: (tomatch byte: 0)) = t {
 						#{
 							matched? <- { true }
 							matchlen <- { 1 }
 							basicYield? <- { true }
 							yield <- { tomatch from: 0 withLength: 1 }
 						}
+					} else: {
+						#{
+							matched? <- { false }
+						}
 					}
 				} else: {
 					#{