initial commit
[rofl0r-xstring.git] / xstring.h
blob4fff6dfab4c5faff651099edca4d7c0302458ac4
1 /*
2 * xstring.h
4 * Created on: 15.10.2010
5 * Author: rofl
6 */
8 #ifndef XSTRING_H_
9 #define XSTRING_H_
11 #include "xtypes.h"
12 #include "xchar.h"
14 #include <stdlib.h>
15 #include <string.h>
17 // heap alloced string.
18 typedef struct xstr_struct {
19 xuint size;
20 xuint capacity;
21 // stores the pointer to the original allocation. used for shifting
22 // and to keep always a reference to keep a GC in tune, if one is used.
23 unsigned char* mallocaddr;
24 // points to the beginning of our string.
25 unsigned char* data;
27 } xstr;
29 // for stackalloc. size limited.
30 typedef struct xshortstr512_struct {
31 xuint size;
32 xuint capacity;
33 // stores the pointer to the original allocation. used for shifting
34 // and to keep always a reference to keep a GC in tune, if one is used.
35 unsigned char* mallocaddr;
36 // points to the beginning of our string.
37 unsigned char* data;
38 unsigned char buffer[512];
40 } xshortstr512;
42 #define shortstr(X) struct xshortstr##X##_struct { \
43 xuint size; \
44 xuint capacity; \
45 uchar* mallocaddr; \
46 uchar* data; \
47 uchar buffer[X]; \
50 // ctor, dtor and stuff to play with the internals.
51 xuint rshift(xstr* str);
52 xstr* xshortstr_init(void* str, xuint size);
53 xstr* xstr_new(xuint size);
54 xstr* xstr_create(void);
55 xstr* xstr_fromliteral(const char* literal, xuint size);
56 void xstr_free(xstr* self);
57 void xstr_shiftright (xstr* self, xint count);
58 void xstr_shiftleft(xstr* self, xint count);
59 bool xstr_setcapacity(xstr* self, xuint newcapacity);
60 xstr* xstr_init(xstr* self, xuint capacity);
61 bool xstr_setlength(xstr* self, xuint new_length);
63 // test functions
64 bool xstr_empty(xstr* self);
65 bool xstr_compare (xstr* self, xstr* other, xint start, xint length);
66 bool xstr_equals (xstr* self, xstr* other);
67 bool xstr_startswith (xstr* self, xstr* s);
68 bool xstr_startswith_char (xstr* self, uchar c);
69 bool xstr_endswith (xstr* self, xstr* s);
70 bool xstr_endswith_char(xstr* self, uchar c);
71 bool xstr_contains_pointer(xstr* self, uchar* what, xuint whatsize);
72 bool xstr_contains_char(xstr* self, uchar what);
73 bool xstr_contains_xstr(xstr* self, xstr* what);
74 xuint xstr_count_char(xstr* self, uchar what, bool searchCaseSensitive);
75 xuint xstr_count_xstr(xstr* self, xstr* what, bool searchCaseSensitive);
76 xuint xstr_count_pointer(xstr* self, uchar* what, xuint whatSize, bool searchCaseSensitive);
79 // helper functions
80 xint xstr_find (xstr* self, xstr* what, xint offset, bool searchCaseSensitive);
81 xint xstr_find_char(xstr* self, uchar what, xint offset, bool searchCaseSensitive);
82 xint xstr_find_pointer (xstr* self, uchar* what, xuint whatSize, xint offset, bool searchCaseSensitive);
84 // conversion function
85 void xstr_tolower(xstr* self);
86 void xstr_toupper(xstr* self);
88 void xstr_trim_pointer(xstr* self, uchar* s, xuint sLength);
89 void xstr_trim_xstr(xstr* self, xstr* s);
90 void xstr_trim_char(xstr* self, uchar c);
91 void xstr_trim_whitespace(xstr* self);
92 void xstr_trim_space(xstr* self);
94 void xstr_trimleft_char(xstr* self, uchar c);
95 void xstr_trimleft_xstr(xstr* self, xstr* s);
96 void xstr_trimleft_pointer(xstr* self, uchar* s, xuint sLength);
97 void xstr_trimleft_whitespace(xstr* self);
98 void xstr_trimleft_space(xstr* self);
100 void xstr_trimright_char(xstr* self, uchar c);
101 void xstr_trimright_xstr(xstr* self, xstr* s);
102 void xstr_trimright_pointer(xstr* self, uchar* s, xuint sLength);
103 void xstr_trimright_whitespace(xstr* self);
104 void xstr_trimright_space(xstr* self);
106 void xstr_reverse(xstr* self);
107 void xstr_append(xstr* self, ...);
109 int xstr_puts(xstr* self);
115 #endif /* XSTRING_H_ */