[385] | 1 | #include "sstring.h" |
---|
| 2 | #include <common/nonstd_stl.h> |
---|
| 3 | #include "extvalue.h" |
---|
| 4 | #include <assert.h> |
---|
[826] | 5 | #ifdef USE_PRINTFLOAT_DRAGON4 |
---|
| 6 | #include <PrintFloat/PrintFloat.h> |
---|
| 7 | #endif |
---|
[385] | 8 | |
---|
| 9 | void SString::initEmpty() |
---|
| 10 | { |
---|
[793] | 11 | txt = NULL; used = 0; size = 0; |
---|
[385] | 12 | } |
---|
| 13 | |
---|
| 14 | SString::SString() |
---|
| 15 | { |
---|
[793] | 16 | initEmpty(); |
---|
[385] | 17 | } |
---|
| 18 | |
---|
| 19 | SString::~SString() |
---|
| 20 | { |
---|
[793] | 21 | resize(0); |
---|
[385] | 22 | } |
---|
| 23 | |
---|
[793] | 24 | SString::SString(const char *t, int t_len) |
---|
[385] | 25 | { |
---|
[793] | 26 | initEmpty(); |
---|
| 27 | if (!t) return; |
---|
| 28 | copyFrom(t, t_len); |
---|
[385] | 29 | } |
---|
| 30 | |
---|
| 31 | SString::SString(const SString &from) |
---|
| 32 | { |
---|
[793] | 33 | initEmpty(); |
---|
| 34 | operator=(from); |
---|
[385] | 35 | } |
---|
| 36 | |
---|
| 37 | SString::SString(SString&& from) |
---|
| 38 | { |
---|
[793] | 39 | txt = from.txt; size = from.size; used = from.used; |
---|
| 40 | from.txt = NULL; from.size = 0; from.used = 0; |
---|
[385] | 41 | } |
---|
| 42 | |
---|
[955] | 43 | SString::SString(char in) |
---|
| 44 | { |
---|
| 45 | initEmpty(); |
---|
| 46 | append(&in, 1); |
---|
| 47 | } |
---|
| 48 | |
---|
[385] | 49 | void SString::resize(int newsize) |
---|
| 50 | { |
---|
[793] | 51 | if (newsize == size) return; |
---|
| 52 | txt = (char*)realloc(txt, newsize); |
---|
| 53 | size = newsize; |
---|
[385] | 54 | } |
---|
| 55 | |
---|
| 56 | void SString::ensureSize(int needed) |
---|
| 57 | { |
---|
[793] | 58 | if (size > needed) return; |
---|
| 59 | resize((size > 0) ? (needed + needed / 2 + 1) : (needed + 1)); |
---|
[385] | 60 | } |
---|
| 61 | |
---|
| 62 | char *SString::directWrite(int ensuresize) |
---|
| 63 | { |
---|
[793] | 64 | ensureSize(ensuresize); |
---|
| 65 | appending = used; |
---|
| 66 | return txt; |
---|
[385] | 67 | } |
---|
| 68 | |
---|
| 69 | char *SString::directAppend(int maxappend) |
---|
| 70 | { |
---|
[793] | 71 | ensureSize(used + maxappend); |
---|
| 72 | appending = used; |
---|
| 73 | return txt + appending; |
---|
[385] | 74 | } |
---|
| 75 | |
---|
| 76 | void SString::endWrite(int newlength) |
---|
| 77 | { |
---|
[793] | 78 | if (newlength < 0) newlength = strlen(txt); |
---|
| 79 | else txt[newlength] = 0; |
---|
| 80 | used = newlength; |
---|
| 81 | assert(used < size); |
---|
[385] | 82 | } |
---|
| 83 | |
---|
| 84 | void SString::endAppend(int newappend) |
---|
| 85 | { |
---|
[793] | 86 | if (newappend < 0) newappend = strlen(txt + appending); |
---|
| 87 | else txt[appending + newappend] = 0; |
---|
| 88 | used = appending + newappend; |
---|
| 89 | assert(used < size); |
---|
[385] | 90 | } |
---|
| 91 | |
---|
| 92 | ////////////// append ///////////////// |
---|
| 93 | |
---|
| 94 | void SString::operator+=(const char *s) |
---|
| 95 | { |
---|
[793] | 96 | if (!s) return; |
---|
| 97 | int x = strlen(s); |
---|
| 98 | if (!x) return; |
---|
| 99 | append(s, x); |
---|
[385] | 100 | } |
---|
| 101 | |
---|
[793] | 102 | void SString::append(const char *t, int n) |
---|
[385] | 103 | { |
---|
[793] | 104 | if (!n) return; |
---|
| 105 | ensureSize(used + n); |
---|
| 106 | memmove(txt + used, t, n); |
---|
| 107 | used += n; |
---|
| 108 | txt[used] = 0; |
---|
[385] | 109 | } |
---|
| 110 | |
---|
| 111 | void SString::operator+=(const SString&s) |
---|
| 112 | { |
---|
[793] | 113 | append(s.c_str(), s.len()); |
---|
[385] | 114 | } |
---|
| 115 | |
---|
| 116 | SString SString::operator+(const SString& s) const |
---|
| 117 | { |
---|
[955] | 118 | SString ret; |
---|
| 119 | ret.reserve(len() + s.len()); |
---|
[793] | 120 | ret = *this; |
---|
| 121 | ret += s; |
---|
| 122 | return ret; |
---|
[385] | 123 | } |
---|
| 124 | |
---|
| 125 | ///////////////////////////// |
---|
| 126 | |
---|
[793] | 127 | void SString::copyFrom(const char *ch, int chlen) |
---|
[385] | 128 | { |
---|
[793] | 129 | if (!ch) chlen = 0; |
---|
| 130 | else if (chlen < 0) chlen = strlen(ch); |
---|
| 131 | if (chlen) |
---|
[385] | 132 | { |
---|
[793] | 133 | ensureSize(chlen); |
---|
| 134 | memmove(txt, ch, chlen); |
---|
| 135 | txt[chlen] = 0; |
---|
| 136 | used = chlen; |
---|
[385] | 137 | } |
---|
[793] | 138 | else |
---|
[385] | 139 | { |
---|
[793] | 140 | if (txt) |
---|
[385] | 141 | { |
---|
[793] | 142 | txt[0] = 0; |
---|
| 143 | used = 0; |
---|
[385] | 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void SString::operator=(const char *ch) |
---|
| 149 | { |
---|
[793] | 150 | copyFrom(ch); |
---|
[385] | 151 | } |
---|
| 152 | |
---|
| 153 | void SString::operator=(const SString&s) |
---|
| 154 | { |
---|
[793] | 155 | if (&s == this) return; |
---|
| 156 | copyFrom(s.c_str(), s.len()); |
---|
[385] | 157 | } |
---|
| 158 | |
---|
| 159 | /////////////////////////////////////// |
---|
| 160 | |
---|
| 161 | SString SString::substr(int begin, int length) const |
---|
| 162 | { |
---|
[793] | 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); |
---|
[385] | 168 | } |
---|
| 169 | |
---|
| 170 | /////////////////////////////////////// |
---|
| 171 | |
---|
[395] | 172 | bool SString::equals(const SString& s) const |
---|
[385] | 173 | { |
---|
[793] | 174 | if (this == &s) return true; |
---|
| 175 | if (len() != s.len()) return false; |
---|
| 176 | return strcmp(getPtr(), s.getPtr()) == 0; |
---|
[385] | 177 | } |
---|
| 178 | |
---|
| 179 | /////////////////////////////////////// |
---|
| 180 | |
---|
[793] | 181 | int SString::indexOf(int character, int start) const |
---|
[385] | 182 | { |
---|
[793] | 183 | const char *found = strchr(getPtr() + start, character); |
---|
| 184 | return found ? found - getPtr() : -1; |
---|
[385] | 185 | } |
---|
| 186 | |
---|
[793] | 187 | int SString::indexOf(const char *substring, int start) const |
---|
[385] | 188 | { |
---|
[793] | 189 | const char *found = strstr(getPtr() + start, substring); |
---|
| 190 | return found ? found - getPtr() : -1; |
---|
[385] | 191 | } |
---|
| 192 | |
---|
[793] | 193 | int SString::indexOf(const SString & substring, int start) const |
---|
[385] | 194 | { |
---|
[793] | 195 | const char *found = strstr(getPtr() + start, substring.c_str()); |
---|
| 196 | return found ? found - getPtr() : -1; |
---|
[385] | 197 | } |
---|
| 198 | |
---|
[793] | 199 | bool SString::getNextToken(int& pos, SString &token, char separator) const |
---|
[385] | 200 | { |
---|
[793] | 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; |
---|
[385] | 209 | } |
---|
| 210 | |
---|
[395] | 211 | bool SString::startsWith(const char *pattern) const |
---|
[385] | 212 | { |
---|
[793] | 213 | const char *t = this->c_str(); |
---|
| 214 | for (; *pattern; pattern++, t++) |
---|
| 215 | if (*t != *pattern) return false; |
---|
| 216 | return true; |
---|
[385] | 217 | } |
---|
| 218 | |
---|
| 219 | SString SString::valueOf(int i) |
---|
| 220 | { |
---|
[793] | 221 | return SString::sprintf("%d", i); |
---|
[385] | 222 | } |
---|
| 223 | SString SString::valueOf(long i) |
---|
| 224 | { |
---|
[793] | 225 | return SString::sprintf("%d", i); |
---|
[385] | 226 | } |
---|
| 227 | SString SString::valueOf(double d) |
---|
| 228 | { |
---|
[826] | 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 |
---|
[793] | 239 | if ((!strchr(tmp.c_str(), '.')) && (!strchr(tmp.c_str(), 'e'))) tmp += ".0"; |
---|
| 240 | return tmp; |
---|
[385] | 241 | } |
---|
| 242 | SString SString::valueOf(const SString& s) |
---|
| 243 | { |
---|
[793] | 244 | return s; |
---|
[385] | 245 | } |
---|
| 246 | |
---|
| 247 | SString SString::sprintf(const char* format, ...) |
---|
| 248 | { |
---|
[793] | 249 | int n, size = 30; |
---|
| 250 | va_list ap; |
---|
[385] | 251 | |
---|
[793] | 252 | SString ret; |
---|
[385] | 253 | |
---|
| 254 | #ifdef USE_VSCPRINTF |
---|
[793] | 255 | va_start(ap, format); |
---|
| 256 | size = _vscprintf(format, ap); |
---|
| 257 | va_end(ap); |
---|
[385] | 258 | #endif |
---|
| 259 | |
---|
[793] | 260 | while (1) |
---|
[385] | 261 | { |
---|
[793] | 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. */ |
---|
[897] | 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 | |
---|
[793] | 293 | if (n > -1 && n < size) |
---|
[385] | 294 | { |
---|
[793] | 295 | ret.endWrite(n); |
---|
| 296 | return ret; |
---|
[385] | 297 | } |
---|
[793] | 298 | /* Else try again with more space. */ |
---|
[385] | 299 | #ifdef VSNPRINTF_RETURNS_REQUIRED_SIZE |
---|
[793] | 300 | if (n > -1) /* glibc 2.1 */ |
---|
| 301 | size = n; /* precisely what is needed */ |
---|
| 302 | else /* glibc 2.0 */ |
---|
[385] | 303 | #endif |
---|
[793] | 304 | size *= 2; /* twice the old size */ |
---|
[385] | 305 | } |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | SString &SString::empty() |
---|
| 309 | { |
---|
[793] | 310 | static SString empty; |
---|
| 311 | return empty; |
---|
[385] | 312 | } |
---|