ucx/string.h

Wed, 17 Jul 2013 15:56:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 17 Jul 2013 15:56:01 +0200
changeset 116
234920008754
parent 109
75cb6590358b
child 118
151f5345f303
permissions
-rw-r--r--

some fixes and some documentation

olaf@20 1 /*
universe@103 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
olaf@20 3 *
universe@103 4 * Copyright 2013 Olaf Wintermann. All rights reserved.
universe@103 5 *
universe@103 6 * Redistribution and use in source and binary forms, with or without
universe@103 7 * modification, are permitted provided that the following conditions are met:
universe@103 8 *
universe@103 9 * 1. Redistributions of source code must retain the above copyright
universe@103 10 * notice, this list of conditions and the following disclaimer.
universe@103 11 *
universe@103 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@103 13 * notice, this list of conditions and the following disclaimer in the
universe@103 14 * documentation and/or other materials provided with the distribution.
universe@103 15 *
universe@103 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@103 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@103 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@103 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@103 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@103 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@103 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@103 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@103 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@103 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@103 26 * POSSIBILITY OF SUCH DAMAGE.
olaf@20 27 */
universe@116 28 /**
universe@116 29 * Bounded string implementation.
universe@116 30 *
universe@116 31 * The UCX strings (<code>sstr_t</code>) provide an alternative to C strings.
universe@116 32 * The main difference to C strings is, that <code>sstr_t</code> does <b>not
universe@116 33 * need to be <code>NULL</code>-terminated</b>. Instead the length is stored
universe@116 34 * within the structure.
universe@116 35 *
universe@116 36 * When using <code>sstr_t</code>, developers must be full aware of what type
universe@116 37 * of string (<code>NULL</code>-terminated) or not) they are using, when
universe@116 38 * accessing the <code>char* ptr</code> directly.
universe@116 39 *
universe@116 40 * The UCX string module provides some common string functions, known from
universe@116 41 * standard libc, working with <code>sstr_t</code>.
universe@116 42 *
universe@116 43 * @file string.h
universe@116 44 * @author Mike Becker
universe@116 45 * @author Olaf Wintermann
universe@116 46 */
olaf@20 47
universe@116 48 #ifndef UCX_STRING_H
universe@116 49 #define UCX_STRING_H
olaf@20 50
universe@69 51 #include "ucx.h"
olaf@109 52 #include "allocator.h"
universe@38 53 #include <stddef.h>
universe@38 54
universe@116 55 /** Shortcut for a <code>sstr_t struct</code> literal. */
universe@116 56 #define ST(s) { (char*)s, sizeof(s)-1 }
universe@116 57 /** Shortcut for the conversion of a C string to a <code>sstr_t</code>. */
universe@116 58 #define S(s) sstrn((char*)s, sizeof(s)-1)
olaf@20 59
olaf@20 60 #ifdef __cplusplus
olaf@20 61 extern "C" {
olaf@20 62 #endif
olaf@20 63
universe@116 64 /**
universe@116 65 * The UCX string structure.
universe@116 66 */
universe@116 67 typedef struct {
universe@116 68 /** A reference to the string (<b>not necessarily <code>NULL</code>
universe@116 69 * -terminated</b>) */
olaf@20 70 char *ptr;
universe@116 71 /** The length of the string */
olaf@20 72 size_t length;
olaf@20 73 } sstr_t;
olaf@20 74
universe@116 75 /**
universe@116 76 * Creates a new sstr_t based on a C string.
universe@116 77 *
universe@116 78 * The length is implicitly inferred by using a call to <code>strlen()</code>.
olaf@20 79 *
universe@116 80 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 81 * do want a copy, use sstrdup() on the return value of this function.
universe@116 82 *
universe@116 83 * @param cstring the C string to wrap
universe@116 84 * @return a new sstr_t containing the C string
universe@116 85 *
universe@116 86 * @see sstrn()
olaf@20 87 */
universe@116 88 sstr_t sstr(char *cstring);
olaf@20 89
universe@116 90 /**
universe@116 91 * Creates a new sstr_t of the specified length based on a C string.
olaf@20 92 *
universe@116 93 * <b>Note:</b> the sstr_t will hold a <i>reference</i> to the C string. If you
universe@116 94 * do want a copy, use sstrdup() on the return value of this function.
universe@116 95 *
universe@116 96 * @param cstring the C string to wrap
universe@116 97 * @param length the length of the string
universe@116 98 * @return a new sstr_t containing the C string
universe@116 99 *
universe@116 100 * @see sstr()
universe@116 101 * @see S()
olaf@20 102 */
universe@116 103 sstr_t sstrn(char *cstring, size_t length);
olaf@20 104
olaf@20 105
universe@116 106 /**
universe@116 107 * Returns the cumulated length of all specified strings.
olaf@20 108 *
universe@116 109 * At least one string must be specified.
universe@116 110 *
universe@116 111 * <b>Attention:</b> if the count argument does not match the count of the
universe@116 112 * specified strings, the behavior is undefined.
universe@116 113 *
universe@116 114 * @param count the total number of specified strings (so at least 1)
universe@116 115 * @param string the first string
universe@116 116 * @param ... all other strings
universe@116 117 * @return the cumulated length of all strings
olaf@20 118 */
universe@116 119 size_t sstrnlen(size_t count, sstr_t string, ...);
olaf@20 120
olaf@20 121
olaf@20 122 /*
olaf@20 123 * concatenates n strings
olaf@20 124 *
olaf@20 125 * n number of strings
olaf@20 126 * s new string with enough memory allocated
olaf@20 127 * ... strings
olaf@20 128 */
olaf@68 129 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...);
olaf@20 130
olaf@20 131
olaf@20 132 /*
olaf@20 133 *
olaf@20 134 */
olaf@68 135 sstr_t sstrsubs(sstr_t s, size_t start);
olaf@20 136
olaf@20 137 /*
olaf@20 138 *
olaf@20 139 */
olaf@68 140 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length);
olaf@20 141
universe@39 142 /*
olaf@108 143 *
olaf@108 144 */
olaf@108 145 sstr_t sstrchr(sstr_t s, int c);
olaf@108 146
olaf@108 147 /*
universe@39 148 * splits s into n parts
universe@39 149 *
universe@39 150 * s the string to split
universe@39 151 * d the delimiter string
universe@39 152 * n the maximum size of the resulting list
universe@39 153 * a size of 0 indicates an unbounded list size
universe@39 154 * the actual size of the list will be stored here
universe@39 155 *
universe@39 156 * Hint: use this value to avoid dynamic reallocation of the result list
universe@39 157 *
universe@39 158 * Returns a list of the split strings
universe@39 159 * NOTE: this list needs to be freed manually after usage
universe@39 160 *
universe@39 161 * Returns NULL on error
universe@39 162 */
olaf@68 163 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
olaf@20 164
universe@116 165 /**
universe@116 166 * Compares two UCX strings with standard <code>memcmp()</code>.
universe@116 167 *
universe@116 168 * At first it compares the sstr_t.length attribute of the two strings. The
universe@116 169 * <code>memcmp()</code> function is called, if and only if the lengths match.
universe@116 170 *
universe@116 171 * @param s1 the first string
universe@116 172 * @param s2 the second string
universe@116 173 * @return -1, if the length of s1 is less than the length of s2 or 1, if the
universe@116 174 * length of s1 is greater than the length of s2 or the result of
universe@116 175 * <code>memcmp()</code> otherwise (i.e. 0 if the strings match)
universe@116 176 */
olaf@68 177 int sstrcmp(sstr_t s1, sstr_t s2);
olaf@20 178
universe@116 179 /**
universe@116 180 * Creates a duplicate of the specified string.
universe@116 181 *
universe@116 182 * The new sstr_t will contain a copy allocated by standard
universe@116 183 * <code>malloc()</code>. So developers <b>MUST</b> pass the sstr_t.ptr to
universe@116 184 * <code>free()</code>.
universe@116 185 *
universe@116 186 * @param string the string to duplicate
universe@116 187 * @return a duplicate of the argument
universe@116 188 */
universe@116 189 sstr_t sstrdup(sstr_t string);
universe@116 190 sstr_t sstrdupa(UcxAllocator *allocator, sstr_t s);
olaf@20 191
olaf@96 192 sstr_t sstrtrim(sstr_t string);
olaf@96 193
olaf@20 194 #ifdef __cplusplus
olaf@20 195 }
olaf@20 196 #endif
olaf@20 197
universe@116 198 #endif /* UCX_STRING_H */

mercurial