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

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

mercurial