File: string.5c

package info (click to toggle)
nickle 2.77-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,612 kB
  • ctags: 3,746
  • sloc: ansic: 26,986; yacc: 1,873; sh: 954; lex: 884; makefile: 225
file content (268 lines) | stat: -rw-r--r-- 5,487 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
extend namespace String {

    public int rindex(string target, string pattern) 
	/*
	 * Return the right-most location of 'pattern'
	 * in 'target'
	 */
    {
	int pl = length(pattern);
	for (int i = length(target) - pl; i >= 0; --i) {
	    bool match_here() {
		for (int j = 0; j < pl; j++)
		    if (target[i + j] != pattern[j])
			return false;
		return true;
	    }
	    if (match_here())
		return i;
	}
	return -1;
    }

    public string dirname(string path) 
	/*
	 * Return the directory portion of 'path'
	 */
    {
	int n = rindex(path, "/");
	if (n == -1)
	    return ".";
	return (substr(path, 0, n));
    }

    public string[*] split(string s, string sep) 
	/*
	 * Split 's' at 'sep' boundaries, returning
	 * an array of the resulting pieces
	 */
    {
	string[...] ss = {};
	int i = -1;
	int j = 0;
	while(j < length(s)) {
	    if (index(sep, new(s[j])) >= 0) {
		ss[dim(ss)] = substr(s, i + 1, j - i - 1);
		i = j;
	    }
	    j++;
	}
	ss[dim(ss)] = substr(s, i + 1, j - i - 1);
	return ss;
    }


    public void shiftn(&(poly[...]) array, int n)
    /*
     * Shift a growable array n elements: left is positive
     */
    {
	int f = dim(array);
	for (int i = 0; i < f - n; i++)
	    array[i] = array[i + n];
	if (n < 0)
	    for (int i = 0; i < -n; i++)
		make_uninit(&array[i]);
	if (n > 0)
	    setdim(array, f - n);
    }

    public void shift(&(poly[...]) array)
    /*
     * Shift a growable array one element to the left
     */
    {
	shiftn(&array, 1);
    }

    public string chump(string s)
    /*
     * Return s with any trailing newlines removed
     */
    {
	int n = length(s);
	while(n > 0 && s[n - 1] == '\n')
	    --n;
	return substr(s, 0, n);
    }

    public string chomp(string s) 
	/*
	 * Trim whitespace from begining and end of 's'
	 */
    {
	int l = length(s);
	int i = 0;
	while(i < l && Ctype::isspace(s[i]))
	    i++;
	int j = l - 1;
	while(j >= i && Ctype::isspace(s[j]))
	    --j;
	return substr(s, i, j - i + 1);
    }

    public bool inchars(int c, string s) 
	/*
	 * Return whether 'c' is in 's'
	 */
    {
	return index(s, String::new(c)) >= 0;
    }

    public string[*] wordsplit(string s, string sep) 
    /*
     * Split 's' at boundaries consisting of sequences of
     * 'sep' characters, returning an array of the resulting
     * pieces
     */
    {
	string[...] ss = {};
	int i = 0; 
	while(i < length(s)) {
	    while (i < length(s) && inchars(s[i], sep))
		i++;
	    int j = i;
	    while (j < length(s) && !inchars(s[j], sep))
		j++;
	    if (i == j)
		break;
	    ss[dim(ss)] = substr(s, i, j - i);
	    i = j;
	}
	return ss;
    }

    public typedef struct { 
	string oq, cq, qq;
    } quote_context;

    typedef enum {normal, openq, xq} qstate;

    typedef string(string) dequote_t;
    
    public dequote_t make_dequote(quote_context q) 
	/*
	 * Construct a quote parsing function based on '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;
		    break;
		}
		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);
	}
	return _dequote;
    }

    public dequote_t dequote = make_dequote(
       (quote_context) { .oq = "\"", .cq = "\"", .qq = "\\"}
    );

    public typedef (string[*])(string) parse_csv_t;

    public exception bad_csv_parse(string error_msg,
    	   	                   string[*] partial_parse);

    public parse_csv_t make_parse_csv(quote_context q) 
	/*
	 * Construct a CSV file parsing context from '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;
		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.openq)
		raise bad_csv_parse("parse_csv: unexpected " +
				    "end of csv line", ss);
	    consume();
	    return ss;
	}
	return _parse_csv;
    }

    public parse_csv_t parse_csv = make_parse_csv(
       (quote_context) { .oq = "\"", .cq = "\"", .qq = "\""}
    );

    public string reverse(string s)
        /*
         * Reverse the string s
         */
    {
        int ns = length(s);
        int[ns] t = { [i] = s[ns - i - 1] };
        return String::new(t);
    }

}