ucx/string.h

Wed, 27 Feb 2013 13:53:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 27 Feb 2013 13:53:28 +0100
changeset 96
fbbff331beba
parent 74
dc8bade7f2a3
child 103
08018864fb91
permissions
-rw-r--r--

added sstrtrim

     1 /*
     2  * File:   sstring.h
     3  * Author: olaf
     4  *
     5  * Created on 17. Juni 2010, 13:26
     6  */
     8 #ifndef _SSTRING_H
     9 #define	_SSTRING_H
    11 #include "ucx.h"
    12 #include <stddef.h>
    14 /* use macros for literals only */
    15 #define S(s) { (char*)s, sizeof(s)-1 }
    16 #define ST(s) sstrn((char*)s, sizeof(s)-1)
    18 #ifdef	__cplusplus
    19 extern "C" {
    20 #endif
    22 typedef struct sstring {
    23     char   *ptr;
    24     size_t length;
    25 } sstr_t;
    27 /*
    28  * creates a new sstr_t from a null terminated string
    29  *
    30  * s  null terminated string
    31  */
    32 sstr_t sstr(char *s);
    34 /*
    35  * creates a new sstr_t from a string and length
    36  *
    37  * s  string
    38  * n  length of string
    39  */
    40 sstr_t sstrn(char *s, size_t n);
    43 /*
    44  * gets the length of n sstr_t strings
    45  *
    46  * n    number of strings
    47  * s    string
    48  * ...  strings
    49  */
    50 size_t sstrnlen(size_t n, sstr_t s, ...);
    53 /*
    54  * concatenates n strings
    55  *
    56  * n    number of strings
    57  * s    new string with enough memory allocated
    58  * ...  strings
    59  */
    60 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...);
    63 /*
    64  *
    65  */
    66 sstr_t sstrsubs(sstr_t s, size_t start);
    68 /*
    69  *
    70  */
    71 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length);
    73 /*
    74  * splits s into n parts
    75  *
    76  * s    the string to split
    77  * d    the delimiter string
    78  * n    the maximum size of the resulting list
    79  *      a size of 0 indicates an unbounded list size
    80  *      the actual size of the list will be stored here
    81  *
    82  *      Hint: use this value to avoid dynamic reallocation of the result list
    83  *
    84  * Returns a list of the split strings
    85  * NOTE: this list needs to be freed manually after usage
    86  *
    87  * Returns NULL on error
    88  */
    89 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
    91 int sstrcmp(sstr_t s1, sstr_t s2);
    93 sstr_t sstrdup(sstr_t s);
    95 sstr_t sstrtrim(sstr_t string);
    97 #ifdef	__cplusplus
    98 }
    99 #endif
   101 #endif	/* _SSTRING_H */

mercurial