1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #ifndef _SSTRING_H_ |
---|
6 | #define _SSTRING_H_ |
---|
7 | |
---|
8 | //#define SSTRING_SIMPLE |
---|
9 | |
---|
10 | #ifdef SSTRING_SIMPLE |
---|
11 | |
---|
12 | // simple sstring implementation using direct character arrays |
---|
13 | // - duplicate = copy all characters |
---|
14 | // - no mutex needed |
---|
15 | |
---|
16 | #include "sstring-simple.h" |
---|
17 | |
---|
18 | #else |
---|
19 | /////////////////////////////////////////////////////////////////////////// |
---|
20 | // old sstring implementation using SBuf references |
---|
21 | // - duplicate = copy buffer pointer |
---|
22 | // - mutex required to be thread safe |
---|
23 | |
---|
24 | #include <stdint.h> |
---|
25 | #include <string.h> |
---|
26 | #include <stdlib.h> |
---|
27 | #include <stdio.h> |
---|
28 | |
---|
29 | class ExtValue; //this include would result in recurrent inclusion: #include "extvalue.h" |
---|
30 | class ExtObject; |
---|
31 | |
---|
32 | class SBuf |
---|
33 | { |
---|
34 | char *txt; |
---|
35 | int used; ///< data size |
---|
36 | int size; ///< buffer size, not including \0, special case: 0==buffer not allocated |
---|
37 | int refcount; ///< buffer is used by 'refcount' objects. |
---|
38 | void initEmpty(); |
---|
39 | void ensureSize(int wantsize); |
---|
40 | void copyFrom(const char* ch, int chlen=-1); |
---|
41 | void freeBuf(); |
---|
42 | void append(const char* ch, int chlen=-1); |
---|
43 | static SBuf &empty(); |
---|
44 | SBuf(int initsize); |
---|
45 | friend class SString; |
---|
46 | SBuf(const SBuf& b) {} |
---|
47 | public: |
---|
48 | SBuf(); |
---|
49 | ~SBuf(); |
---|
50 | uint32_t hash() const; // 32-bit FNV-1 hash -> http://en.wikipedia.org/wiki/Fowler_Noll_Vo_hash |
---|
51 | }; |
---|
52 | |
---|
53 | /// (not so) simple text string class |
---|
54 | |
---|
55 | class SString |
---|
56 | { |
---|
57 | private: |
---|
58 | SBuf *buf; ///< buffer |
---|
59 | int appending; ///< append mode, changes can occur after character # 'appending' |
---|
60 | //int memhint; |
---|
61 | |
---|
62 | void initEmpty(); |
---|
63 | int guessMemSize(int request); |
---|
64 | void copyFrom(SString &from); ///< copy from SString, reference if possible |
---|
65 | void detach(); ///< detach from shared buffer, if any |
---|
66 | void detachEmpty(int ensuresize=0); ///< detach and make empty |
---|
67 | void detachCopy(int ensuresize=0); ///< detach and make private copy |
---|
68 | |
---|
69 | public: |
---|
70 | SString(); ///< make an empty string |
---|
71 | SString(const char*t,int t_len=-1); ///< make a string from char* |
---|
72 | SString(int x); ///< string with initial buffer size |
---|
73 | SString(const SString& from); ///< duplicate string |
---|
74 | ~SString(); |
---|
75 | |
---|
76 | void copyFrom(const char* ch, int chlen=-1); ///< copy string, length of -1 == unknown |
---|
77 | |
---|
78 | void* operator new(size_t s, void* mem) {return mem;} |
---|
79 | #ifdef _MSC_VER |
---|
80 | void operator delete(void* mem, void* t) {} |
---|
81 | #endif |
---|
82 | void* operator new(size_t s) {return malloc(sizeof(SString));} |
---|
83 | void operator delete(void* mem) {free(mem);} |
---|
84 | |
---|
85 | int len() const {return buf->used;} ///< get string length |
---|
86 | void shrink(); ///< free unnecessary buffer |
---|
87 | |
---|
88 | /// after this call, you can modify sstring directly. |
---|
89 | /// returned value is the pointer to the internal buffer. |
---|
90 | /// <B>ensuresize</B> is minimal value of bytes you need, |
---|
91 | /// the buffer will be resized as needed. |
---|
92 | /// all "direct" operations have to leave the buffer with trailing '\0' |
---|
93 | /// at the end. endWrite() will search for this value in order to determine |
---|
94 | /// new string length. |
---|
95 | /// <P>Sample:<CODE> |
---|
96 | /// SString t; |
---|
97 | /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); |
---|
98 | /// t.endWrite();</CODE> |
---|
99 | char *directWrite(int ensuresize=-1); |
---|
100 | //char *directWrite(); |
---|
101 | /// like directWrite, but it returns the pointer to the first char after current string |
---|
102 | /// for easy appending. <B>maxappend</B> is minimum of character in buffer |
---|
103 | /// that can be appended after this call. |
---|
104 | /// <P>Sample:<CODE> |
---|
105 | /// SString t; |
---|
106 | /// sprintf(t.directAppend(10),"c=%d",c); |
---|
107 | /// t.endAppend();</CODE> |
---|
108 | char *directAppend(int maxappend=0); |
---|
109 | /// update string length, after directWrite. |
---|
110 | /// you don't have to to call endWrite after directWrite if the string's length doesn't change. |
---|
111 | /// optional <B>newlength</B> parameter gives a chance to further optimize |
---|
112 | /// this operation if you know exact length of resulting string. |
---|
113 | /// <P>Sample:<CODE> |
---|
114 | /// SString t("samplestring"); |
---|
115 | /// strncpy(t.directWrite(50),src,bytecount); |
---|
116 | /// t.endWrite(bytecount);</CODE> |
---|
117 | void endWrite(int newlength=-1); |
---|
118 | /// update string length, after directAppend. |
---|
119 | /// you will usually need to call endAppend (or endWrite) after directAppend, |
---|
120 | /// because the purpose of directAppend is to change string's length. |
---|
121 | /// optional <B>appendlength</B> parameter gives a chance to further optimize |
---|
122 | /// this operation if you know exact length of the appended string. |
---|
123 | /// <P>Sample:<CODE> |
---|
124 | /// SString t("samplestring"); |
---|
125 | /// strncpy(t.directAppend(50),src,bytecount); |
---|
126 | /// t.endAppend(bytecount);</CODE> |
---|
127 | void endAppend(int appendlength=-1); |
---|
128 | /// argument is the amount of memory, that will be probably used |
---|
129 | /// by this string instance. string can use this value |
---|
130 | /// to optimize memory allocation (bigger chunks will be allocated). |
---|
131 | void memoryHint(int howbig); |
---|
132 | int directMaxLen() {return buf->size;} ///< when called after directWrite: max number of characters allowed (can be more than requested) |
---|
133 | |
---|
134 | /// find a character in SString. |
---|
135 | /// return index if the character was found or -1 otherwise. |
---|
136 | int indexOf(int character,int start=0) const; |
---|
137 | |
---|
138 | /// find a substring. |
---|
139 | /// return index if the substring was found or -1 otherwise. |
---|
140 | int indexOf(const char *substring,int start=0) const; |
---|
141 | |
---|
142 | /// find a substring. |
---|
143 | /// return index if the substring was found or -1 otherwise. |
---|
144 | int indexOf(const SString & substring,int start=0) const; |
---|
145 | |
---|
146 | operator const char*() const {return buf->txt;} ///< get SString's readonly buffer |
---|
147 | //operator char*() {detachCopy(len()); return buf->txt;} ///< get SString's writable buffer |
---|
148 | void operator=(const char*t); ///< assign from const char* |
---|
149 | //void operator=(int x) {free(txt);nowy(x);} ///< clear string and make new empty one |
---|
150 | void operator=(const SString &s); |
---|
151 | |
---|
152 | void append(const char *txt,int count); |
---|
153 | SString operator+(const SString &s) const; |
---|
154 | void operator+=(int x); ///< append x spaces after current string |
---|
155 | void operator+=(const char*); ///< append char* contents |
---|
156 | void operator+=(const SString&); ///< append other SString |
---|
157 | |
---|
158 | bool equals(const SString &s) const; ///< TRUE if equal |
---|
159 | bool operator==(const SString &s) const {return equals(s);} ///< TRUE if equal |
---|
160 | bool operator!=(const SString &s) const {return !equals(s);} |
---|
161 | const char* operator()(int p) const {return buf->txt+p;} ///< pointer to p'th character in SString |
---|
162 | char operator[](int i) const {return buf->txt[i];} ///< get char like in array |
---|
163 | |
---|
164 | /// return a substring of the current string |
---|
165 | SString substr(int begin, int length=1<<30) const; |
---|
166 | |
---|
167 | /// simple tokenization: |
---|
168 | /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character |
---|
169 | /// and put it in output parameter <B>token</B>. |
---|
170 | /// <B>pos</B> is moved accordingly. |
---|
171 | /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. |
---|
172 | int getNextToken(int& pos,SString &token,char separator) const; |
---|
173 | |
---|
174 | void operator+=(char ch) {directAppend(1)[0]=ch;endAppend(1);} ///< append single character |
---|
175 | |
---|
176 | int startsWith(const char *pattern) const; |
---|
177 | char charAt(int pos) const {return buf->txt[pos];} |
---|
178 | uint32_t hash() const {return buf->hash();} |
---|
179 | |
---|
180 | static SString valueOf(int); |
---|
181 | static SString valueOf(long); |
---|
182 | static SString valueOf(double); |
---|
183 | static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
184 | static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
185 | static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
186 | static SString sprintf(const char* format, ...); |
---|
187 | |
---|
188 | static SString &empty(); |
---|
189 | }; |
---|
190 | |
---|
191 | #endif //#ifdef SSTRING_SIMPLE |
---|
192 | |
---|
193 | #endif |
---|