Review Board 1.7.16


utils: Create ast_strsep function that ignores separators inside quotes.

Review Request #3989 - Created Sept. 10, 2014 and submitted

George Joseph
12
Reviewers
asterisk-dev
rmudgett
Asterisk
I'm going to need this for my imminent manager and config
enhancements but I thought I'd post this separately.

/*!
  \brief Act like strsep but ignore separators inside quotes.
  \param s Pointer to address of the the string to be processed.
  Will be modified and can't be constant.
  \param sep A single character delimiter.
  \param flags Controls post-processing of the result.
  AST_STRSEP_TRIM trims all leading and trailing whitespace from the result.
  AST_STRSEP_STRIP does a trim then strips the outermost quotes.  You may want
  to trim again after the strip.  Just OR both the TRIM and STRIP flags.
  AST_STRSEP_UNESCAPE unescapes '\' sequences.
  AST_STRSEP_ALL does all of the above processing.
  \return The next token or NULL if done or if there are more than 8 levels of
  nested quotes.

  This function acts like strsep with three exceptions...
  The separator is a single character instead of a string.
  Separators inside quotes are treated literally instead of like separators.
  You can elect to have leading and trailing whitespace and quotes
  stripped from the result and have '\' sequences unescaped.

  Like strsep, ast_strsep maintains no internal state and you can call it
  recursively using different separators on the same storage.

  Also like strsep, for consistent results, consecutive separators are not
  collapsed so you may get an empty string as a valid result.

  Examples:
  \code
	char *mystr = ast_strdupa("abc=def,ghi='zzz=yyy,456',jkl");
	char *token, *token2, *token3;

	while((token = ast_strsep(&mystr, ',', AST_SEP_STRIP))) {
		// 1st token will be aaa=def
		// 2nd token will be ghi='zzz=yyy,456'
		while((token2 = ast_strsep(&token, '=', AST_SEP_STRIP))) {
			// 1st token2 will be ghi
			// 2nd token2 will be zzz=yyy,456
			while((token3 = ast_strsep(&token2, ',', AST_SEP_STRIP))) {
				// 1st token3 will be zzz=yyy
				// 2nd token3 will be 456
				// and so on
			}
		}
		// 3rd token will be jkl
	}

  \endcode
 */
char *ast_strsep(char **s, const char sep, uint32_t flags);

 

Diff revision 2

This is not the most recent revision of the diff. The latest diff is revision 4. See what's changed.

1 2 3 4
1 2 3 4

  1. branches/12/include/asterisk/strings.h: Loading...
  2. branches/12/main/utils.c: Loading...
  3. branches/12/tests/test_strings.c: Loading...
branches/12/include/asterisk/strings.h
Revision 422963 New Change
[20] 233 lines
[+20] [+] char *ast_strip(char *s),
234
  \endcode
234
  \endcode
235
 */
235
 */
236
char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
236
char *ast_strip_quoted(char *s, const char *beg_quotes, const char *end_quotes);
237

    
   
237

   
238
/*!
238
/*!

    
   
239
  \brief Flags for ast_strsep

    
   
240
 */

    
   
241
enum ast_strsep_flags {

    
   
242
	AST_STRSEP_STRIP =		0x01, /*!< Trim, then strip quotes.  You may want to trim again */

    
   
243
	AST_STRSEP_TRIM =		0x02, /*!< Trim leading and trailing whitespace */

    
   
244
	AST_STRSEP_UNESCAPE =	0x04, /*!< Unescape '/' */

    
   
245
	AST_STRSEP_ALL =		0x07, /*!< Trim, strip, unescape */

    
   
246
};

    
   
247

   

    
   
248
/*!

    
   
249
  \brief Act like strsep but ignore separators inside quotes.

    
   
250
  \param s Pointer to address of the the string to be processed.

    
   
251
  Will be modified and can't be constant.

    
   
252
  \param sep A single character delimiter.

    
   
253
  \param flags Controls post-processing of the result.

    
   
254
  AST_STRSEP_TRIM trims all leading and trailing whitespace from the result.

    
   
255
  AST_STRSEP_STRIP does a trim then strips the outermost quotes.  You may want

    
   
256
  to trim again after the strip.  Just OR both the TRIM and STRIP flags.

    
   
257
  AST_STRSEP_UNESCAPE unescapes '\' sequences.

    
   
258
  AST_STRSEP_ALL does all of the above processing.

    
   
259
  \return The next token or NULL if done.

    
   
260

   

    
   
261
  This function acts like strsep with three exceptions...

    
   
262
  The separator is a single character instead of a string.

    
   
263
  Separators inside quotes are treated literally instead of like separators.

    
   
264
  You can elect to have leading and trailing whitespace and quotes

    
   
265
  stripped from the result and have '\' sequences unescaped.

    
   
266

   

    
   
267
  Like strsep, ast_strsep maintains no internal state and you can call it

    
   
268
  recursively using different separators on the same storage.

    
   
269

   

    
   
270
  Also like strsep, for consistent results, consecutive separators are not

    
   
271
  collapsed so you may get an empty string as a valid result.

    
   
272

   

    
   
273
  Examples:

    
   
274
  \code

    
   
275
	char *mystr = ast_strdupa("abc=def,ghi='zzz=yyy,456',jkl");

    
   
276
	char *token, *token2, *token3;

    
   
277

   

    
   
278
	while((token = ast_strsep(&mystr, ',', AST_SEP_STRIP))) {

    
   
279
		// 1st token will be aaa=def

    
   
280
		// 2nd token will be ghi='zzz=yyy,456'

    
   
281
		while((token2 = ast_strsep(&token, '=', AST_SEP_STRIP))) {

    
   
282
			// 1st token2 will be ghi

    
   
283
			// 2nd token2 will be zzz=yyy,456

    
   
284
			while((token3 = ast_strsep(&token2, ',', AST_SEP_STRIP))) {

    
   
285
				// 1st token3 will be zzz=yyy

    
   
286
				// 2nd token3 will be 456

    
   
287
				// and so on

    
   
288
			}

    
   
289
		}

    
   
290
		// 3rd token will be jkl

    
   
291
	}

    
   
292

   

    
   
293
  \endcode

    
   
294
 */

    
   
295
char *ast_strsep(char **s, const char sep, uint32_t flags);

    
   
296

   

    
   
297
/*!
239
  \brief Strip backslash for "escaped" semicolons, 
298
  \brief Strip backslash for "escaped" semicolons, 
240
	the string to be stripped (will be modified).
299
	the string to be stripped (will be modified).
241
  \return The stripped string.
300
  \return The stripped string.
242
 */
301
 */
243
char *ast_unescape_semicolon(char *s);
302
char *ast_unescape_semicolon(char *s);
[+20] [20] 956 lines
branches/12/main/utils.c
Revision 422963 New Change
 
branches/12/tests/test_strings.c
Revision 422963 New Change
 
  1. branches/12/include/asterisk/strings.h: Loading...
  2. branches/12/main/utils.c: Loading...
  3. branches/12/tests/test_strings.c: Loading...

https://reviewboard.asterisk.org/ runs on a server provided by Digium, Inc. and uses bandwidth donated to the open source Asterisk community by API Digital Communications in Huntsville, AL USA.
Please report problems with this site to asteriskteam@digium.com.