[Commit] nickle ChangeLog,1.43,1.44 string.5c,1.7,1.8

Bart Massey commit at keithp.com
Thu May 13 15:17:23 PDT 2004


Committed by: bart

Update of /local/src/CVS/nickle
In directory home.keithp.com:/tmp/cvs-serv19350

Modified Files:
	ChangeLog string.5c 
Log Message:
Rebuild dequote() and parse_csv() to handle
quote contexts using generator functions.



Index: ChangeLog
===================================================================
RCS file: /local/src/CVS/nickle/ChangeLog,v
retrieving revision 1.43
retrieving revision 1.44
diff -u -d -r1.43 -r1.44
--- a/ChangeLog	13 May 2004 21:16:52 -0000	1.43
+++ b/ChangeLog	13 May 2004 22:17:20 -0000	1.44
@@ -1,5 +1,11 @@
 2004-05-13  Bart Massey  <bart at cs.pdx.edu>
 
+	* string.5c:
+	Rebuild dequote() and parse_csv() to handle
+	quote contexts using generator functions.
+
+2004-05-13  Bart Massey  <bart at cs.pdx.edu>
+
 	* builtin-toplevel.c:
 	* nickle.h:
 	Rename is_defined() to is_uninit().

Index: string.5c
===================================================================
RCS file: /local/src/CVS/nickle/string.5c,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- a/string.5c	16 Apr 2004 18:19:08 -0000	1.7
+++ b/string.5c	13 May 2004 22:17:20 -0000	1.8
@@ -58,102 +58,110 @@
 
     typedef enum {normal, openq, xq} qstate;
 
-    public string _dequote(string s, quote_context q) {
-	string result = "";
-	qstate t = qstate.normal;
-	int c;
-	for (int cur = 0; cur < length(s); cur++) {
-	    c = s[cur];
-	    switch(t) {
-	    case qstate.normal:
-		if (inchars(c, q.oq)) {
+    typedef string(string) dequote_t;
+    
+    public dequote_t make_dequote(quote_context q) {
+	public string _dequote(string s) {
+	    string result = "";
+	    qstate t = qstate.normal;
+	    int c;
+	    for (int cur = 0; cur < length(s); cur++) {
+		c = s[cur];
+		switch(t) {
+		case qstate.normal:
+		    if (inchars(c, q.oq)) {
+			t = qstate.openq;
+			continue;
+		    }
+		    raise invalid_argument("leading garbage", 0, s);
+		    break;
+		case qstate.openq:
+		    if (inchars(c, q.qq)) {
+			t = qstate.xq;
+			continue;
+		    }
+		    if (inchars(c, q.cq)) {
+			if (cur != length(s) - 1)
+			    raise invalid_argument("trailing garbage", 0, s);
+			t = qstate.normal;
+			continue;
+		    }
+		    break;
+		case qstate.xq:
 		    t = qstate.openq;
-		    continue;
-		}
-		raise invalid_argument("_dequote: leading garbage", 0, s);
-		break;
-	    case qstate.openq:
-		if (inchars(c, q.qq)) {
-		    t = qstate.xq;
-		    continue;
-		}
-		if (inchars(c, q.cq)) {
-		    if (cur != length(s) - 1)
-			raise invalid_argument("_dequote: trailing garbage", 0, s);
-		    t = qstate.normal;
-		    continue;
+		    break;
 		}
-		break;
-	    case qstate.xq:
-		t = qstate.openq;
-		break;
+		result = result + String::new(c);
 	    }
-	    result = result + String::new(c);
+	    if (t == qstate.normal || t == qstate.xq && inchars(c, q.cq))
+		return result;
+	    raise invalid_argument("unexpected end of string", 0, s);
 	}
-	if (t == qstate.normal || t == qstate.xq && inchars(c, q.cq))
-	    return result;
-	raise invalid_argument("_dequote: unexpected end of string", 0, s);
+	return _dequote;
     }
 
-    public quote_context default_quotes = {
-	oq = "\"", cq = "\"", qq = "\\"
-    };
+    public dequote_t dequote = make_dequote(
+       (quote_context) {oq = "\"", cq = "\"", qq = "\\"}
+    );
 
-    public string dequote(string s) {
-	return _dequote(s, default_quotes);
-    }
+    typedef (string[*])(string) parse_csv_t;
 
-    public string[*] parse_csv(string s) {
-	string[...] ss = {};
-	quote_context q = default_quotes;
-	q.qq = "\"";
-	string curs = "";
-	void consume() {
-	    string t = chomp(curs);
-	    if (t != "" && t[0] == '"')
-		t = _dequote(t, q);
-	    ss[dim(ss)] = t;
-	    curs = "";
-	}
-	qstate t = qstate.normal;
-	int cur = 0;
-	while (cur < length(s)) {
-	    int c = s[cur];
-#	    printf("%d: %c %v\n", cur, c, t);
-	    switch(t) {
-	    case qstate.normal:
-		if (c == ',') {
-		    consume();
-		    cur++;
-		    continue;
-		}
-		if (c == '"')
-		    t = qstate.openq;
-		break;
-	    case qstate.openq:
-		if (c == '"')
-		    t = qstate.xq;
-		break;
-	    case qstate.xq:
-		if (c == '"') {
-		    t = qstate.openq;
+    public parse_csv_t make_parse_csv(quote_context q) {
+	dequote_t dq = make_dequote(q);
+	public string[*] _parse_csv(string s) {
+	    string[...] ss = {};
+	    string curs = "";
+	    void consume() {
+		string t = chomp(curs);
+		if (t != "" && t[0] == '"')
+		    t = dq(t);
+		ss[dim(ss)] = t;
+		curs = "";
+	    }
+	    qstate t = qstate.normal;
+	    int cur = 0;
+	    while (cur < length(s)) {
+		int c = s[cur];
+		switch(t) {
+		case qstate.normal:
+		    if (c == ',') {
+			consume();
+			cur++;
+			continue;
+		    }
+		    if (c == '"')
+			t = qstate.openq;
 		    break;
-		}
-		if (c == ',') {
+		case qstate.openq:
+		    if (c == '"')
+			t = qstate.xq;
+		    break;
+		case qstate.xq:
+		    if (c == '"') {
+			t = qstate.openq;
+			break;
+		    }
+		    if (c == ',') {
+			t = qstate.normal;
+			continue;
+		    }
 		    t = qstate.normal;
-		    continue;
+		    break;
 		}
-		t = qstate.normal;
-		break;
+		curs = curs + String::new(c);
+		cur++;
 	    }
-	    curs = curs + String::new(c);
-	    cur++;
+	    if (t == qstate.openq)
+		raise invalid_argument("parse_csv: unexpected " +
+				       "end of csv line", 0, s);
+	    consume();
+	    return ss;
 	}
-	if (t == qstate.openq)
-	    raise invalid_argument("parse_csv: unexpected " +
-				   "end of csv line", 0, s);
-	consume();
-	return ss;
+	return _parse_csv;
     }
 
+    public parse_csv_t parse_csv = make_parse_csv(
+       (quote_context) {oq = "\"", cq = "\"", qq = "\""}
+    );
+
 }




More information about the Commit mailing list