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

Last change on this file since 1092 was 1040, checked in by Maciej Komosinski, 4 years ago

Follow-up to r897: A workaround for Android bug in vsnprintf() and vsprintf() in SString, https://github.com/android-ndk/ndk/issues/879

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