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