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;
50 delsvec_recycle_this_string (u8 *s)
55 vec_add1 (string_cache, s);
60 delsvec_recycle_this_svec (u8 **svec)
66 vec_free (svec_cache);
68 vec_set_len (svec, 0);
80 delsvec (void *input_arg, char *fmt)
87 u8 *input = input_arg;
98 if (vec_len (string_cache) > 0)
100 this = string_cache[vec_len (string_cache) - 1];
101 vec_set_len (string_cache, vec_len (string_cache) - 1);
106 * '*' means one of two things: match the rest of the input,
107 * or match as many characters as possible
114 * no more format: eat rest of string...
118 for (; input[input_index]; input_index++)
119 vec_add1 (this, input[input_index]);
124 printf ("final star-match adds: '%s'\n", this);
130 vec_add1 (string_cache, this);
137 * Left-to-right scan, adding chars until next delimiter char
142 while (input[input_index])
144 if (input[input_index] == fmt[0])
146 /* If we just (exact) matched a whitespace delimiter */
149 /* scan forward eating whitespace */
150 while (input[input_index] == ' ' ||
151 input[input_index] == '\t' ||
152 input[input_index] == '\n')
158 /* If we're looking for whitespace */
161 /* and we have whitespace */
162 if (input[input_index] == ' ' ||
163 input[input_index] == '\t' || input[input_index] == '\n')
165 /* scan forward eating whitespace */
166 while (input[input_index] == ' ' ||
167 input[input_index] == '\t' ||
168 input[input_index] == '\n')
176 /* Not a delimiter, save it */
177 vec_add1 (this, input[input_index]);
181 * Fell off the wagon, clean up and bail out
186 printf ("failed, fmt[0] = '%c', input[%d]='%s'\n", fmt[0],
187 input_index, &input[input_index]);
189 delsvec_recycle_this_string (this);
190 for (i = 0; i < vec_len (rv); i++)
191 delsvec_recycle_this_string (rv[i]);
192 delsvec_recycle_this_svec (rv);
202 * If we actually accumulated non-delimiter characters,
203 * add them to the result vector
209 printf ("match: add '%s'\n", this);
215 vec_add1 (string_cache, this);
221 * right-to-left scan, '*' not at
222 * the end of the delimiter string
228 while (i > input_index)
230 if (input[i] == fmt[0])
233 if (fmt[0] == ' ' || fmt[0] == '\t' || fmt[0] == '\n')
235 if (input[i] == ' ' || input[i] == '\t' || input[i] == '\n')
243 for (; input_index < i; input_index++)
245 vec_add1 (this, input[input_index]);
251 printf ("inner '*' match: add '%s'\n", this);
261 typedef struct utest_
269 { "Dec 7 08:56", " :*" },
270 { "Dec 17 08:56", " :*" },
271 { "Dec 7 08:56:41.239 install/inst_repl 0/9/CPU0 t1 [40989] File "
272 "List:Successfully blobbified file list. Took 1 milliseconds",
274 { "RP/0/9/CPU0:Dec 7 08:55:28.550 : sam_server[291]: SAM backs up digest "
275 "list to memory file",
276 "///: ::. : []: *" },
277 /* Expected to fail */
278 { "Dec 7 08:56:41.239 install/inst_repl 0/9/CPU0 t1 [40989] File "
279 "List:Successfully blobbified file list. Took 1 milliseconds",
281 /* Expected to fail */
282 { "RP/0/9/CPU0:Dec 7 08:55:28.550 : sam_server[291]: SAM backs up digest "
283 "list to memory file",
285 { "THIS that and + theother", "*+ *" },
286 { "Dec 12 15:33:07.103 ifmgr/errors 0/RP0/CPU0 3# t2 Failed to open IM "
287 "connection: No such file or directory",
289 { "Dec 16 21:43:47.328 ifmgr/bulk 0/3/CPU0 t8 Bulk DPC async download "
290 "complete. Partitions 1, node_count 1, total_out 0, out_offset 0, "
291 "out_expected 0: No error",
293 { "t:0x53034bd6 CPU:00 PROCESS :PROCCREATE_NAME", ": : :*" },
295 { "t:0x53034cbb CPU:00 THREAD :THCREATE pid:1 tid:1",
296 ": : : pid: tid:*" },
297 { "t:0x5303f950 CPU:00 COMM :REC_PULSE scoid:0x40000003 pid:364659",
299 { "/hfr-base-3.3.85/lib/libttyconnection.dll 0xfc000000 0x0000306c "
300 "0xfc027000 0x000001c8 1",
302 { "Feb 28 02:38:26.123 seqtrace 0/1/CPU0 t8 "
303 ":msg_receive:ifmgr/t8:IMC_MSG_MTU_UPDATE:ppp_ma/t1",
306 { "Feb 28 02:38:26.123 seqtrace 0/1/CPU0 t8 "
307 ":msg_send_event:call:ifmgr/t8:124/0:cdp/t1",
308 " ::. // :msg_send_event::::*" },
310 { "Feb 28 02:38:26.125 seqtrace 0/1/CPU0 t1 "
311 ":msg_receive_event:cdp/t1:124/0",
312 " ::. // :msg_receive_event::*" } {
313 "t:0x645dd86d CPU:00 USREVENT:EVENT:100, d0:0x00000002 d1:0x00000000",
314 ": : USREVENT:EVENT:, d0: *" } {
315 "t:0x5303f950 CPU:00 COMM :REC_PULSE scoid:0x40000003 pid:364659",
317 { "t:0x2ccf9f5a CPU:00 INT_ENTR:0x80000000 (-2147483648) "
319 ": : INT_ENTR: IP:*" } {
320 "t:0xd473951c CPU:00 KER_EXIT:SCHED_GET/88 ret_val:2 sched_priority:10",
321 ": : KER_EXIT:SCHED_GET : sched_priority:*" } {
322 "t:0x00000123 CPU:01 SYSTEM :FUNC_ENTER thisfn:0x40e62048 "
323 "call_site:0x00000000",
324 ": : SYSTEM :FUNC_ thisfn: *" },
325 { "t:0x5af8de95 CPU:00 INT_HANDLER_ENTR:0x0000004d (77) PID:8200 "
326 "IP:0x00000000 AREA:0x0bf9b290",
327 ": : INT_HANDLER_*" },
329 { "t:0x6d1ff92f CPU:00 CONTROL: BUFFER sequence = 1053, num_events = 714",
331 { "t:0x6d1ff92f CPU:00 CONTROL :TIME msb:0x0000003c lsb(offset):0x6d1ff921",
336 main (int argc, char **argv)
341 for (j = 0; j < ARRAY_LEN (tests); j++)
343 printf ("input string: '%s'\n", tests[j].string);
344 printf ("delimiter arg: '%s'\n", tests[j].fmt);
345 printf ("parse trace:\n");
346 svec = delsvec (tests[j].string, tests[j].fmt);
349 printf ("index %d failed\n", j);
352 printf ("%d substring vectors\n", vec_len (svec));
353 for (i = 0; i < vec_len (svec); i++)
355 printf ("[%d]: '%s'\n", i, svec[i]);
357 printf ("-------------------\n");