1 | #ifndef _SSTRING_SIMPLE_H_ |
---|
2 | #define _SSTRING_SIMPLE_H_ |
---|
3 | |
---|
4 | #include <stdint.h> |
---|
5 | #include <string.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <stdio.h> |
---|
8 | |
---|
9 | class ExtValue; //this include would result in recurrent inclusion: #include "extvalue.h" |
---|
10 | class ExtObject; |
---|
11 | |
---|
12 | class SString |
---|
13 | { |
---|
14 | private: |
---|
15 | char *txt; ///< string buffer or NULL for empty string |
---|
16 | int size; ///< allocated memory (including \0) |
---|
17 | int used; ///< string length |
---|
18 | int appending; ///< append mode, changes can occur after character # 'appending' |
---|
19 | |
---|
20 | void initEmpty(); |
---|
21 | void copyFrom(SString &from); ///< copy from SString |
---|
22 | void resize(int newsize); |
---|
23 | void ensureSize(int needed); |
---|
24 | const char* getPtr() const { return txt ? txt : ""; } |
---|
25 | |
---|
26 | public: |
---|
27 | SString(); ///< make an empty string |
---|
28 | SString(const char*t, int t_len = -1); ///< make a string from char* |
---|
29 | SString(int x) = delete; ///< disallow the former 'int' constructor (so the new 'char' version is not used through implicit conversion) |
---|
30 | SString(const SString& from); ///< duplicate string |
---|
31 | SString(SString&& from);///< move |
---|
32 | SString(char in); |
---|
33 | ~SString(); |
---|
34 | |
---|
35 | void copyFrom(const char* ch, int chlen = -1); ///< copy string, length of -1 == unknown |
---|
36 | |
---|
37 | void* operator new(size_t s, void* mem){ return mem; } |
---|
38 | #ifdef _MSC_VER |
---|
39 | void operator delete(void* mem, void* t) {} |
---|
40 | #endif |
---|
41 | void* operator new(size_t s){ return malloc(sizeof(SString)); } |
---|
42 | void operator delete(void* mem) { free(mem); } |
---|
43 | |
---|
44 | int len() const { return used; } ///< get string length |
---|
45 | void shrink(); ///< free unnecessary buffer |
---|
46 | void reserve(int needed) { ensureSize(needed); } ///< like in std::string |
---|
47 | |
---|
48 | /// after this call, you can modify sstring directly. |
---|
49 | /// returned value is the pointer to the internal buffer. |
---|
50 | /// <B>ensuresize</B> is minimal value of bytes you need, |
---|
51 | /// the buffer will be resized as needed. |
---|
52 | /// all "direct" operations have to leave the buffer with trailing '\0' |
---|
53 | /// at the end. endWrite() will search for this value in order to determine |
---|
54 | /// new string length. |
---|
55 | /// <P>Sample:<CODE> |
---|
56 | /// SString t; |
---|
57 | /// sprintf(t.directWrite(50),"a=%d,b=%f",a,b); |
---|
58 | /// t.endWrite();</CODE> |
---|
59 | char *directWrite(int ensuresize = -1); |
---|
60 | //char *directWrite(); |
---|
61 | /// like directWrite, but it returns the pointer to the first char after current string |
---|
62 | /// for easy appending. <B>maxappend</B> is minimum of character in buffer |
---|
63 | /// that can be appended after this call. |
---|
64 | /// <P>Sample:<CODE> |
---|
65 | /// SString t; |
---|
66 | /// sprintf(t.directAppend(10),"c=%d",c); |
---|
67 | /// t.endAppend();</CODE> |
---|
68 | char *directAppend(int maxappend = 0); |
---|
69 | /// update string length, after directWrite. |
---|
70 | /// you don't have to to call endWrite after directWrite if the string's length doesn't change. |
---|
71 | /// optional <B>newlength</B> parameter gives a chance to further optimize |
---|
72 | /// this operation if you know exact length of resulting string. |
---|
73 | /// <P>Sample:<CODE> |
---|
74 | /// SString t("samplestring"); |
---|
75 | /// strncpy(t.directWrite(50),src,bytecount); |
---|
76 | /// t.endWrite(bytecount);</CODE> |
---|
77 | void endWrite(int newlength = -1); |
---|
78 | /// update string length, after directAppend. |
---|
79 | /// you will usually need to call endAppend (or endWrite) after directAppend, |
---|
80 | /// because the purpose of directAppend is to change string's length. |
---|
81 | /// optional <B>appendlength</B> parameter gives a chance to further optimize |
---|
82 | /// this operation if you know exact length of the appended string. |
---|
83 | /// <P>Sample:<CODE> |
---|
84 | /// SString t("samplestring"); |
---|
85 | /// strncpy(t.directAppend(50),src,bytecount); |
---|
86 | /// t.endAppend(bytecount);</CODE> |
---|
87 | void endAppend(int appendlength = -1); |
---|
88 | |
---|
89 | void memoryHint(int howbig) { ensureSize(howbig); } |
---|
90 | int directMaxLen() { return size - 1; } ///< when called after directWrite: max number of characters allowed (can be more than requested) |
---|
91 | |
---|
92 | /// find a character in SString. |
---|
93 | /// return index if the character was found or -1 otherwise. |
---|
94 | int indexOf(int character, int start = 0) const; |
---|
95 | |
---|
96 | /// find a substring. |
---|
97 | /// return index if the substring was found or -1 otherwise. |
---|
98 | int indexOf(const char *substring, int start = 0) const; |
---|
99 | |
---|
100 | /// find a substring. |
---|
101 | /// return index if the substring was found or -1 otherwise. |
---|
102 | int indexOf(const SString & substring, int start = 0) const; |
---|
103 | |
---|
104 | const char* c_str() const { return getPtr(); } ///< get SString's readonly buffer |
---|
105 | void operator=(const char*t); ///< assign from const char* |
---|
106 | void operator=(const SString &s); |
---|
107 | |
---|
108 | void append(const char *t, int n); |
---|
109 | SString operator+(const SString &s) const; |
---|
110 | void operator+=(int x); ///< append x spaces after current string |
---|
111 | void operator+=(const char*); ///< append char* contents |
---|
112 | void operator+=(const SString&); ///< append other SString |
---|
113 | |
---|
114 | bool equals(const SString &s) const; ///< TRUE if equal |
---|
115 | bool operator==(const SString &s) const { return equals(s); } ///< TRUE if equal |
---|
116 | bool operator!=(const SString &s) const { return !equals(s); } ///< TRUE if not equal |
---|
117 | bool operator<(const SString &s) const { return strcmp(getPtr(), s.getPtr()) < 1; } |
---|
118 | const char* operator()(int p) const { return getPtr() + p; } ///< pointer to p'th character in SString |
---|
119 | char operator[](int i) const { return getPtr()[i]; } ///< get char like in array |
---|
120 | |
---|
121 | /// return a substring of the current string |
---|
122 | SString substr(int begin, int length = 1 << 30) const; |
---|
123 | |
---|
124 | /// simple tokenization: |
---|
125 | /// starting at <B>pos</B>, get next substring delimited by <B>separator</B> character |
---|
126 | /// and put it in output parameter <B>token</B>. |
---|
127 | /// <B>pos</B> is moved accordingly. |
---|
128 | /// returns <B>false</B> if no more tokens are available or <B>true</B> otherwise. |
---|
129 | bool getNextToken(int& pos, SString &token, char separator) const; |
---|
130 | |
---|
131 | void operator+=(char ch) { directAppend(1)[0] = ch; endAppend(1); } ///< append single character |
---|
132 | |
---|
133 | bool startsWith(const char *pattern) const; |
---|
134 | char charAt(int pos) const { return operator[](pos); } |
---|
135 | uint32_t hash() const; |
---|
136 | |
---|
137 | static SString valueOf(int); |
---|
138 | static SString valueOf(long); |
---|
139 | static SString valueOf(double); |
---|
140 | static SString valueOf(const SString&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
141 | static SString valueOf(const ExtValue&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
142 | static SString valueOf(const ExtObject&); //tylko do kompletu zeby mozna uzyc tej funkcji nie martwiac sie o typ argumentu |
---|
143 | static SString sprintf(const char* format, ...); |
---|
144 | |
---|
145 | static SString &empty(); |
---|
146 | }; |
---|
147 | |
---|
148 | #endif |
---|