ucx/string.h

Fri, 12 Jul 2013 20:50:18 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 12 Jul 2013 20:50:18 +0200
changeset 108
d2b1e67b2b48
parent 103
08018864fb91
child 109
75cb6590358b
permissions
-rw-r--r--

new properties parser

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 */
olaf@20 28
olaf@20 29 #ifndef _SSTRING_H
olaf@20 30 #define _SSTRING_H
olaf@20 31
universe@69 32 #include "ucx.h"
universe@38 33 #include <stddef.h>
universe@38 34
universe@42 35 /* use macros for literals only */
universe@74 36 #define S(s) { (char*)s, sizeof(s)-1 }
universe@74 37 #define ST(s) sstrn((char*)s, sizeof(s)-1)
olaf@20 38
olaf@20 39 #ifdef __cplusplus
olaf@20 40 extern "C" {
olaf@20 41 #endif
olaf@20 42
olaf@20 43 typedef struct sstring {
olaf@20 44 char *ptr;
olaf@20 45 size_t length;
olaf@20 46 } sstr_t;
olaf@20 47
olaf@20 48 /*
olaf@20 49 * creates a new sstr_t from a null terminated string
olaf@20 50 *
olaf@20 51 * s null terminated string
olaf@20 52 */
olaf@68 53 sstr_t sstr(char *s);
olaf@20 54
olaf@20 55 /*
olaf@20 56 * creates a new sstr_t from a string and length
olaf@20 57 *
olaf@20 58 * s string
olaf@20 59 * n length of string
olaf@20 60 */
olaf@68 61 sstr_t sstrn(char *s, size_t n);
olaf@20 62
olaf@20 63
olaf@20 64 /*
olaf@20 65 * gets the length of n sstr_t strings
olaf@20 66 *
olaf@20 67 * n number of strings
olaf@20 68 * s string
olaf@20 69 * ... strings
olaf@20 70 */
olaf@68 71 size_t sstrnlen(size_t n, sstr_t s, ...);
olaf@20 72
olaf@20 73
olaf@20 74 /*
olaf@20 75 * concatenates n strings
olaf@20 76 *
olaf@20 77 * n number of strings
olaf@20 78 * s new string with enough memory allocated
olaf@20 79 * ... strings
olaf@20 80 */
olaf@68 81 sstr_t sstrncat(size_t n, sstr_t s, sstr_t c1, ...);
olaf@20 82
olaf@20 83
olaf@20 84 /*
olaf@20 85 *
olaf@20 86 */
olaf@68 87 sstr_t sstrsubs(sstr_t s, size_t start);
olaf@20 88
olaf@20 89 /*
olaf@20 90 *
olaf@20 91 */
olaf@68 92 sstr_t sstrsubsl(sstr_t s, size_t start, size_t length);
olaf@20 93
universe@39 94 /*
olaf@108 95 *
olaf@108 96 */
olaf@108 97 sstr_t sstrchr(sstr_t s, int c);
olaf@108 98
olaf@108 99 /*
universe@39 100 * splits s into n parts
universe@39 101 *
universe@39 102 * s the string to split
universe@39 103 * d the delimiter string
universe@39 104 * n the maximum size of the resulting list
universe@39 105 * a size of 0 indicates an unbounded list size
universe@39 106 * the actual size of the list will be stored here
universe@39 107 *
universe@39 108 * Hint: use this value to avoid dynamic reallocation of the result list
universe@39 109 *
universe@39 110 * Returns a list of the split strings
universe@39 111 * NOTE: this list needs to be freed manually after usage
universe@39 112 *
universe@39 113 * Returns NULL on error
universe@39 114 */
olaf@68 115 sstr_t* sstrsplit(sstr_t s, sstr_t d, size_t *n);
olaf@20 116
olaf@68 117 int sstrcmp(sstr_t s1, sstr_t s2);
olaf@20 118
olaf@68 119 sstr_t sstrdup(sstr_t s);
olaf@20 120
olaf@96 121 sstr_t sstrtrim(sstr_t string);
olaf@96 122
olaf@20 123 #ifdef __cplusplus
olaf@20 124 }
olaf@20 125 #endif
olaf@20 126
olaf@20 127 #endif /* _SSTRING_H */

mercurial