[Commit] nickle ChangeLog, 1.36, 1.37 builtin-string.c, 1.8, 1.9 string.5c, 1.4, 1.5

Bart Massey commit at keithp.com
Fri Apr 16 02:34:33 PDT 2004


Committed by: bart

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

Modified Files:
	ChangeLog builtin-string.c string.5c 
Log Message:
	* builtin-string.c: (do_String_substr):
	Allow a zero-length substr() at the end of the string.
	
	* string.5c:
	Clean up a bunch of substr() references.  Remove
	accidental redundant code.
	


Index: ChangeLog
===================================================================
RCS file: /local/src/CVS/nickle/ChangeLog,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -d -r1.36 -r1.37
--- a/ChangeLog	16 Apr 2004 08:24:58 -0000	1.36
+++ b/ChangeLog	16 Apr 2004 09:34:30 -0000	1.37
@@ -1,5 +1,14 @@
 2004-04-15  Bart Massey  <bart at cs.pdx.edu>
 
+	* builtin-string.c: (do_String_substr):
+	Allow a zero-length substr() at the end of the string.
+	
+	* string.5c:
+	Clean up a bunch of substr() references.  Remove
+	accidental redundant code.
+	
+2004-04-15  Bart Massey  <bart at cs.pdx.edu>
+
 	* file.c: (FilePuts):
 	Handle emitting backslash in quoted string properly.
 	

Index: builtin-string.c
===================================================================
RCS file: /local/src/CVS/nickle/builtin-string.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- a/builtin-string.c	16 Apr 2004 05:26:44 -0000	1.8
+++ b/builtin-string.c	16 Apr 2004 09:34:30 -0000	1.9
@@ -131,7 +131,7 @@
 	b += c;
 	c = -c;
     }
-    if (b < 0 || b >= al)
+    if (b < 0 || b > al)
     {
 	RaiseStandardException (exception_invalid_argument,
 				"substr: index out of range",

Index: string.5c
===================================================================
RCS file: /local/src/CVS/nickle/string.5c,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- a/string.5c	16 Apr 2004 07:46:02 -0000	1.4
+++ b/string.5c	16 Apr 2004 09:34:30 -0000	1.5
@@ -1,122 +1,5 @@
 extend namespace String {
 
-    public string chomp(string s) {
-	if (s == "")
-	    return s;
-	int l = length(s);
-	int i = 0;
-	while(i < l && Ctype::isspace(s[i]))
-	    i++;
-	int j = l - 1;
-	while(j >= 0 && Ctype::isspace(s[j]))
-	    --j;
-	return substr(s, i, j - i + 1);
-    }
-
-    typedef enum {normal, openq, xq} qstate;
-
-    public bool inchars(int c, string s) {
-	return index(String::new(c), s) >= 0;
-    }
-
-    public typedef struct { 
-	string oq, cq, qq;
-    } quote_context;
-
-    public string _dequote(string s, quote_context q) {
-	string result = "";
-	qstate t = qstate.normal;
-	for (int cur = 0; cur < length(s); cur++) {
-	    int c = s[cur];
-	    switch(t) {
-	    case qstate.normal:
-		if (inchars(c, q.oq)) {
-		    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);
-		    continue;
-		}
-		break;
-	    case qstate.xq:
-		t = qstate.openq;
-		break;
-	    }
-	    result = result + String::new(c);
-	}
-	if (q.qq == q.cq && t == qstate.xq)
-	    return result;
-	raise invalid_argument("_dequote: unexpected end of string", 0, s);
-    }
-
-    public quote_context default_quotes = {
-	oq = "\"", cq = "\"", qq = "\\"
-    };
-
-    public string dequote(string s) {
-	return _dequote(s, default_quotes);
-    }
-
-    public string[*] readcsv(string s) {
-	string[...] ss = {};
-	if (s == "" || s == "\n" || s == "\r\n")
-	    return ss;
-	quote_context q = default_quotes;
-	q.qq = "\"";
-	string 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 == ',') {
-		    string t = chomp(curs);
-		    if (t != "" && t[0] == '"')
-			t = _dequote(t, q);
-		    ss[dim(ss)] = t;
-		    curs = "";
-		    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;
-		    break;
-		}
-		if (c == ',') {
-		    t = qstate.normal;
-		    continue;
-		}
-		t = qstate.normal;
-		break;
-	    }
-	    curs = curs + String::new(c);
-	    cur++;
-	}
-	if (t != qstate.normal)
-	    raise invalid_argument("readcsv: unexpected end of csv line", 0, s);
-	ss[dim(ss)] = curs;
-	return ss;
-    }
-
     public int rindex(string target, string pattern) {
 	int pl = length(pattern);
 	for (int i = length(target) - pl; i >= 0; --i) {
@@ -150,22 +33,17 @@
 	    }
 	    j++;
 	}
-	if (i + 1 >= length(s))
-	    ss[dim(ss)] = "";
-	else
-	    ss[dim(ss)] = substr(s, i + 1, j - i - 1);
+	ss[dim(ss)] = substr(s, i + 1, j - i - 1);
 	return ss;
     }
 
     public string chomp(string s) {
-	if (s == "")
-	    return s;
 	int l = length(s);
 	int i = 0;
 	while(i < l && Ctype::isspace(s[i]))
 	    i++;
 	int j = l - 1;
-	while(j >= 0 && Ctype::isspace(s[j]))
+	while(j >= i && Ctype::isspace(s[j]))
 	    --j;
 	return substr(s, i, j - i + 1);
     }
@@ -223,10 +101,8 @@
 	return _dequote(s, default_quotes);
     }
 
-    public string[*] readcsv(string s) {
+    public string[*] parse_csv(string s) {
 	string[...] ss = {};
-	if (s == "" || s == "\n" || s == "\r\n")
-	    return ss;
 	quote_context q = default_quotes;
 	q.qq = "\"";
 	string curs = "";




More information about the Commit mailing list