SDL_string.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #if defined(__clang_analyzer__)
  19. #define SDL_DISABLE_ANALYZE_MACROS 1
  20. #endif
  21. #include "../SDL_internal.h"
  22. /* This file contains portable string manipulation functions for SDL */
  23. #include "SDL_stdinc.h"
  24. #include "SDL_vacopy.h"
  25. #if defined(__vita__)
  26. #include <psp2/kernel/clib.h>
  27. #endif
  28. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  29. #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
  30. #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
  31. #endif
  32. #define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
  33. #define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
  34. static size_t UTF8_TrailingBytes(unsigned char c)
  35. {
  36. if (c >= 0xC0 && c <= 0xDF) {
  37. return 1;
  38. } else if (c >= 0xE0 && c <= 0xEF) {
  39. return 2;
  40. } else if (c >= 0xF0 && c <= 0xF4) {
  41. return 3;
  42. }
  43. return 0;
  44. }
  45. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  46. /**
  47. * Parses an unsigned long long and returns the unsigned value and sign bit.
  48. *
  49. * Positive values are clamped to ULLONG_MAX.
  50. * The result `value == 0 && negative` indicates negative overflow
  51. * and might need to be handled differently depending on whether a
  52. * signed or unsigned integer is being parsed.
  53. */
  54. static size_t SDL_ScanUnsignedLongLongInternal(const char *text, int count, int radix, unsigned long long *valuep, SDL_bool *negativep)
  55. {
  56. const unsigned long long ullong_max = ~0ULL;
  57. const char *text_start = text;
  58. const char *number_start = text_start;
  59. unsigned long long value = 0;
  60. SDL_bool negative = SDL_FALSE;
  61. SDL_bool overflow = SDL_FALSE;
  62. if (radix == 0 || (radix >= 2 && radix <= 36)) {
  63. while (SDL_isspace(*text)) {
  64. ++text;
  65. }
  66. if (*text == '-' || *text == '+') {
  67. negative = *text == '-';
  68. ++text;
  69. }
  70. if ((radix == 0 || radix == 16) && *text == '0' && (text[1] == 'x' || text[1] == 'X')) {
  71. text += 2;
  72. radix = 16;
  73. } else if (radix == 0 && *text == '0' && (text[1] >= '0' && text[1] <= '9')) {
  74. ++text;
  75. radix = 8;
  76. } else if (radix == 0) {
  77. radix = 10;
  78. }
  79. number_start = text;
  80. do {
  81. unsigned long long digit;
  82. if (*text >= '0' && *text <= '9') {
  83. digit = *text - '0';
  84. } else if (radix > 10) {
  85. if (*text >= 'A' && *text < 'A' + (radix - 10)) {
  86. digit = 10 + (*text - 'A');
  87. } else if (*text >= 'a' && *text < 'a' + (radix - 10)) {
  88. digit = 10 + (*text - 'a');
  89. } else {
  90. break;
  91. }
  92. } else {
  93. break;
  94. }
  95. if (value != 0 && radix > ullong_max / value) {
  96. overflow = SDL_TRUE;
  97. } else {
  98. value *= radix;
  99. if (digit > ullong_max - value) {
  100. overflow = SDL_TRUE;
  101. } else {
  102. value += digit;
  103. }
  104. }
  105. ++text;
  106. } while (count == 0 || (text - text_start) != count);
  107. }
  108. if (text == number_start) {
  109. if (radix == 16 && text > text_start && (*(text - 1) == 'x' || *(text - 1) == 'X')) {
  110. // the string was "0x"; consume the '0' but not the 'x'
  111. --text;
  112. } else {
  113. // no number was parsed, and thus no characters were consumed
  114. text = text_start;
  115. }
  116. }
  117. if (overflow) {
  118. if (negative) {
  119. value = 0;
  120. } else {
  121. value = ullong_max;
  122. }
  123. } else if (value == 0) {
  124. negative = SDL_FALSE;
  125. }
  126. *valuep = value;
  127. *negativep = negative;
  128. return text - text_start;
  129. }
  130. static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
  131. {
  132. const unsigned long long_max = (~0UL) >> 1;
  133. unsigned long long value;
  134. SDL_bool negative;
  135. size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
  136. if (negative) {
  137. const unsigned long abs_long_min = long_max + 1;
  138. if (value == 0 || value > abs_long_min) {
  139. value = 0ULL - abs_long_min;
  140. } else {
  141. value = 0ULL - value;
  142. }
  143. } else if (value > long_max) {
  144. value = long_max;
  145. }
  146. *valuep = (long)value;
  147. return len;
  148. }
  149. #endif
  150. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  151. static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
  152. {
  153. const unsigned long ulong_max = ~0UL;
  154. unsigned long long value;
  155. SDL_bool negative;
  156. size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
  157. if (negative) {
  158. if (value == 0 || value > ulong_max) {
  159. value = ulong_max;
  160. } else if (value == ulong_max) {
  161. value = 1;
  162. } else {
  163. value = 0ULL - value;
  164. }
  165. } else if (value > ulong_max) {
  166. value = ulong_max;
  167. }
  168. *valuep = (unsigned long)value;
  169. return len;
  170. }
  171. #endif
  172. #ifndef HAVE_VSSCANF
  173. static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
  174. {
  175. const uintptr_t uintptr_max = ~(uintptr_t)0;
  176. unsigned long long value;
  177. SDL_bool negative;
  178. size_t len = SDL_ScanUnsignedLongLongInternal(text, 0, 16, &value, &negative);
  179. if (negative) {
  180. if (value == 0 || value > uintptr_max) {
  181. value = uintptr_max;
  182. } else if (value == uintptr_max) {
  183. value = 1;
  184. } else {
  185. value = 0ULL - value;
  186. }
  187. } else if (value > uintptr_max) {
  188. value = uintptr_max;
  189. }
  190. *valuep = (uintptr_t)value;
  191. return len;
  192. }
  193. #endif
  194. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  195. static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
  196. {
  197. const unsigned long long llong_max = (~0ULL) >> 1;
  198. unsigned long long value;
  199. SDL_bool negative;
  200. size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, &value, &negative);
  201. if (negative) {
  202. const unsigned long long abs_llong_min = llong_max + 1;
  203. if (value == 0 || value > abs_llong_min) {
  204. value = 0ULL - abs_llong_min;
  205. } else {
  206. value = 0ULL - value;
  207. }
  208. } else if (value > llong_max) {
  209. value = llong_max;
  210. }
  211. *valuep = value;
  212. return len;
  213. }
  214. #endif
  215. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
  216. static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
  217. {
  218. const unsigned long long ullong_max = ~0ULL;
  219. SDL_bool negative;
  220. size_t len = SDL_ScanUnsignedLongLongInternal(text, count, radix, valuep, &negative);
  221. if (negative) {
  222. if (*valuep == 0) {
  223. *valuep = ullong_max;
  224. } else {
  225. *valuep = 0ULL - *valuep;
  226. }
  227. }
  228. return len;
  229. }
  230. #endif
  231. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
  232. static size_t SDL_ScanFloat(const char *text, double *valuep)
  233. {
  234. const char *text_start = text;
  235. const char *number_start = text_start;
  236. double value = 0.0;
  237. SDL_bool negative = SDL_FALSE;
  238. while (SDL_isspace(*text)) {
  239. ++text;
  240. }
  241. if (*text == '-' || *text == '+') {
  242. negative = *text == '-';
  243. ++text;
  244. }
  245. number_start = text;
  246. if (SDL_isdigit(*text)) {
  247. value += SDL_strtoull(text, (char **)(&text), 10);
  248. if (*text == '.') {
  249. double denom = 10;
  250. ++text;
  251. while (SDL_isdigit(*text)) {
  252. value += (double)(*text - '0') / denom;
  253. denom *= 10;
  254. ++text;
  255. }
  256. }
  257. }
  258. if (text == number_start) {
  259. // no number was parsed, and thus no characters were consumed
  260. text = text_start;
  261. } else if (negative) {
  262. value = -value;
  263. }
  264. *valuep = value;
  265. return text - text_start;
  266. }
  267. #endif
  268. void *SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
  269. {
  270. #if defined(HAVE_MEMMOVE)
  271. return memmove(dst, src, len);
  272. #else
  273. char *srcp = (char *)src;
  274. char *dstp = (char *)dst;
  275. if (src < dst) {
  276. srcp += len - 1;
  277. dstp += len - 1;
  278. while (len--) {
  279. *dstp-- = *srcp--;
  280. }
  281. } else {
  282. while (len--) {
  283. *dstp++ = *srcp++;
  284. }
  285. }
  286. return dst;
  287. #endif /* HAVE_MEMMOVE */
  288. }
  289. int SDL_memcmp(const void *s1, const void *s2, size_t len)
  290. {
  291. #if defined(__vita__)
  292. /*
  293. Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
  294. But, both linux and bsd allow that, with an exception:
  295. zero length strings are always identical, so NULLs are never dereferenced.
  296. sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
  297. */
  298. if (len == 0) {
  299. return 0;
  300. }
  301. return sceClibMemcmp(s1, s2, len);
  302. #elif defined(HAVE_MEMCMP)
  303. return memcmp(s1, s2, len);
  304. #else
  305. char *s1p = (char *)s1;
  306. char *s2p = (char *)s2;
  307. while (len--) {
  308. if (*s1p != *s2p) {
  309. return *s1p - *s2p;
  310. }
  311. ++s1p;
  312. ++s2p;
  313. }
  314. return 0;
  315. #endif /* HAVE_MEMCMP */
  316. }
  317. size_t
  318. SDL_strlen(const char *string)
  319. {
  320. #if defined(HAVE_STRLEN)
  321. return strlen(string);
  322. #else
  323. size_t len = 0;
  324. while (*string++) {
  325. ++len;
  326. }
  327. return len;
  328. #endif /* HAVE_STRLEN */
  329. }
  330. size_t
  331. SDL_wcslen(const wchar_t *string)
  332. {
  333. #if defined(HAVE_WCSLEN)
  334. return wcslen(string);
  335. #else
  336. size_t len = 0;
  337. while (*string++) {
  338. ++len;
  339. }
  340. return len;
  341. #endif /* HAVE_WCSLEN */
  342. }
  343. size_t
  344. SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  345. {
  346. #if defined(HAVE_WCSLCPY)
  347. return wcslcpy(dst, src, maxlen);
  348. #else
  349. size_t srclen = SDL_wcslen(src);
  350. if (maxlen > 0) {
  351. size_t len = SDL_min(srclen, maxlen - 1);
  352. SDL_memcpy(dst, src, len * sizeof(wchar_t));
  353. dst[len] = '\0';
  354. }
  355. return srclen;
  356. #endif /* HAVE_WCSLCPY */
  357. }
  358. size_t
  359. SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  360. {
  361. #if defined(HAVE_WCSLCAT)
  362. return wcslcat(dst, src, maxlen);
  363. #else
  364. size_t dstlen = SDL_wcslen(dst);
  365. size_t srclen = SDL_wcslen(src);
  366. if (dstlen < maxlen) {
  367. SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
  368. }
  369. return dstlen + srclen;
  370. #endif /* HAVE_WCSLCAT */
  371. }
  372. wchar_t *SDL_wcsdup(const wchar_t *string)
  373. {
  374. size_t len = ((SDL_wcslen(string) + 1) * sizeof(wchar_t));
  375. wchar_t *newstr = (wchar_t *)SDL_malloc(len);
  376. if (newstr) {
  377. SDL_memcpy(newstr, string, len);
  378. }
  379. return newstr;
  380. }
  381. wchar_t *SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
  382. {
  383. #if defined(HAVE_WCSSTR)
  384. return SDL_const_cast(wchar_t *, wcsstr(haystack, needle));
  385. #else
  386. size_t length = SDL_wcslen(needle);
  387. while (*haystack) {
  388. if (SDL_wcsncmp(haystack, needle, length) == 0) {
  389. return (wchar_t *)haystack;
  390. }
  391. ++haystack;
  392. }
  393. return NULL;
  394. #endif /* HAVE_WCSSTR */
  395. }
  396. int SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
  397. {
  398. #if defined(HAVE_WCSCMP)
  399. return wcscmp(str1, str2);
  400. #else
  401. while (*str1 && *str2) {
  402. if (*str1 != *str2) {
  403. break;
  404. }
  405. ++str1;
  406. ++str2;
  407. }
  408. return *str1 - *str2;
  409. #endif /* HAVE_WCSCMP */
  410. }
  411. int SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
  412. {
  413. #if defined(HAVE_WCSNCMP)
  414. return wcsncmp(str1, str2, maxlen);
  415. #else
  416. while (*str1 && *str2 && maxlen) {
  417. if (*str1 != *str2) {
  418. break;
  419. }
  420. ++str1;
  421. ++str2;
  422. --maxlen;
  423. }
  424. if (!maxlen) {
  425. return 0;
  426. }
  427. return *str1 - *str2;
  428. #endif /* HAVE_WCSNCMP */
  429. }
  430. int SDL_wcscasecmp(const wchar_t *str1, const wchar_t *str2)
  431. {
  432. #if defined(HAVE_WCSCASECMP)
  433. return wcscasecmp(str1, str2);
  434. #elif defined(HAVE__WCSICMP)
  435. return _wcsicmp(str1, str2);
  436. #else
  437. wchar_t a = 0;
  438. wchar_t b = 0;
  439. while (*str1 && *str2) {
  440. /* FIXME: This doesn't actually support wide characters */
  441. if (*str1 >= 0x80 || *str2 >= 0x80) {
  442. a = *str1;
  443. b = *str2;
  444. } else {
  445. a = SDL_toupper((unsigned char)*str1);
  446. b = SDL_toupper((unsigned char)*str2);
  447. }
  448. if (a != b) {
  449. break;
  450. }
  451. ++str1;
  452. ++str2;
  453. }
  454. /* FIXME: This doesn't actually support wide characters */
  455. if (*str1 >= 0x80 || *str2 >= 0x80) {
  456. a = *str1;
  457. b = *str2;
  458. } else {
  459. a = SDL_toupper((unsigned char)*str1);
  460. b = SDL_toupper((unsigned char)*str2);
  461. }
  462. return (int)((unsigned int)a - (unsigned int)b);
  463. #endif /* HAVE__WCSICMP */
  464. }
  465. int SDL_wcsncasecmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
  466. {
  467. #if defined(HAVE_WCSNCASECMP)
  468. return wcsncasecmp(str1, str2, maxlen);
  469. #elif defined(HAVE__WCSNICMP)
  470. return _wcsnicmp(str1, str2, maxlen);
  471. #else
  472. wchar_t a = 0;
  473. wchar_t b = 0;
  474. while (*str1 && *str2 && maxlen) {
  475. /* FIXME: This doesn't actually support wide characters */
  476. if (*str1 >= 0x80 || *str2 >= 0x80) {
  477. a = *str1;
  478. b = *str2;
  479. } else {
  480. a = SDL_toupper((unsigned char)*str1);
  481. b = SDL_toupper((unsigned char)*str2);
  482. }
  483. if (a != b) {
  484. break;
  485. }
  486. ++str1;
  487. ++str2;
  488. --maxlen;
  489. }
  490. if (maxlen == 0) {
  491. return 0;
  492. } else {
  493. /* FIXME: This doesn't actually support wide characters */
  494. if (*str1 >= 0x80 || *str2 >= 0x80) {
  495. a = *str1;
  496. b = *str2;
  497. } else {
  498. a = SDL_toupper((unsigned char)*str1);
  499. b = SDL_toupper((unsigned char)*str2);
  500. }
  501. return (int)((unsigned int)a - (unsigned int)b);
  502. }
  503. #endif /* HAVE__WCSNICMP */
  504. }
  505. size_t
  506. SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  507. {
  508. #if defined(HAVE_STRLCPY)
  509. return strlcpy(dst, src, maxlen);
  510. #else
  511. size_t srclen = SDL_strlen(src);
  512. if (maxlen > 0) {
  513. size_t len = SDL_min(srclen, maxlen - 1);
  514. SDL_memcpy(dst, src, len);
  515. dst[len] = '\0';
  516. }
  517. return srclen;
  518. #endif /* HAVE_STRLCPY */
  519. }
  520. size_t
  521. SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
  522. {
  523. size_t src_bytes = SDL_strlen(src);
  524. size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
  525. size_t i = 0;
  526. size_t trailing_bytes = 0;
  527. if (bytes) {
  528. unsigned char c = (unsigned char)src[bytes - 1];
  529. if (UTF8_IsLeadByte(c)) {
  530. --bytes;
  531. } else if (UTF8_IsTrailingByte(c)) {
  532. for (i = bytes - 1; i != 0; --i) {
  533. c = (unsigned char)src[i];
  534. trailing_bytes = UTF8_TrailingBytes(c);
  535. if (trailing_bytes) {
  536. if (bytes - i != trailing_bytes + 1) {
  537. bytes = i;
  538. }
  539. break;
  540. }
  541. }
  542. }
  543. SDL_memcpy(dst, src, bytes);
  544. }
  545. dst[bytes] = '\0';
  546. return bytes;
  547. }
  548. size_t
  549. SDL_utf8strlen(const char *str)
  550. {
  551. size_t retval = 0;
  552. const char *p = str;
  553. unsigned char ch;
  554. while ((ch = *(p++)) != 0) {
  555. /* if top two bits are 1 and 0, it's a continuation byte. */
  556. if ((ch & 0xc0) != 0x80) {
  557. retval++;
  558. }
  559. }
  560. return retval;
  561. }
  562. size_t
  563. SDL_utf8strnlen(const char *str, size_t bytes)
  564. {
  565. size_t retval = 0;
  566. const char *p = str;
  567. unsigned char ch;
  568. while ((ch = *(p++)) != 0 && bytes-- > 0) {
  569. /* if top two bits are 1 and 0, it's a continuation byte. */
  570. if ((ch & 0xc0) != 0x80) {
  571. retval++;
  572. }
  573. }
  574. return retval;
  575. }
  576. size_t
  577. SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  578. {
  579. #if defined(HAVE_STRLCAT)
  580. return strlcat(dst, src, maxlen);
  581. #else
  582. size_t dstlen = SDL_strlen(dst);
  583. size_t srclen = SDL_strlen(src);
  584. if (dstlen < maxlen) {
  585. SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
  586. }
  587. return dstlen + srclen;
  588. #endif /* HAVE_STRLCAT */
  589. }
  590. char *SDL_strdup(const char *string)
  591. {
  592. size_t len = SDL_strlen(string) + 1;
  593. char *newstr = (char *)SDL_malloc(len);
  594. if (newstr) {
  595. SDL_memcpy(newstr, string, len);
  596. }
  597. return newstr;
  598. }
  599. char *SDL_strrev(char *string)
  600. {
  601. #if defined(HAVE__STRREV)
  602. return _strrev(string);
  603. #else
  604. size_t len = SDL_strlen(string);
  605. char *a = &string[0];
  606. char *b = &string[len - 1];
  607. len /= 2;
  608. while (len--) {
  609. char c = *a; /* NOLINT(clang-analyzer-core.uninitialized.Assign) */
  610. *a++ = *b;
  611. *b-- = c;
  612. }
  613. return string;
  614. #endif /* HAVE__STRREV */
  615. }
  616. char *SDL_strupr(char *string)
  617. {
  618. #if defined(HAVE__STRUPR)
  619. return _strupr(string);
  620. #else
  621. char *bufp = string;
  622. while (*bufp) {
  623. *bufp = SDL_toupper((unsigned char)*bufp);
  624. ++bufp;
  625. }
  626. return string;
  627. #endif /* HAVE__STRUPR */
  628. }
  629. char *SDL_strlwr(char *string)
  630. {
  631. #if defined(HAVE__STRLWR)
  632. return _strlwr(string);
  633. #else
  634. char *bufp = string;
  635. while (*bufp) {
  636. *bufp = SDL_tolower((unsigned char)*bufp);
  637. ++bufp;
  638. }
  639. return string;
  640. #endif /* HAVE__STRLWR */
  641. }
  642. char *SDL_strchr(const char *string, int c)
  643. {
  644. #ifdef HAVE_STRCHR
  645. return SDL_const_cast(char *, strchr(string, c));
  646. #elif defined(HAVE_INDEX)
  647. return SDL_const_cast(char *, index(string, c));
  648. #else
  649. while (*string) {
  650. if (*string == c) {
  651. return (char *)string;
  652. }
  653. ++string;
  654. }
  655. if (c == '\0') {
  656. return (char *)string;
  657. }
  658. return NULL;
  659. #endif /* HAVE_STRCHR */
  660. }
  661. char *SDL_strrchr(const char *string, int c)
  662. {
  663. #ifdef HAVE_STRRCHR
  664. return SDL_const_cast(char *, strrchr(string, c));
  665. #elif defined(HAVE_RINDEX)
  666. return SDL_const_cast(char *, rindex(string, c));
  667. #else
  668. const char *bufp = string + SDL_strlen(string);
  669. while (bufp >= string) {
  670. if (*bufp == c) {
  671. return (char *)bufp;
  672. }
  673. --bufp;
  674. }
  675. return NULL;
  676. #endif /* HAVE_STRRCHR */
  677. }
  678. char *SDL_strstr(const char *haystack, const char *needle)
  679. {
  680. #if defined(HAVE_STRSTR)
  681. return SDL_const_cast(char *, strstr(haystack, needle));
  682. #else
  683. size_t length = SDL_strlen(needle);
  684. while (*haystack) {
  685. if (SDL_strncmp(haystack, needle, length) == 0) {
  686. return (char *)haystack;
  687. }
  688. ++haystack;
  689. }
  690. return NULL;
  691. #endif /* HAVE_STRSTR */
  692. }
  693. char *SDL_strcasestr(const char *haystack, const char *needle)
  694. {
  695. #if defined(HAVE_STRCASESTR)
  696. return SDL_const_cast(char *, strcasestr(haystack, needle));
  697. #else
  698. size_t length = SDL_strlen(needle);
  699. while (*haystack) {
  700. if (SDL_strncasecmp(haystack, needle, length) == 0) {
  701. return (char *)haystack;
  702. }
  703. ++haystack;
  704. }
  705. return NULL;
  706. #endif /* HAVE_STRCASESTR */
  707. }
  708. #if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
  709. !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
  710. static const char ntoa_table[] = {
  711. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  712. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  713. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  714. 'U', 'V', 'W', 'X', 'Y', 'Z'
  715. };
  716. #endif /* ntoa() conversion table */
  717. char *SDL_itoa(int value, char *string, int radix)
  718. {
  719. #ifdef HAVE_ITOA
  720. return itoa(value, string, radix);
  721. #else
  722. return SDL_ltoa((long)value, string, radix);
  723. #endif /* HAVE_ITOA */
  724. }
  725. char *SDL_uitoa(unsigned int value, char *string, int radix)
  726. {
  727. #ifdef HAVE__UITOA
  728. return _uitoa(value, string, radix);
  729. #else
  730. return SDL_ultoa((unsigned long)value, string, radix);
  731. #endif /* HAVE__UITOA */
  732. }
  733. char *SDL_ltoa(long value, char *string, int radix)
  734. {
  735. #if defined(HAVE__LTOA)
  736. return _ltoa(value, string, radix);
  737. #else
  738. char *bufp = string;
  739. if (value < 0) {
  740. *bufp++ = '-';
  741. SDL_ultoa(-value, bufp, radix);
  742. } else {
  743. SDL_ultoa(value, bufp, radix);
  744. }
  745. return string;
  746. #endif /* HAVE__LTOA */
  747. }
  748. char *SDL_ultoa(unsigned long value, char *string, int radix)
  749. {
  750. #if defined(HAVE__ULTOA)
  751. return _ultoa(value, string, radix);
  752. #else
  753. char *bufp = string;
  754. if (value) {
  755. while (value > 0) {
  756. *bufp++ = ntoa_table[value % radix];
  757. value /= radix;
  758. }
  759. } else {
  760. *bufp++ = '0';
  761. }
  762. *bufp = '\0';
  763. /* The numbers went into the string backwards. :) */
  764. SDL_strrev(string);
  765. return string;
  766. #endif /* HAVE__ULTOA */
  767. }
  768. char *SDL_lltoa(Sint64 value, char *string, int radix)
  769. {
  770. #if defined(HAVE__I64TOA)
  771. return _i64toa(value, string, radix);
  772. #else
  773. char *bufp = string;
  774. if (value < 0) {
  775. *bufp++ = '-';
  776. SDL_ulltoa(-value, bufp, radix);
  777. } else {
  778. SDL_ulltoa(value, bufp, radix);
  779. }
  780. return string;
  781. #endif /* HAVE__I64TOA */
  782. }
  783. char *SDL_ulltoa(Uint64 value, char *string, int radix)
  784. {
  785. #if defined(HAVE__UI64TOA)
  786. return _ui64toa(value, string, radix);
  787. #else
  788. char *bufp = string;
  789. if (value) {
  790. while (value > 0) {
  791. *bufp++ = ntoa_table[value % radix];
  792. value /= radix;
  793. }
  794. } else {
  795. *bufp++ = '0';
  796. }
  797. *bufp = '\0';
  798. /* The numbers went into the string backwards. :) */
  799. SDL_strrev(string);
  800. return string;
  801. #endif /* HAVE__UI64TOA */
  802. }
  803. int SDL_atoi(const char *string)
  804. {
  805. #ifdef HAVE_ATOI
  806. return atoi(string);
  807. #else
  808. return SDL_strtol(string, NULL, 10);
  809. #endif /* HAVE_ATOI */
  810. }
  811. double SDL_atof(const char *string)
  812. {
  813. #ifdef HAVE_ATOF
  814. return atof(string);
  815. #else
  816. return SDL_strtod(string, NULL);
  817. #endif /* HAVE_ATOF */
  818. }
  819. long SDL_strtol(const char *string, char **endp, int base)
  820. {
  821. #if defined(HAVE_STRTOL)
  822. return strtol(string, endp, base);
  823. #else
  824. long value = 0;
  825. size_t len = SDL_ScanLong(string, 0, base, &value);
  826. if (endp) {
  827. *endp = (char *)string + len;
  828. }
  829. return value;
  830. #endif /* HAVE_STRTOL */
  831. }
  832. unsigned long
  833. SDL_strtoul(const char *string, char **endp, int base)
  834. {
  835. #if defined(HAVE_STRTOUL)
  836. return strtoul(string, endp, base);
  837. #else
  838. unsigned long value = 0;
  839. size_t len = SDL_ScanUnsignedLong(string, 0, base, &value);
  840. if (endp) {
  841. *endp = (char *)string + len;
  842. }
  843. return value;
  844. #endif /* HAVE_STRTOUL */
  845. }
  846. Sint64 SDL_strtoll(const char *string, char **endp, int base)
  847. {
  848. #if defined(HAVE_STRTOLL)
  849. return strtoll(string, endp, base);
  850. #else
  851. long long value = 0;
  852. size_t len = SDL_ScanLongLong(string, 0, base, &value);
  853. if (endp) {
  854. *endp = (char *)string + len;
  855. }
  856. return value;
  857. #endif /* HAVE_STRTOLL */
  858. }
  859. Uint64 SDL_strtoull(const char *string, char **endp, int base)
  860. {
  861. #if defined(HAVE_STRTOULL)
  862. return strtoull(string, endp, base);
  863. #else
  864. unsigned long long value = 0;
  865. size_t len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
  866. if (endp) {
  867. *endp = (char *)string + len;
  868. }
  869. return value;
  870. #endif /* HAVE_STRTOULL */
  871. }
  872. double
  873. SDL_strtod(const char *string, char **endp)
  874. {
  875. #if defined(HAVE_STRTOD)
  876. return strtod(string, endp);
  877. #else
  878. size_t len;
  879. double value = 0.0;
  880. len = SDL_ScanFloat(string, &value);
  881. if (endp) {
  882. *endp = (char *)string + len;
  883. }
  884. return value;
  885. #endif /* HAVE_STRTOD */
  886. }
  887. int SDL_strcmp(const char *str1, const char *str2)
  888. {
  889. #if defined(HAVE_STRCMP)
  890. return strcmp(str1, str2);
  891. #else
  892. int result;
  893. while (1) {
  894. result = ((unsigned char)*str1 - (unsigned char)*str2);
  895. if (result != 0 || (*str1 == '\0' /* && *str2 == '\0'*/)) {
  896. break;
  897. }
  898. ++str1;
  899. ++str2;
  900. }
  901. return result;
  902. #endif /* HAVE_STRCMP */
  903. }
  904. int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
  905. {
  906. #if defined(HAVE_STRNCMP)
  907. return strncmp(str1, str2, maxlen);
  908. #else
  909. int result;
  910. while (maxlen) {
  911. result = (int)(unsigned char)*str1 - (unsigned char)*str2;
  912. if (result != 0 || *str1 == '\0' /* && *str2 == '\0'*/) {
  913. break;
  914. }
  915. ++str1;
  916. ++str2;
  917. --maxlen;
  918. }
  919. if (!maxlen) {
  920. result = 0;
  921. }
  922. return result;
  923. #endif /* HAVE_STRNCMP */
  924. }
  925. int SDL_strcasecmp(const char *str1, const char *str2)
  926. {
  927. #ifdef HAVE_STRCASECMP
  928. return strcasecmp(str1, str2);
  929. #elif defined(HAVE__STRICMP)
  930. return _stricmp(str1, str2);
  931. #else
  932. int a, b, result;
  933. while (1) {
  934. a = SDL_toupper((unsigned char)*str1);
  935. b = SDL_toupper((unsigned char)*str2);
  936. result = a - b;
  937. if (result != 0 || a == 0 /*&& b == 0*/) {
  938. break;
  939. }
  940. ++str1;
  941. ++str2;
  942. }
  943. return result;
  944. #endif /* HAVE_STRCASECMP */
  945. }
  946. int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
  947. {
  948. #ifdef HAVE_STRNCASECMP
  949. return strncasecmp(str1, str2, maxlen);
  950. #elif defined(HAVE__STRNICMP)
  951. return _strnicmp(str1, str2, maxlen);
  952. #else
  953. int a, b, result;
  954. while (maxlen) {
  955. a = SDL_tolower((unsigned char)*str1);
  956. b = SDL_tolower((unsigned char)*str2);
  957. result = a - b;
  958. if (result != 0 || a == 0 /*&& b == 0*/) {
  959. break;
  960. }
  961. ++str1;
  962. ++str2;
  963. --maxlen;
  964. }
  965. if (maxlen == 0) {
  966. result = 0;
  967. }
  968. return result;
  969. #endif /* HAVE_STRNCASECMP */
  970. }
  971. int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  972. {
  973. int rc;
  974. va_list ap;
  975. va_start(ap, fmt);
  976. rc = SDL_vsscanf(text, fmt, ap);
  977. va_end(ap);
  978. return rc;
  979. }
  980. #ifdef HAVE_VSSCANF
  981. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  982. {
  983. return vsscanf(text, fmt, ap);
  984. }
  985. #else
  986. static SDL_bool CharacterMatchesSet(char c, const char *set, size_t set_len)
  987. {
  988. SDL_bool invert = SDL_FALSE;
  989. SDL_bool result = SDL_FALSE;
  990. if (*set == '^') {
  991. invert = SDL_TRUE;
  992. ++set;
  993. --set_len;
  994. }
  995. while (set_len > 0 && !result) {
  996. if (set_len >= 3 && set[1] == '-') {
  997. char low_char = SDL_min(set[0], set[2]);
  998. char high_char = SDL_max(set[0], set[2]);
  999. if (c >= low_char && c <= high_char) {
  1000. result = SDL_TRUE;
  1001. }
  1002. set += 3;
  1003. set_len -= 3;
  1004. } else {
  1005. if (c == *set) {
  1006. result = SDL_TRUE;
  1007. }
  1008. ++set;
  1009. --set_len;
  1010. }
  1011. }
  1012. if (invert) {
  1013. result = result ? SDL_FALSE : SDL_TRUE;
  1014. }
  1015. return result;
  1016. }
  1017. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1018. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1019. {
  1020. const char *start = text;
  1021. int retval = 0;
  1022. if (!text || !*text) {
  1023. return -1;
  1024. }
  1025. while (*fmt) {
  1026. if (*fmt == ' ') {
  1027. while (SDL_isspace((unsigned char)*text)) {
  1028. ++text;
  1029. }
  1030. ++fmt;
  1031. continue;
  1032. }
  1033. if (*fmt == '%') {
  1034. SDL_bool done = SDL_FALSE;
  1035. long count = 0;
  1036. int radix = 10;
  1037. enum
  1038. {
  1039. DO_SHORT,
  1040. DO_INT,
  1041. DO_LONG,
  1042. DO_LONGLONG,
  1043. DO_SIZE_T
  1044. } inttype = DO_INT;
  1045. size_t advance;
  1046. SDL_bool suppress = SDL_FALSE;
  1047. ++fmt;
  1048. if (*fmt == '%') {
  1049. if (*text == '%') {
  1050. ++text;
  1051. ++fmt;
  1052. continue;
  1053. }
  1054. break;
  1055. }
  1056. if (*fmt == '*') {
  1057. suppress = SDL_TRUE;
  1058. ++fmt;
  1059. }
  1060. fmt += SDL_ScanLong(fmt, 0, 10, &count);
  1061. if (*fmt == 'c') {
  1062. if (!count) {
  1063. count = 1;
  1064. }
  1065. if (suppress) {
  1066. while (count--) {
  1067. ++text;
  1068. }
  1069. } else {
  1070. char *valuep = va_arg(ap, char *);
  1071. while (count--) {
  1072. *valuep++ = *text++;
  1073. }
  1074. ++retval;
  1075. }
  1076. continue;
  1077. }
  1078. while (SDL_isspace((unsigned char)*text)) {
  1079. ++text;
  1080. }
  1081. /* FIXME: implement more of the format specifiers */
  1082. while (!done) {
  1083. switch (*fmt) {
  1084. case '*':
  1085. suppress = SDL_TRUE;
  1086. break;
  1087. case 'h':
  1088. if (inttype == DO_INT) {
  1089. inttype = DO_SHORT;
  1090. } else if (inttype > DO_SHORT) {
  1091. ++inttype;
  1092. }
  1093. break;
  1094. case 'l':
  1095. if (inttype < DO_LONGLONG) {
  1096. ++inttype;
  1097. }
  1098. break;
  1099. case 'I':
  1100. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1101. fmt += 2;
  1102. inttype = DO_LONGLONG;
  1103. }
  1104. break;
  1105. case 'z':
  1106. inttype = DO_SIZE_T;
  1107. break;
  1108. case 'i':
  1109. {
  1110. int index = 0;
  1111. if (text[index] == '-') {
  1112. ++index;
  1113. }
  1114. if (text[index] == '0') {
  1115. if (SDL_tolower((unsigned char)text[index + 1]) == 'x') {
  1116. radix = 16;
  1117. } else {
  1118. radix = 8;
  1119. }
  1120. }
  1121. }
  1122. SDL_FALLTHROUGH;
  1123. case 'd':
  1124. if (inttype == DO_LONGLONG) {
  1125. Sint64 value = 0;
  1126. advance = SDL_ScanLongLong(text, count, radix, &value);
  1127. text += advance;
  1128. if (advance && !suppress) {
  1129. Sint64 *valuep = va_arg(ap, Sint64 *);
  1130. *valuep = value;
  1131. ++retval;
  1132. }
  1133. } else if (inttype == DO_SIZE_T) {
  1134. Sint64 value = 0;
  1135. advance = SDL_ScanLongLong(text, count, radix, &value);
  1136. text += advance;
  1137. if (advance && !suppress) {
  1138. size_t *valuep = va_arg(ap, size_t *);
  1139. *valuep = (size_t)value;
  1140. ++retval;
  1141. }
  1142. } else {
  1143. long value = 0;
  1144. advance = SDL_ScanLong(text, count, radix, &value);
  1145. text += advance;
  1146. if (advance && !suppress) {
  1147. switch (inttype) {
  1148. case DO_SHORT:
  1149. {
  1150. short *valuep = va_arg(ap, short *);
  1151. *valuep = (short)value;
  1152. } break;
  1153. case DO_INT:
  1154. {
  1155. int *valuep = va_arg(ap, int *);
  1156. *valuep = (int)value;
  1157. } break;
  1158. case DO_LONG:
  1159. {
  1160. long *valuep = va_arg(ap, long *);
  1161. *valuep = value;
  1162. } break;
  1163. case DO_LONGLONG:
  1164. case DO_SIZE_T:
  1165. /* Handled above */
  1166. break;
  1167. }
  1168. ++retval;
  1169. }
  1170. }
  1171. done = SDL_TRUE;
  1172. break;
  1173. case 'o':
  1174. if (radix == 10) {
  1175. radix = 8;
  1176. }
  1177. SDL_FALLTHROUGH;
  1178. case 'x':
  1179. case 'X':
  1180. if (radix == 10) {
  1181. radix = 16;
  1182. }
  1183. SDL_FALLTHROUGH;
  1184. case 'u':
  1185. if (inttype == DO_LONGLONG) {
  1186. Uint64 value = 0;
  1187. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1188. text += advance;
  1189. if (advance && !suppress) {
  1190. Uint64 *valuep = va_arg(ap, Uint64 *);
  1191. *valuep = value;
  1192. ++retval;
  1193. }
  1194. } else if (inttype == DO_SIZE_T) {
  1195. Uint64 value = 0;
  1196. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1197. text += advance;
  1198. if (advance && !suppress) {
  1199. size_t *valuep = va_arg(ap, size_t *);
  1200. *valuep = (size_t)value;
  1201. ++retval;
  1202. }
  1203. } else {
  1204. unsigned long value = 0;
  1205. advance = SDL_ScanUnsignedLong(text, count, radix, &value);
  1206. text += advance;
  1207. if (advance && !suppress) {
  1208. switch (inttype) {
  1209. case DO_SHORT:
  1210. {
  1211. short *valuep = va_arg(ap, short *);
  1212. *valuep = (short)value;
  1213. } break;
  1214. case DO_INT:
  1215. {
  1216. int *valuep = va_arg(ap, int *);
  1217. *valuep = (int)value;
  1218. } break;
  1219. case DO_LONG:
  1220. {
  1221. long *valuep = va_arg(ap, long *);
  1222. *valuep = value;
  1223. } break;
  1224. case DO_LONGLONG:
  1225. case DO_SIZE_T:
  1226. /* Handled above */
  1227. break;
  1228. }
  1229. ++retval;
  1230. }
  1231. }
  1232. done = SDL_TRUE;
  1233. break;
  1234. case 'p':
  1235. {
  1236. uintptr_t value = 0;
  1237. advance = SDL_ScanUintPtrT(text, 16, &value);
  1238. text += advance;
  1239. if (advance && !suppress) {
  1240. void **valuep = va_arg(ap, void **);
  1241. *valuep = (void *)value;
  1242. ++retval;
  1243. }
  1244. }
  1245. done = SDL_TRUE;
  1246. break;
  1247. case 'f':
  1248. {
  1249. double value = 0.0;
  1250. advance = SDL_ScanFloat(text, &value);
  1251. text += advance;
  1252. if (advance && !suppress) {
  1253. float *valuep = va_arg(ap, float *);
  1254. *valuep = (float)value;
  1255. ++retval;
  1256. }
  1257. }
  1258. done = SDL_TRUE;
  1259. break;
  1260. case 's':
  1261. if (suppress) {
  1262. while (!SDL_isspace((unsigned char)*text)) {
  1263. ++text;
  1264. if (count) {
  1265. if (--count == 0) {
  1266. break;
  1267. }
  1268. }
  1269. }
  1270. } else {
  1271. char *valuep = va_arg(ap, char *);
  1272. while (!SDL_isspace((unsigned char)*text)) {
  1273. *valuep++ = *text++;
  1274. if (count) {
  1275. if (--count == 0) {
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. *valuep = '\0';
  1281. ++retval;
  1282. }
  1283. done = SDL_TRUE;
  1284. break;
  1285. case 'n':
  1286. switch (inttype) {
  1287. case DO_SHORT:
  1288. {
  1289. short *valuep = va_arg(ap, short *);
  1290. *valuep = (short)(text - start);
  1291. } break;
  1292. case DO_INT:
  1293. {
  1294. int *valuep = va_arg(ap, int *);
  1295. *valuep = (int)(text - start);
  1296. } break;
  1297. case DO_LONG:
  1298. {
  1299. long *valuep = va_arg(ap, long *);
  1300. *valuep = (long)(text - start);
  1301. } break;
  1302. case DO_LONGLONG:
  1303. {
  1304. long long *valuep = va_arg(ap, long long *);
  1305. *valuep = (long long)(text - start);
  1306. } break;
  1307. case DO_SIZE_T:
  1308. {
  1309. size_t *valuep = va_arg(ap, size_t *);
  1310. *valuep = (size_t)(text - start);
  1311. } break;
  1312. }
  1313. done = SDL_TRUE;
  1314. break;
  1315. case '[':
  1316. {
  1317. const char *set = fmt + 1;
  1318. while (*fmt && *fmt != ']') {
  1319. ++fmt;
  1320. }
  1321. if (*fmt) {
  1322. size_t set_len = (fmt - set);
  1323. if (suppress) {
  1324. while (CharacterMatchesSet(*text, set, set_len)) {
  1325. ++text;
  1326. if (count) {
  1327. if (--count == 0) {
  1328. break;
  1329. }
  1330. }
  1331. }
  1332. } else {
  1333. SDL_bool had_match = SDL_FALSE;
  1334. char *valuep = va_arg(ap, char *);
  1335. while (CharacterMatchesSet(*text, set, set_len)) {
  1336. had_match = SDL_TRUE;
  1337. *valuep++ = *text++;
  1338. if (count) {
  1339. if (--count == 0) {
  1340. break;
  1341. }
  1342. }
  1343. }
  1344. *valuep = '\0';
  1345. if (had_match) {
  1346. ++retval;
  1347. }
  1348. }
  1349. }
  1350. }
  1351. done = SDL_TRUE;
  1352. break;
  1353. default:
  1354. done = SDL_TRUE;
  1355. break;
  1356. }
  1357. ++fmt;
  1358. }
  1359. continue;
  1360. }
  1361. if (*text == *fmt) {
  1362. ++text;
  1363. ++fmt;
  1364. continue;
  1365. }
  1366. /* Text didn't match format specifier */
  1367. break;
  1368. }
  1369. return retval;
  1370. }
  1371. #endif /* HAVE_VSSCANF */
  1372. int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1373. {
  1374. va_list ap;
  1375. int retval;
  1376. va_start(ap, fmt);
  1377. retval = SDL_vsnprintf(text, maxlen, fmt, ap);
  1378. va_end(ap);
  1379. return retval;
  1380. }
  1381. #if defined(HAVE_LIBC) && defined(__WATCOMC__)
  1382. /* _vsnprintf() doesn't ensure nul termination */
  1383. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1384. {
  1385. int retval;
  1386. if (!fmt) {
  1387. fmt = "";
  1388. }
  1389. retval = _vsnprintf(text, maxlen, fmt, ap);
  1390. if (maxlen > 0) {
  1391. text[maxlen - 1] = '\0';
  1392. }
  1393. if (retval < 0) {
  1394. retval = (int)maxlen;
  1395. }
  1396. return retval;
  1397. }
  1398. #elif defined(HAVE_VSNPRINTF)
  1399. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1400. {
  1401. if (!fmt) {
  1402. fmt = "";
  1403. }
  1404. return vsnprintf(text, maxlen, fmt, ap);
  1405. }
  1406. #else
  1407. #define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
  1408. /* FIXME: implement more of the format specifiers */
  1409. typedef enum
  1410. {
  1411. SDL_CASE_NOCHANGE,
  1412. SDL_CASE_LOWER,
  1413. SDL_CASE_UPPER
  1414. } SDL_letter_case;
  1415. typedef struct
  1416. {
  1417. SDL_bool left_justify;
  1418. SDL_bool force_sign;
  1419. SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */
  1420. SDL_bool pad_zeroes;
  1421. SDL_letter_case force_case;
  1422. int width;
  1423. int radix;
  1424. int precision;
  1425. } SDL_FormatInfo;
  1426. static size_t SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
  1427. {
  1428. const char fill = (info && info->pad_zeroes) ? '0' : ' ';
  1429. size_t width = 0;
  1430. size_t filllen = 0;
  1431. size_t length = 0;
  1432. size_t slen, sz;
  1433. if (!string) {
  1434. string = "(null)";
  1435. }
  1436. sz = SDL_strlen(string);
  1437. if (info && info->width > 0 && (size_t)info->width > sz) {
  1438. width = info->width - sz;
  1439. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1440. width += sz - (size_t)info->precision;
  1441. }
  1442. filllen = SDL_min(width, maxlen);
  1443. if (!info->left_justify) {
  1444. SDL_memset(text, fill, filllen);
  1445. text += filllen;
  1446. maxlen -= filllen;
  1447. length += width;
  1448. filllen = 0;
  1449. }
  1450. }
  1451. SDL_strlcpy(text, string, maxlen);
  1452. length += sz;
  1453. if (filllen > 0) {
  1454. SDL_memset(text + sz, fill, filllen);
  1455. length += width;
  1456. }
  1457. if (info) {
  1458. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1459. slen = (size_t)info->precision;
  1460. if (slen < maxlen) {
  1461. text[slen] = '\0';
  1462. }
  1463. length -= (sz - slen);
  1464. }
  1465. if (maxlen > 1) {
  1466. if (info->force_case == SDL_CASE_LOWER) {
  1467. SDL_strlwr(text);
  1468. } else if (info->force_case == SDL_CASE_UPPER) {
  1469. SDL_strupr(text);
  1470. }
  1471. }
  1472. }
  1473. return length;
  1474. }
  1475. static size_t SDL_PrintStringW(char *text, size_t maxlen, SDL_FormatInfo *info, const wchar_t *wide_string)
  1476. {
  1477. size_t length = 0;
  1478. if (wide_string) {
  1479. char *string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)(wide_string), (SDL_wcslen(wide_string) + 1) * sizeof(*wide_string));
  1480. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, string);
  1481. SDL_free(string);
  1482. } else {
  1483. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, NULL);
  1484. }
  1485. return length;
  1486. }
  1487. static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
  1488. { /* left-pad num with zeroes. */
  1489. size_t sz, pad, have_sign;
  1490. if (!info) {
  1491. return;
  1492. }
  1493. have_sign = 0;
  1494. if (*num == '-' || *num == '+') {
  1495. have_sign = 1;
  1496. ++num;
  1497. --maxlen;
  1498. }
  1499. sz = SDL_strlen(num);
  1500. if (info->precision > 0 && sz < (size_t)info->precision) {
  1501. pad = (size_t)info->precision - sz;
  1502. if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
  1503. SDL_memmove(num + pad, num, sz + 1);
  1504. SDL_memset(num, '0', pad);
  1505. }
  1506. }
  1507. info->precision = -1; /* so that SDL_PrintString() doesn't make a mess. */
  1508. if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
  1509. /* handle here: spaces are added before the sign
  1510. but zeroes must be placed _after_ the sign. */
  1511. /* sz hasn't changed: we ignore pad_zeroes if a precision is given. */
  1512. pad = (size_t)info->width - sz - have_sign;
  1513. if (pad + sz + 1 <= maxlen) {
  1514. SDL_memmove(num + pad, num, sz + 1);
  1515. SDL_memset(num, '0', pad);
  1516. }
  1517. info->width = 0; /* so that SDL_PrintString() doesn't make a mess. */
  1518. }
  1519. }
  1520. static size_t SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
  1521. {
  1522. char num[130], *p = num;
  1523. if (info->force_sign && value >= 0L) {
  1524. *p++ = '+';
  1525. }
  1526. SDL_ltoa(value, p, info ? info->radix : 10);
  1527. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1528. return SDL_PrintString(text, maxlen, info, num);
  1529. }
  1530. static size_t SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
  1531. {
  1532. char num[130];
  1533. SDL_ultoa(value, num, info ? info->radix : 10);
  1534. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1535. return SDL_PrintString(text, maxlen, info, num);
  1536. }
  1537. static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
  1538. {
  1539. char num[130], *p = num;
  1540. if (info->force_sign && value >= (Sint64)0) {
  1541. *p++ = '+';
  1542. }
  1543. SDL_lltoa(value, p, info ? info->radix : 10);
  1544. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1545. return SDL_PrintString(text, maxlen, info, num);
  1546. }
  1547. static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
  1548. {
  1549. char num[130];
  1550. SDL_ulltoa(value, num, info ? info->radix : 10);
  1551. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1552. return SDL_PrintString(text, maxlen, info, num);
  1553. }
  1554. static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
  1555. {
  1556. size_t length = 0;
  1557. /* This isn't especially accurate, but hey, it's easy. :) */
  1558. unsigned long value;
  1559. if (arg < 0) {
  1560. if (length < maxlen) {
  1561. text[length] = '-';
  1562. }
  1563. ++length;
  1564. arg = -arg;
  1565. } else if (info->force_sign) {
  1566. if (length < maxlen) {
  1567. text[length] = '+';
  1568. }
  1569. ++length;
  1570. }
  1571. value = (unsigned long)arg;
  1572. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
  1573. arg -= value;
  1574. if (info->precision < 0) {
  1575. info->precision = 6;
  1576. }
  1577. if (info->force_type || info->precision > 0) {
  1578. int mult = 10;
  1579. if (length < maxlen) {
  1580. text[length] = '.';
  1581. }
  1582. ++length;
  1583. while (info->precision-- > 0) {
  1584. value = (unsigned long)(arg * mult);
  1585. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
  1586. arg -= (double)value / mult;
  1587. mult *= 10;
  1588. }
  1589. }
  1590. if (info->width > 0 && (size_t)info->width > length) {
  1591. const char fill = info->pad_zeroes ? '0' : ' ';
  1592. size_t width = info->width - length;
  1593. size_t filllen, movelen;
  1594. filllen = SDL_min(width, maxlen);
  1595. movelen = SDL_min(length, (maxlen - filllen));
  1596. SDL_memmove(&text[filllen], text, movelen);
  1597. SDL_memset(text, fill, filllen);
  1598. length += width;
  1599. }
  1600. return length;
  1601. }
  1602. static size_t SDL_PrintPointer(char *text, size_t maxlen, SDL_FormatInfo *info, const void *value)
  1603. {
  1604. char num[130];
  1605. size_t length;
  1606. if (!value) {
  1607. return SDL_PrintString(text, maxlen, info, NULL);
  1608. }
  1609. SDL_ulltoa((unsigned long long)(uintptr_t)value, num, 16);
  1610. length = SDL_PrintString(text, maxlen, info, "0x");
  1611. return length + SDL_PrintString(TEXT_AND_LEN_ARGS, info, num);
  1612. }
  1613. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1614. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1615. {
  1616. size_t length = 0;
  1617. if (!text) {
  1618. maxlen = 0;
  1619. }
  1620. if (!fmt) {
  1621. fmt = "";
  1622. }
  1623. while (*fmt) {
  1624. if (*fmt == '%') {
  1625. SDL_bool done = SDL_FALSE;
  1626. SDL_bool check_flag;
  1627. SDL_FormatInfo info;
  1628. enum
  1629. {
  1630. DO_INT,
  1631. DO_LONG,
  1632. DO_LONGLONG,
  1633. DO_SIZE_T
  1634. } inttype = DO_INT;
  1635. SDL_zero(info);
  1636. info.radix = 10;
  1637. info.precision = -1;
  1638. check_flag = SDL_TRUE;
  1639. while (check_flag) {
  1640. ++fmt;
  1641. switch (*fmt) {
  1642. case '-':
  1643. info.left_justify = SDL_TRUE;
  1644. break;
  1645. case '+':
  1646. info.force_sign = SDL_TRUE;
  1647. break;
  1648. case '#':
  1649. info.force_type = SDL_TRUE;
  1650. break;
  1651. case '0':
  1652. info.pad_zeroes = SDL_TRUE;
  1653. break;
  1654. default:
  1655. check_flag = SDL_FALSE;
  1656. break;
  1657. }
  1658. }
  1659. if (*fmt >= '0' && *fmt <= '9') {
  1660. info.width = SDL_strtol(fmt, (char **)&fmt, 0);
  1661. } else if (*fmt == '*') {
  1662. ++fmt;
  1663. info.width = va_arg(ap, int);
  1664. }
  1665. if (*fmt == '.') {
  1666. ++fmt;
  1667. if (*fmt >= '0' && *fmt <= '9') {
  1668. info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
  1669. } else if (*fmt == '*') {
  1670. ++fmt;
  1671. info.precision = va_arg(ap, int);
  1672. } else {
  1673. info.precision = 0;
  1674. }
  1675. if (info.precision < 0) {
  1676. info.precision = 0;
  1677. }
  1678. }
  1679. while (!done) {
  1680. switch (*fmt) {
  1681. case '%':
  1682. if (length < maxlen) {
  1683. text[length] = '%';
  1684. }
  1685. ++length;
  1686. done = SDL_TRUE;
  1687. break;
  1688. case 'c':
  1689. /* char is promoted to int when passed through (...) */
  1690. if (length < maxlen) {
  1691. text[length] = (char)va_arg(ap, int);
  1692. }
  1693. ++length;
  1694. done = SDL_TRUE;
  1695. break;
  1696. case 'h':
  1697. /* short is promoted to int when passed through (...) */
  1698. break;
  1699. case 'l':
  1700. if (inttype < DO_LONGLONG) {
  1701. ++inttype;
  1702. }
  1703. break;
  1704. case 'I':
  1705. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1706. fmt += 2;
  1707. inttype = DO_LONGLONG;
  1708. }
  1709. break;
  1710. case 'z':
  1711. inttype = DO_SIZE_T;
  1712. break;
  1713. case 'i':
  1714. case 'd':
  1715. if (info.precision >= 0) {
  1716. info.pad_zeroes = SDL_FALSE;
  1717. }
  1718. switch (inttype) {
  1719. case DO_INT:
  1720. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1721. (long)va_arg(ap, int));
  1722. break;
  1723. case DO_LONG:
  1724. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1725. va_arg(ap, long));
  1726. break;
  1727. case DO_LONGLONG:
  1728. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1729. va_arg(ap, Sint64));
  1730. break;
  1731. case DO_SIZE_T:
  1732. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1733. va_arg(ap, size_t));
  1734. break;
  1735. }
  1736. done = SDL_TRUE;
  1737. break;
  1738. case 'p':
  1739. info.force_case = SDL_CASE_LOWER;
  1740. length += SDL_PrintPointer(TEXT_AND_LEN_ARGS, &info, va_arg(ap, void *));
  1741. done = SDL_TRUE;
  1742. break;
  1743. case 'x':
  1744. info.force_case = SDL_CASE_LOWER;
  1745. SDL_FALLTHROUGH;
  1746. case 'X':
  1747. if (info.force_case == SDL_CASE_NOCHANGE) {
  1748. info.force_case = SDL_CASE_UPPER;
  1749. }
  1750. if (info.radix == 10) {
  1751. info.radix = 16;
  1752. }
  1753. SDL_FALLTHROUGH;
  1754. case 'o':
  1755. if (info.radix == 10) {
  1756. info.radix = 8;
  1757. }
  1758. SDL_FALLTHROUGH;
  1759. case 'u':
  1760. info.force_sign = SDL_FALSE;
  1761. if (info.precision >= 0) {
  1762. info.pad_zeroes = SDL_FALSE;
  1763. }
  1764. switch (inttype) {
  1765. case DO_INT:
  1766. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1767. (unsigned long)
  1768. va_arg(ap, unsigned int));
  1769. break;
  1770. case DO_LONG:
  1771. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1772. va_arg(ap, unsigned long));
  1773. break;
  1774. case DO_LONGLONG:
  1775. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1776. va_arg(ap, Uint64));
  1777. break;
  1778. case DO_SIZE_T:
  1779. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1780. va_arg(ap, size_t));
  1781. break;
  1782. }
  1783. done = SDL_TRUE;
  1784. break;
  1785. case 'f':
  1786. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double));
  1787. done = SDL_TRUE;
  1788. break;
  1789. case 'S':
  1790. info.pad_zeroes = SDL_FALSE;
  1791. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  1792. done = SDL_TRUE;
  1793. break;
  1794. case 's':
  1795. info.pad_zeroes = SDL_FALSE;
  1796. if (inttype > DO_INT) {
  1797. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  1798. } else {
  1799. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
  1800. }
  1801. done = SDL_TRUE;
  1802. break;
  1803. default:
  1804. done = SDL_TRUE;
  1805. break;
  1806. }
  1807. ++fmt;
  1808. }
  1809. } else {
  1810. if (length < maxlen) {
  1811. text[length] = *fmt;
  1812. }
  1813. ++fmt;
  1814. ++length;
  1815. }
  1816. }
  1817. if (length < maxlen) {
  1818. text[length] = '\0';
  1819. } else if (maxlen > 0) {
  1820. text[maxlen - 1] = '\0';
  1821. }
  1822. return (int)length;
  1823. }
  1824. #undef TEXT_AND_LEN_ARGS
  1825. #endif /* HAVE_VSNPRINTF */
  1826. int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1827. {
  1828. va_list ap;
  1829. int retval;
  1830. va_start(ap, fmt);
  1831. retval = SDL_vasprintf(strp, fmt, ap);
  1832. va_end(ap);
  1833. return retval;
  1834. }
  1835. int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
  1836. {
  1837. int retval;
  1838. int size = 100; /* Guess we need no more than 100 bytes */
  1839. char *p, *np;
  1840. va_list aq;
  1841. *strp = NULL;
  1842. p = (char *)SDL_malloc(size);
  1843. if (!p) {
  1844. return -1;
  1845. }
  1846. while (1) {
  1847. /* Try to print in the allocated space */
  1848. va_copy(aq, ap);
  1849. retval = SDL_vsnprintf(p, size, fmt, aq);
  1850. va_end(aq);
  1851. /* Check error code */
  1852. if (retval < 0) {
  1853. SDL_free(p);
  1854. return retval;
  1855. }
  1856. /* If that worked, return the string */
  1857. if (retval < size) {
  1858. *strp = p;
  1859. return retval;
  1860. }
  1861. /* Else try again with more space */
  1862. size = retval + 1; /* Precisely what is needed */
  1863. np = (char *)SDL_realloc(p, size);
  1864. if (!np) {
  1865. SDL_free(p);
  1866. return -1;
  1867. } else {
  1868. p = np;
  1869. }
  1870. }
  1871. }