shithub: libmujs

Download patch

ref: bc845cbd64a80429e0785dd4d28bed56561fdbe0
parent: 93cc0584dfd56f0d65ff4f1185ba5f85cc4656da
author: Tor Andersson <tor.andersson@artifex.com>
date: Mon Sep 22 11:47:04 EDT 2014

Optimize lexnumber when using strtod.

Keep the old--fast but inaccurate--version in #ifdef 0.

--- a/jslex.c
+++ b/jslex.c
@@ -239,6 +239,8 @@
 	return n;
 }
 
+#if 0
+
 static double lexinteger(js_State *J)
 {
 	double n = 0;
@@ -277,7 +279,6 @@
 
 static int lexnumber(js_State *J)
 {
-	const char *s = J->source - 1;
 	double n;
 	double e;
 
@@ -310,9 +311,57 @@
 	if (jsY_isidentifierstart(PEEK))
 		jsY_error(J, "number with letter suffix");
 
+	J->number = n;
+	return TK_NUMBER;
+}
+
+#else
+
+static int lexnumber(js_State *J)
+{
+	const char *s = J->source - 1;
+
+	if (ACCEPT('0')) {
+		if (ACCEPT('x') || ACCEPT('X')) {
+			J->number = lexhex(J);
+			return TK_NUMBER;
+		}
+		if (jsY_isdec(PEEK))
+			jsY_error(J, "number with leading zero");
+		if (ACCEPT('.')) {
+			while (jsY_isdec(PEEK))
+				NEXT();
+		}
+	} else if (ACCEPT('.')) {
+		if (!jsY_isdec(PEEK))
+			return '.';
+		while (jsY_isdec(PEEK))
+			NEXT();
+	} else {
+		while (jsY_isdec(PEEK))
+			NEXT();
+		if (ACCEPT('.')) {
+			while (jsY_isdec(PEEK))
+				NEXT();
+		}
+	}
+
+	if (ACCEPT('e') || ACCEPT('E')) {
+		if (PEEK == '-' || PEEK == '+')
+			NEXT();
+		while (jsY_isdec(PEEK))
+			NEXT();
+	}
+
+	if (jsY_isidentifierstart(PEEK))
+		jsY_error(J, "number with letter suffix");
+
 	J->number = js_strtod(s, NULL);
 	return TK_NUMBER;
+
 }
+
+#endif
 
 static int lexescape(js_State *J)
 {