ucx/string.h

Sun, 14 Jul 2013 17:11:34 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 14 Jul 2013 17:11:34 +0200
changeset 109
75cb6590358b
parent 108
d2b1e67b2b48
child 116
234920008754
permissions
-rw-r--r--

added properties load/store functions

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2013 Olaf Wintermann. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #ifndef _SSTRING_H
    30 #define	_SSTRING_H
    32 #include "ucx.h"
    33 #include "allocator.h"
    34 #include <stddef.h>
    36 /* use macros for literals only */
    37 #define S(s) { (char*)s, sizeof(s)-1 }
    38 #define ST(s) sstrn((char*)s, sizeof(s)-1)
    40 #ifdef	__cplusplus
    41 extern "C" {
    42 #endif
    44 typedef struct sstring {
    45     char   *ptr;
    46     size_t length;
    47 } sstr_t;
    49 /*
    50  * creates a new sstr_t from a null terminated string
    51  *
    52  * s  null terminated string
    53  */
    54 sstr_t sstr(char *s);
    56 /*
    57  * creates a new sstr_t from a string and length
    58  *
    59  * s  string
    60  * n  length of string
    61  */
    62 sstr_t sstrn(char *s, size_t n);
    65 /*
    66  * gets the length of n sstr_t strings
    67  *
    68  * n    number of strings
    69  * s    string
    70  * ...  strings
    71  */
    72 size_t sstrnlen(size_t n, sstr_t s, ...);
    75 /*
    76  * concatenates n strings
    77  *
    78  * n    number of strings
    79  * s    new string with enough memory allocated
    80  * ...  strings
    81  */
    82 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...);
    85 /*
    86  *
    87  */
    88 sstr_t sstrsubs(sstr_t s, size_t start);
    90 /*
    91  *
    92  */
    93 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length);
    95 /*
    96  * 
    97  */
    98 sstr_t sstrchr(sstr_t s, int c);
   100 /*
   101  * splits s into n parts
   102  *
   103  * s    the string to split
   104  * d    the delimiter string
   105  * n    the maximum size of the resulting list
   106  *      a size of 0 indicates an unbounded list size
   107  *      the actual size of the list will be stored here
   108  *
   109  *      Hint: use this value to avoid dynamic reallocation of the result list
   110  *
   111  * Returns a list of the split strings
   112  * NOTE: this list needs to be freed manually after usage
   113  *
   114  * Returns NULL on error
   115  */
   116 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
   118 int sstrcmp(sstr_t s1, sstr_t s2);
   120 sstr_t sstrdup(sstr_t s);
   121 sstr_t sstrdup_alloc(UcxAllocator *allocator, sstr_t s);
   123 sstr_t sstrtrim(sstr_t string);
   125 #ifdef	__cplusplus
   126 }
   127 #endif
   129 #endif	/* _SSTRING_H */

mercurial