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

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

mercurial