shithub: libmujs

Download patch

ref: 46dec712a71b4155f66b40ac92eb796d7e1fab89
parent: 31acf8517f2b51f01dbdd528a8352033015f754e
author: Tor Andersson <[email protected]>
date: Mon Feb 10 08:17:09 EST 2014

Simplify string-buffer function API.

--- a/jsbuiltin.c
+++ b/jsbuiltin.c
@@ -68,7 +68,7 @@
 
 static int Encode(js_State *J, const char *str, const char *unescaped)
 {
-	struct sbuffer *sb = NULL;
+	js_Buffer *sb = NULL;
 
 	static const char *HEX = "0123456789ABCDEF";
 
@@ -75,14 +75,14 @@
 	while (*str) {
 		int c = (unsigned char) *str++;
 		if (strchr(unescaped, c))
-			sb = sb_putc(sb, c);
+			sb_putc(&sb, c);
 		else {
-			sb = sb_putc(sb, '%');
-			sb = sb_putc(sb, HEX[(c >> 4) & 0xf]);
-			sb = sb_putc(sb, HEX[c & 0xf]);
+			sb_putc(&sb, '%');
+			sb_putc(&sb, HEX[(c >> 4) & 0xf]);
+			sb_putc(&sb, HEX[c & 0xf]);
 		}
 	}
-	sb = sb_putc(sb, 0);
+	sb_putc(&sb, 0);
 
 	if (js_try(J)) {
 		free(sb);
@@ -109,13 +109,13 @@
 
 static int Decode(js_State *J, const char *str, const char *reserved)
 {
-	struct sbuffer *sb = NULL;
+	js_Buffer *sb = NULL;
 	int a, b;
 
 	while (*str) {
 		int c = (unsigned char) *str++;
 		if (c != '%')
-			sb = sb_putc(sb, c);
+			sb_putc(&sb, c);
 		else {
 			if (!str[0] || !str[1])
 				js_urierror(J, "truncated escape sequence");
@@ -125,15 +125,15 @@
 				js_urierror(J, "invalid escape sequence");
 			c = tohex(a) << 4 | tohex(b);
 			if (!strchr(reserved, c))
-				sb = sb_putc(sb, c);
+				sb_putc(&sb, c);
 			else {
-				sb = sb_putc(sb, '%');
-				sb = sb_putc(sb, a);
-				sb = sb_putc(sb, b);
+				sb_putc(&sb, '%');
+				sb_putc(&sb, a);
+				sb_putc(&sb, b);
 			}
 		}
 	}
-	sb = sb_putc(sb, 0);
+	sb_putc(&sb, 0);
 
 	if (js_try(J)) {
 		free(sb);
--- a/jsbuiltin.h
+++ b/jsbuiltin.h
@@ -17,33 +17,33 @@
 void jsB_propn(js_State *J, const char *name, double number);
 void jsB_props(js_State *J, const char *name, const char *string);
 
-struct sbuffer { int n, m; char s[64]; };
+typedef struct js_Buffer { int n, m; char s[64]; } js_Buffer;
 
-static struct sbuffer *sb_putc(struct sbuffer *sb, int c)
+static void sb_putc(js_Buffer **sbp, int c)
 {
+	js_Buffer *sb = *sbp;
 	if (!sb) {
 		sb = malloc(sizeof *sb);
 		sb->n = 0;
 		sb->m = sizeof sb->s;
+		*sbp = sb;
 	} else if (sb->n == sb->m) {
-		sb = realloc(sb, (sb->m *= 2) + offsetof(struct sbuffer, s));
+		sb = realloc(sb, (sb->m *= 2) + offsetof(js_Buffer, s));
+		*sbp = sb;
 	}
 	sb->s[sb->n++] = c;
-	return sb;
 }
 
-static inline struct sbuffer *sb_puts(struct sbuffer *sb, const char *s)
+static inline void sb_puts(js_Buffer **sb, const char *s)
 {
 	while (*s)
-		sb = sb_putc(sb, *s++);
-	return sb;
+		sb_putc(sb, *s++);
 }
 
-static inline struct sbuffer *sb_putm(struct sbuffer *sb, const char *s, const char *e)
+static inline void sb_putm(js_Buffer **sb, const char *s, const char *e)
 {
 	while (s < e)
-		sb = sb_putc(sb, *s++);
-	return sb;
+		sb_putc(sb, *s++);
 }
 
 #endif
--- a/jsfunction.c
+++ b/jsfunction.c
@@ -7,7 +7,7 @@
 static int jsB_Function(js_State *J, int argc)
 {
 	const char *source;
-	struct sbuffer *sb;
+	js_Buffer *sb;
 	js_Ast *parse;
 	js_Function *fun;
 	int i;
@@ -19,10 +19,10 @@
 		sb = NULL;
 		if (argc > 1) {
 			for (i = 1; i < argc; ++i) {
-				if (i > 1) sb = sb_putc(sb, ',');
-				sb = sb_puts(sb, js_tostring(J, i));
+				if (i > 1) sb_putc(&sb, ',');
+				sb_puts(&sb, js_tostring(J, i));
 			}
-			sb = sb_putc(sb, ')');
+			sb_putc(&sb, ')');
 		}
 	}
 
--- a/jsstring.c
+++ b/jsstring.c
@@ -393,7 +393,7 @@
 	js_Regexp *re;
 	regmatch_t m[10];
 	const char *source, *s, *r;
-	struct sbuffer *sb = NULL;
+	js_Buffer *sb = NULL;
 	int n, x;
 
 	source = js_tostring(J, 0);
@@ -419,39 +419,39 @@
 		js_copy(J, 0); /* arg x+3: search string */
 		js_call(J, 2 + x);
 		r = js_tostring(J, -1);
-		sb = sb_putm(sb, source, s);
-		sb = sb_puts(sb, r);
+		sb_putm(&sb, source, s);
+		sb_puts(&sb, r);
 		js_pop(J, 1);
 	} else {
 		r = js_tostring(J, 2);
-		sb = sb_putm(sb, source, s);
+		sb_putm(&sb, source, s);
 		while (*r) {
 			if (*r == '$') {
 				switch (*(++r)) {
-				case '$': sb = sb_putc(sb, '$'); break;
-				case '`': sb = sb_putm(sb, source, s); break;
-				case '\'': sb = sb_puts(sb, s + n); break;
+				case '$': sb_putc(&sb, '$'); break;
+				case '`': sb_putm(&sb, source, s); break;
+				case '\'': sb_puts(&sb, s + n); break;
 				case '&':
-					sb = sb_putm(sb, s, s + n);
+					sb_putm(&sb, s, s + n);
 					break;
 				case '0': case '1': case '2': case '3': case '4':
 				case '5': case '6': case '7': case '8': case '9':
 					x = *r - '0';
 					if (m[x].rm_so >= 0) {
-						sb = sb_putm(sb, source + m[x].rm_so, source + m[x].rm_eo);
+						sb_putm(&sb, source + m[x].rm_so, source + m[x].rm_eo);
 					} else {
-						sb = sb_putc(sb, '$');
-						sb = sb_putc(sb, '0'+x);
+						sb_putc(&sb, '$');
+						sb_putc(&sb, '0'+x);
 					}
 					break;
 				default:
-					sb = sb_putc(sb, '$');
-					sb = sb_putc(sb, *r);
+					sb_putc(&sb, '$');
+					sb_putc(&sb, *r);
 					break;
 				}
 				++r;
 			} else {
-				sb = sb_putc(sb, *r++);
+				sb_putc(&sb, *r++);
 			}
 		}
 	}
@@ -460,7 +460,7 @@
 		source = source + m[0].rm_eo;
 		if (n == 0) {
 			if (*source)
-				sb = sb_putc(sb, *source++);
+				sb_putc(&sb, *source++);
 			else
 				goto end;
 		}
@@ -469,8 +469,8 @@
 	}
 
 end:
-	sb = sb_puts(sb, s + n);
-	sb = sb_putc(sb, 0);
+	sb_puts(&sb, s + n);
+	sb_putc(&sb, 0);
 
 	if (js_try(J)) {
 		free(sb);
@@ -485,7 +485,7 @@
 static int Sp_replace_string(js_State *J, int argc)
 {
 	const char *source, *needle, *s, *r;
-	struct sbuffer *sb = NULL;
+	js_Buffer *sb = NULL;
 	int n;
 
 	source = js_tostring(J, 0);
@@ -506,30 +506,30 @@
 		js_copy(J, 0); /* arg 3: search string */
 		js_call(J, 3);
 		r = js_tostring(J, -1);
-		sb = sb_putm(sb, source, s);
-		sb = sb_puts(sb, r);
-		sb = sb_puts(sb, s + n);
-		sb = sb_putc(sb, 0);
+		sb_putm(&sb, source, s);
+		sb_puts(&sb, r);
+		sb_puts(&sb, s + n);
+		sb_putc(&sb, 0);
 		js_pop(J, 1);
 	} else {
 		r = js_tostring(J, 2);
-		sb = sb_putm(sb, source, s);
+		sb_putm(&sb, source, s);
 		while (*r) {
 			if (*r == '$') {
 				switch (*(++r)) {
-				case '$': sb = sb_putc(sb, '$'); break;
-				case '&': sb = sb_putm(sb, s, s + n); break;
-				case '`': sb = sb_putm(sb, source, s); break;
-				case '\'': sb = sb_puts(sb, s + n); break;
-				default: sb = sb_putc(sb, '$'); sb = sb_putc(sb, *r); break;
+				case '$': sb_putc(&sb, '$'); break;
+				case '&': sb_putm(&sb, s, s + n); break;
+				case '`': sb_putm(&sb, source, s); break;
+				case '\'': sb_puts(&sb, s + n); break;
+				default: sb_putc(&sb, '$'); sb_putc(&sb, *r); break;
 				}
 				++r;
 			} else {
-				sb = sb_putc(sb, *r++);
+				sb_putc(&sb, *r++);
 			}
 		}
-		sb = sb_puts(sb, s + n);
-		sb = sb_putc(sb, 0);
+		sb_puts(&sb, s + n);
+		sb_putc(&sb, 0);
 	}
 
 	if (js_try(J)) {