source: cpp/frams/util/sstring-simple.cpp @ 955

Last change on this file since 955 was 955, checked in by Maciej Komosinski, 3 months ago

Genetic format ID becomes a string (no longer limited to a single character)

  • Property svn:eol-style set to native
File size: 7.4 KB
Line 
1#include "sstring.h"
2#include <common/nonstd_stl.h>
3#include "extvalue.h"
4#include <assert.h>
5#ifdef USE_PRINTFLOAT_DRAGON4
6#include <PrintFloat/PrintFloat.h>
7#endif
8
9void SString::initEmpty()
10{
11        txt = NULL; used = 0; size = 0;
12}
13
14SString::SString()
15{
16        initEmpty();
17}
18
19SString::~SString()
20{
21        resize(0);
22}
23
24SString::SString(const char *t, int t_len)
25{
26        initEmpty();
27        if (!t) return;
28        copyFrom(t, t_len);
29}
30
31SString::SString(const SString &from)
32{
33        initEmpty();
34        operator=(from);
35}
36
37SString::SString(SString&& from)
38{
39        txt = from.txt; size = from.size; used = from.used;
40        from.txt = NULL; from.size = 0; from.used = 0;
41}
42
43SString::SString(char in)
44{
45        initEmpty();
46        append(&in, 1);
47}
48
49void SString::resize(int newsize)
50{
51        if (newsize == size) return;
52        txt = (char*)realloc(txt, newsize);
53        size = newsize;
54}
55
56void SString::ensureSize(int needed)
57{
58        if (size > needed) return;
59        resize((size > 0) ? (needed + needed / 2 + 1) : (needed + 1));
60}
61
62char *SString::directWrite(int ensuresize)
63{
64        ensureSize(ensuresize);
65        appending = used;
66        return txt;
67}
68
69char *SString::directAppend(int maxappend)
70{
71        ensureSize(used + maxappend);
72        appending = used;
73        return txt + appending;
74}
75
76void SString::endWrite(int newlength)
77{
78        if (newlength < 0) newlength = strlen(txt);
79        else txt[newlength] = 0;
80        used = newlength;
81        assert(used < size);
82}
83
84void SString::endAppend(int newappend)
85{
86        if (newappend < 0) newappend = strlen(txt + appending);
87        else txt[appending + newappend] = 0;
88        used = appending + newappend;
89        assert(used < size);
90}
91
92////////////// append /////////////////
93
94void SString::operator+=(const char *s)
95{
96        if (!s) return;
97        int x = strlen(s);
98        if (!x) return;
99        append(s, x);
100}
101
102void SString::append(const char *t, int n)
103{
104        if (!n) return;
105        ensureSize(used + n);
106        memmove(txt + used, t, n);
107        used += n;
108        txt[used] = 0;
109}
110
111void SString::operator+=(const SString&s)
112{
113        append(s.c_str(), s.len());
114}
115
116SString SString::operator+(const SString& s) const
117{
118        SString ret;
119        ret.reserve(len() + s.len());
120        ret = *this;
121        ret += s;
122        return ret;
123}
124
125/////////////////////////////
126
127void SString::copyFrom(const char *ch, int chlen)
128{
129        if (!ch) chlen = 0;
130        else if (chlen < 0) chlen = strlen(ch);
131        if (chlen)
132        {
133                ensureSize(chlen);
134                memmove(txt, ch, chlen);
135                txt[chlen] = 0;
136                used = chlen;
137        }
138        else
139        {
140                if (txt)
141                {
142                        txt[0] = 0;
143                        used = 0;
144                }
145        }
146}
147
148void SString::operator=(const char *ch)
149{
150        copyFrom(ch);
151}
152
153void SString::operator=(const SString&s)
154{
155        if (&s == this) return;
156        copyFrom(s.c_str(), s.len());
157}
158
159///////////////////////////////////////
160
161SString SString::substr(int begin, int length) const
162{
163        if (begin < 0) { length += begin; begin = 0; }
164        if (length >= (len() - begin)) length = len() - begin;
165        if (length <= 0) return SString();
166        if (length == len()) return *this;
167        return SString((*this)(begin), length);
168}
169
170///////////////////////////////////////
171
172bool SString::equals(const SString& s) const
173{
174        if (this == &s) return true;
175        if (len() != s.len()) return false;
176        return strcmp(getPtr(), s.getPtr()) == 0;
177}
178
179///////////////////////////////////////
180
181int SString::indexOf(int character, int start) const
182{
183        const char *found = strchr(getPtr() + start, character);
184        return found ? found - getPtr() : -1;
185}
186
187int SString::indexOf(const char *substring, int start) const
188{
189        const char *found = strstr(getPtr() + start, substring);
190        return found ? found - getPtr() : -1;
191}
192
193int SString::indexOf(const SString & substring, int start) const
194{
195        const char *found = strstr(getPtr() + start, substring.c_str());
196        return found ? found - getPtr() : -1;
197}
198
199bool SString::getNextToken(int& pos, SString &token, char separator) const
200{
201        if (pos >= len()) { token = 0; return false; }
202        int p1 = pos, p2;
203        const char *t1 = getPtr() + pos;
204        const char *t2 = strchr(t1, separator);
205        if (t2) pos = (p2 = (t2 - getPtr())) + 1; else p2 = pos = len();
206        strncpy(token.directWrite(p2 - p1), t1, p2 - p1);
207        token.endWrite(p2 - p1);
208        return true;
209}
210
211bool SString::startsWith(const char *pattern) const
212{
213        const char *t = this->c_str();
214        for (; *pattern; pattern++, t++)
215                if (*t != *pattern) return false;
216        return true;
217}
218
219SString SString::valueOf(int i)
220{
221        return SString::sprintf("%d", i);
222}
223SString SString::valueOf(long i)
224{
225        return SString::sprintf("%d", i);
226}
227SString SString::valueOf(double d)
228{
229#ifdef USE_PRINTFLOAT_DRAGON4
230        SString tmp;
231        char* here = tmp.directWrite(30);
232        tmp.endWrite(PrintFloat64(here, 30, d,
233                ((d < -1e17) || (d > 1e17) || ((d < 1e-4) && (d > -1e-4) && (d != 0.0)))
234                ? PrintFloatFormat_Scientific : PrintFloatFormat_Positional,
235                -1));//http://www.ryanjuckett.com/programming/printing-floating-point-numbers/
236#else
237        SString tmp = SString::sprintf("%.17g", d); //https://stackoverflow.com/questions/16839658/printf-width-specifier-to-maintain-precision-of-floating-point-value
238#endif
239        if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0";
240        return tmp;
241}
242SString SString::valueOf(const SString& s)
243{
244        return s;
245}
246
247SString SString::sprintf(const char* format, ...)
248{
249        int n, size = 30;
250        va_list ap;
251
252        SString ret;
253
254#ifdef USE_VSCPRINTF
255        va_start(ap, format);
256        size = _vscprintf(format, ap);
257        va_end(ap);
258#endif
259
260        while (1)
261        {
262                char* p = ret.directWrite(size);
263                assert(p != NULL);
264                size = ret.directMaxLen() + 1;
265                /* Try to print in the allocated space. */
266                va_start(ap, format);
267                n = vsnprintf(p, size, format, ap);
268                va_end(ap);
269                /* If that worked, return the string. */
270
271#ifdef __ANDROID__
272                //Workaround for Android bug. /system/lib64/libc.so? maybe only arm 64-bit? "If an encoding error occurs, a negative number is returned". On some devices keeps returning -1 forever.
273                //https://github.com/android-ndk/ndk/issues/879 but unfortunately during google play tests (Firebase Test Lab) this problem turned out to be not limited to Chinese devices and occurred in Mate 9, Galaxy S9, Pixel, Pixel 2, Moto Z (even with the en_GB locale; the locale is not important but the problem seem to be utf8 non-ascii chars in the format string).
274                if (n < 0 && size >= (1 << 24)) //wants more than 16M
275                {
276                        buf[size - 1] = 0; //just to ensure there is at least some ending \0 in memory... who knows what buggy vsnprintf() did.
277                        __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "Giving up due to Android bug: vsnprintf() wants more than %d bytes, it used %zu bytes, for format='%s'", size, strlen(buf), format);
278                        //in my tests, it always used 0 bytes, so it produced a 0-length string: ""
279                        va_copy(ap_copy, ap);
280                        n = vsprintf(buf, format, ap_copy); //hoping 16M is enough
281                        va_end(ap_copy);
282                        __android_log_print(ANDROID_LOG_INFO, LOG_APP_NAME, "Fallback to vsprintf() produced string: '%s'", buf);
283                        if (n < 0) //vsprintf was also buggy. If we were strict, we should abort the app now.
284                        {
285                                strcpy(buf, "[STR_ERR] "); //a special prefix just to indicate the returned string is incorrect
286                                strcat(buf, format); //append and return the original formatting string
287                                __android_log_print(ANDROID_LOG_ERROR, LOG_APP_NAME, "vsprintf() also failed, using the incorrect resulting string: '%s'", buf);
288                        }
289                        n = strlen(buf); //pretend vsnprintf() or vsprintf() was OK to exit the endless loop
290                }
291#endif
292
293                if (n > -1 && n < size)
294                {
295                        ret.endWrite(n);
296                        return ret;
297                }
298                /* Else try again with more space. */
299#ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE
300                if (n > -1)    /* glibc 2.1 */
301                        size = n; /* precisely what is needed */
302                else           /* glibc 2.0 */
303#endif
304                        size *= 2;  /* twice the old size */
305        }
306}
307
308SString &SString::empty()
309{
310        static SString empty;
311        return empty;
312}
Note: See TracBrowser for help on using the repository browser.