shithub: libmujs

Download patch

ref: 58421f16017721fd155fb471e28c3044ffe01aee
parent: f3ea55fc6440ca9bf08e472f2d5e1f14ee0f4273
author: Tor Andersson <[email protected]>
date: Sun Jan 19 10:24:03 EST 2014

Optimize String.prototype.charAt and charCodeAt.

--- a/jsbstring.c
+++ b/jsbstring.c
@@ -32,20 +32,19 @@
 	return 1;
 }
 
-static inline const char *utfindex(const char *s, int i)
+static inline Rune runeAt(const char *s, int i)
 {
-	Rune rune;
-	int c, n = 0;
-	for (n = 0; n < i; ++n) {
-		c = *(unsigned char*)s;
-		if (c < Runeself) {
-			if (c == 0)
-				return NULL;
+	Rune rune = 0;
+	while (i-- >= 0) {
+		rune = *(unsigned char*)s;
+		if (rune < Runeself) {
+			if (rune == 0)
+				return 0;
 			++s;
 		} else
 			s += chartorune(&rune, s);
 	}
-	return s;
+	return rune;
 }
 
 static int Sp_charAt(js_State *J, int n)
@@ -53,10 +52,8 @@
 	char buf[UTFmax + 1];
 	const char *s = js_tostring(J, 0);
 	int pos = js_tointeger(J, 1);
-	s = utfindex(s, pos);
-	if (s) {
-		Rune rune;
-		chartorune(&rune, s);
+	Rune rune = runeAt(s, pos);
+	if (rune > 0) {
 		buf[runetochar(buf, &rune)] = 0;
 		js_pushstring(J, buf);
 	} else {
@@ -69,14 +66,11 @@
 {
 	const char *s = js_tostring(J, 0);
 	int pos = js_tointeger(J, 1);
-	s = utfindex(s, pos);
-	if (s) {
-		Rune rune;
-		chartorune(&rune, s);
+	Rune rune = runeAt(s, pos);
+	if (rune > 0)
 		js_pushnumber(J, rune);
-	} else {
+	else
 		js_pushnumber(J, NAN);
-	}
 	return 1;
 }