2 *------------------------------------------------------------------
3 * Copyright (c) 2006-2016 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* Break up a delimited string into a vector of substrings */
20 #include <vppinfra/clib.h>
21 #include <vppinfra/vec.h>
22 #include <vppinfra/hash.h>
26 * #define UNIT_TESTS 1
27 * #define MATCH_TRACE 1
32 * break up an input string into a vector of [null-terminated] u8 *'s
34 * Each supplied delimiter character results in a string in the output
35 * vector, unless the delimiters occur back-to-back. When matched,
36 * a whitespace character in the delimiter consumes an arbitrary
37 * run of whitespace. See the unit tests at the end of this file
38 * for a set of examples.
40 * Returns a u8 **, or NULL if the input fails to match. It is assumed
41 * that both input and fmt are C strings, not necessarily vectors.
43 * Output strings are both vectors and proper C strings.
46 static u8 **string_cache;
47 static u8 **svec_cache;
49 void delsvec_recycle_this_string (u8 *s)
53 vec_add1(string_cache, s);
57 void delsvec_recycle_this_svec (u8 **svec)
61 vec_free (svec_cache);
73 u8 **delsvec(void *input_arg, char *fmt)
80 u8 *input = input_arg;
89 if (vec_len (string_cache) > 0) {
90 this = string_cache [vec_len(string_cache)-1];
91 _vec_len (string_cache) = vec_len (string_cache) - 1;
95 * '*' means one of two things: match the rest of the input,
96 * or match as many characters as possible
102 * no more format: eat rest of string...
105 for (;input[input_index]; input_index++)
106 vec_add1(this, input[input_index]);
110 printf("final star-match adds: '%s'\n", this);
114 vec_add1(string_cache, this);
121 * Left-to-right scan, adding chars until next delimiter char
125 while (input[input_index]) {
126 if (input[input_index] == fmt[0]) {
127 /* If we just (exact) matched a whitespace delimiter */
129 /* scan forward eating whitespace */
130 while (input[input_index] == ' ' ||
131 input[input_index] == '\t' ||
132 input[input_index] == '\n')
138 /* If we're looking for whitespace */
140 /* and we have whitespace */
141 if (input[input_index] == ' ' ||
142 input[input_index] == '\t' ||
143 input[input_index] == '\n') {
144 /* scan forward eating whitespace */
145 while (input[input_index] == ' ' ||
146 input[input_index] == '\t' ||
147 input[input_index] == '\n') {
154 /* Not a delimiter, save it */
155 vec_add1(this, input[input_index]);
159 * Fell off the wagon, clean up and bail out
164 printf("failed, fmt[0] = '%c', input[%d]='%s'\n",
165 fmt[0], input_index, &input[input_index]);
167 delsvec_recycle_this_string(this);
168 for (i = 0; i < vec_len(rv); i++)
169 delsvec_recycle_this_string(rv[i]);
170 delsvec_recycle_this_svec(rv);
180 * If we actually accumulated non-delimiter characters,
181 * add them to the result vector
186 printf("match: add '%s'\n", this);
190 vec_add1(string_cache, this);
194 * right-to-left scan, '*' not at
195 * the end of the delimiter string
201 while (i > input_index) {
202 if (input[i] == fmt[0])
205 if (fmt[0] == ' ' || fmt[0] == '\t' ||
207 if (input[i] == ' ' ||
217 for (; input_index < i; input_index++) {
218 vec_add1(this, input[input_index]);
224 printf("inner '*' match: add '%s'\n", this);
234 typedef struct utest_ {
245 {"Dec 7 08:56:41.239 install/inst_repl 0/9/CPU0 t1 [40989] File List:Successfully blobbified file list. Took 1 milliseconds",
247 {"RP/0/9/CPU0:Dec 7 08:55:28.550 : sam_server[291]: SAM backs up digest list to memory file",
249 /* Expected to fail */
250 {"Dec 7 08:56:41.239 install/inst_repl 0/9/CPU0 t1 [40989] File List:Successfully blobbified file list. Took 1 milliseconds",
252 /* Expected to fail */
253 {"RP/0/9/CPU0:Dec 7 08:55:28.550 : sam_server[291]: SAM backs up digest list to memory file",
255 {"THIS that and + theother", "*+ *"},
256 {"Dec 12 15:33:07.103 ifmgr/errors 0/RP0/CPU0 3# t2 Failed to open IM connection: No such file or directory", " ::. / // *"},
257 {"Dec 16 21:43:47.328 ifmgr/bulk 0/3/CPU0 t8 Bulk DPC async download complete. Partitions 1, node_count 1, total_out 0, out_offset 0, out_expected 0: No error"," ::. / // *"},
258 {"t:0x53034bd6 CPU:00 PROCESS :PROCCREATE_NAME",
261 {"t:0x53034cbb CPU:00 THREAD :THCREATE pid:1 tid:1",
263 {"t:0x5303f950 CPU:00 COMM :REC_PULSE scoid:0x40000003 pid:364659",
265 {"/hfr-base-3.3.85/lib/libttyconnection.dll 0xfc000000 0x0000306c 0xfc027000 0x000001c8 1",
267 {"Feb 28 02:38:26.123 seqtrace 0/1/CPU0 t8 :msg_receive:ifmgr/t8:IMC_MSG_MTU_UPDATE:ppp_ma/t1",
270 {"Feb 28 02:38:26.123 seqtrace 0/1/CPU0 t8 :msg_send_event:call:ifmgr/t8:124/0:cdp/t1",
271 " ::. // :msg_send_event::::*"},
273 {"Feb 28 02:38:26.125 seqtrace 0/1/CPU0 t1 :msg_receive_event:cdp/t1:124/0",
274 " ::. // :msg_receive_event::*"}
275 {"t:0x645dd86d CPU:00 USREVENT:EVENT:100, d0:0x00000002 d1:0x00000000",
276 ": : USREVENT:EVENT:, d0: *"}
277 {"t:0x5303f950 CPU:00 COMM :REC_PULSE scoid:0x40000003 pid:364659",
279 {"t:0x2ccf9f5a CPU:00 INT_ENTR:0x80000000 (-2147483648) IP:0x002d8b18",
280 ": : INT_ENTR: IP:*"}
281 {"t:0xd473951c CPU:00 KER_EXIT:SCHED_GET/88 ret_val:2 sched_priority:10",
282 ": : KER_EXIT:SCHED_GET : sched_priority:*"}
283 {"t:0x00000123 CPU:01 SYSTEM :FUNC_ENTER thisfn:0x40e62048 call_site:0x00000000",
284 ": : SYSTEM :FUNC_ thisfn: *"},
285 {"t:0x5af8de95 CPU:00 INT_HANDLER_ENTR:0x0000004d (77) PID:8200 IP:0x00000000 AREA:0x0bf9b290", ": : INT_HANDLER_*"},
287 {"t:0x6d1ff92f CPU:00 CONTROL: BUFFER sequence = 1053, num_events = 714",
289 {"t:0x6d1ff92f CPU:00 CONTROL :TIME msb:0x0000003c lsb(offset):0x6d1ff921",
293 int main (int argc, char **argv)
298 for (j = 0; j < ARRAY_LEN(tests); j++) {
299 printf ("input string: '%s'\n", tests[j].string);
300 printf ("delimiter arg: '%s'\n", tests[j].fmt);
301 printf ("parse trace:\n");
302 svec = delsvec(tests[j].string, tests[j].fmt);
304 printf("index %d failed\n", j);
307 printf("%d substring vectors\n", vec_len(svec));
308 for (i = 0; i < vec_len(svec); i++) {
309 printf("[%d]: '%s'\n", i, svec[i]);
311 printf ("-------------------\n");