shithub: libmujs

Download patch

ref: 8e917e8c2786c31ab1a8209f429286afcc4d1ebc
parent: bc6da35e59c59afc599e67ec45b9f67cfcdeca84
author: Tor Andersson <[email protected]>
date: Thu Feb 6 18:40:38 EST 2014

String.trim()

--- a/jsstring.c
+++ b/jsstring.c
@@ -262,6 +262,25 @@
 	return 1;
 }
 
+static int iswhite(int c)
+{
+	return c == 0x9 || c == 0xB || c == 0xC || c == 0x20 || c == 0xA0 || c == 0xFEFF ||
+		c == 0xA || c == 0xD || c == 0x2028 || c == 0x2029;
+}
+
+static int Sp_trim(js_State *J, int argc)
+{
+	const char *s, *e;
+	s = js_tostring(J, 0);
+	while (iswhite(*s))
+		++s;
+	e = s + strlen(s);
+	while (e > s && iswhite(e[-1]))
+		--e;
+	js_pushlstring(J, s, e - s);
+	return 1;
+}
+
 static int S_fromCharCode(js_State *J, int argc)
 {
 	int i;
@@ -655,7 +674,9 @@
 		jsB_propf(J, "toLocaleLowerCase", Sp_toLowerCase, 0);
 		jsB_propf(J, "toUpperCase", Sp_toUpperCase, 0);
 		jsB_propf(J, "toLocaleUpperCase", Sp_toUpperCase, 0);
-		// trim (ES5)
+
+		/* ES5 */
+		jsB_propf(J, "trim", Sp_trim, 0);
 	}
 	js_newcconstructor(J, jsB_String, jsB_new_String, 1);
 	{