1
0

SDL_string.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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 unsigned 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. static size_t
  47. SDL_ScanLong(const char *text, int count, int radix, long *valuep)
  48. {
  49. const char *textstart = text;
  50. long value = 0;
  51. SDL_bool negative = SDL_FALSE;
  52. if (*text == '-') {
  53. negative = SDL_TRUE;
  54. ++text;
  55. }
  56. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  57. text += 2;
  58. }
  59. for (;;) {
  60. int v;
  61. if (SDL_isdigit((unsigned char)*text)) {
  62. v = *text - '0';
  63. } else if (radix == 16 && SDL_isupperhex(*text)) {
  64. v = 10 + (*text - 'A');
  65. } else if (radix == 16 && SDL_islowerhex(*text)) {
  66. v = 10 + (*text - 'a');
  67. } else {
  68. break;
  69. }
  70. value *= radix;
  71. value += v;
  72. ++text;
  73. if (count > 0 && (text - textstart) == count) {
  74. break;
  75. }
  76. }
  77. if (valuep && text > textstart) {
  78. if (negative && value) {
  79. *valuep = -value;
  80. } else {
  81. *valuep = value;
  82. }
  83. }
  84. return text - textstart;
  85. }
  86. #endif
  87. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  88. static size_t
  89. SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
  90. {
  91. const char *textstart = text;
  92. unsigned long value = 0;
  93. if (*text == '-') {
  94. return SDL_ScanLong(text, count, radix, (long *)valuep);
  95. }
  96. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  97. text += 2;
  98. }
  99. for (;;) {
  100. int v;
  101. if (SDL_isdigit((unsigned char)*text)) {
  102. v = *text - '0';
  103. } else if (radix == 16 && SDL_isupperhex(*text)) {
  104. v = 10 + (*text - 'A');
  105. } else if (radix == 16 && SDL_islowerhex(*text)) {
  106. v = 10 + (*text - 'a');
  107. } else {
  108. break;
  109. }
  110. value *= radix;
  111. value += v;
  112. ++text;
  113. if (count > 0 && (text - textstart) == count) {
  114. break;
  115. }
  116. }
  117. if (valuep && text > textstart) {
  118. *valuep = value;
  119. }
  120. return text - textstart;
  121. }
  122. #endif
  123. #ifndef HAVE_VSSCANF
  124. static size_t
  125. SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
  126. {
  127. const char *textstart = text;
  128. uintptr_t value = 0;
  129. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  130. text += 2;
  131. }
  132. for (;;) {
  133. int v;
  134. if (SDL_isdigit((unsigned char)*text)) {
  135. v = *text - '0';
  136. } else if (radix == 16 && SDL_isupperhex(*text)) {
  137. v = 10 + (*text - 'A');
  138. } else if (radix == 16 && SDL_islowerhex(*text)) {
  139. v = 10 + (*text - 'a');
  140. } else {
  141. break;
  142. }
  143. value *= radix;
  144. value += v;
  145. ++text;
  146. }
  147. if (valuep && text > textstart) {
  148. *valuep = value;
  149. }
  150. return text - textstart;
  151. }
  152. #endif
  153. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  154. static size_t
  155. SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
  156. {
  157. const char *textstart = text;
  158. Sint64 value = 0;
  159. SDL_bool negative = SDL_FALSE;
  160. if (*text == '-') {
  161. negative = SDL_TRUE;
  162. ++text;
  163. }
  164. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  165. text += 2;
  166. }
  167. for (;;) {
  168. int v;
  169. if (SDL_isdigit((unsigned char)*text)) {
  170. v = *text - '0';
  171. } else if (radix == 16 && SDL_isupperhex(*text)) {
  172. v = 10 + (*text - 'A');
  173. } else if (radix == 16 && SDL_islowerhex(*text)) {
  174. v = 10 + (*text - 'a');
  175. } else {
  176. break;
  177. }
  178. value *= radix;
  179. value += v;
  180. ++text;
  181. if (count > 0 && (text - textstart) == count) {
  182. break;
  183. }
  184. }
  185. if (valuep && text > textstart) {
  186. if (negative && value) {
  187. *valuep = -value;
  188. } else {
  189. *valuep = value;
  190. }
  191. }
  192. return text - textstart;
  193. }
  194. #endif
  195. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
  196. static size_t
  197. SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
  198. {
  199. const char *textstart = text;
  200. Uint64 value = 0;
  201. if (*text == '-') {
  202. return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
  203. }
  204. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  205. text += 2;
  206. }
  207. for (;;) {
  208. int v;
  209. if (SDL_isdigit((unsigned char)*text)) {
  210. v = *text - '0';
  211. } else if (radix == 16 && SDL_isupperhex(*text)) {
  212. v = 10 + (*text - 'A');
  213. } else if (radix == 16 && SDL_islowerhex(*text)) {
  214. v = 10 + (*text - 'a');
  215. } else {
  216. break;
  217. }
  218. value *= radix;
  219. value += v;
  220. ++text;
  221. if (count > 0 && (text - textstart) == count) {
  222. break;
  223. }
  224. }
  225. if (valuep && text > textstart) {
  226. *valuep = value;
  227. }
  228. return text - textstart;
  229. }
  230. #endif
  231. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
  232. static size_t
  233. SDL_ScanFloat(const char *text, double *valuep)
  234. {
  235. const char *textstart = text;
  236. unsigned long lvalue = 0;
  237. double value = 0.0;
  238. SDL_bool negative = SDL_FALSE;
  239. if (*text == '-') {
  240. negative = SDL_TRUE;
  241. ++text;
  242. }
  243. text += SDL_ScanUnsignedLong(text, 0, 10, &lvalue);
  244. value += lvalue;
  245. if (*text == '.') {
  246. int mult = 10;
  247. ++text;
  248. while (SDL_isdigit((unsigned char)*text)) {
  249. lvalue = *text - '0';
  250. value += (double)lvalue / mult;
  251. mult *= 10;
  252. ++text;
  253. }
  254. }
  255. if (valuep && text > textstart) {
  256. if (negative && value) {
  257. *valuep = -value;
  258. } else {
  259. *valuep = value;
  260. }
  261. }
  262. return text - textstart;
  263. }
  264. #endif
  265. void *
  266. SDL_memmove(SDL_OUT_BYTECAP(len) void *dst, SDL_IN_BYTECAP(len) const void *src, size_t len)
  267. {
  268. #if defined(HAVE_MEMMOVE)
  269. return memmove(dst, src, len);
  270. #else
  271. char *srcp = (char *)src;
  272. char *dstp = (char *)dst;
  273. if (src < dst) {
  274. srcp += len - 1;
  275. dstp += len - 1;
  276. while (len--) {
  277. *dstp-- = *srcp--;
  278. }
  279. } else {
  280. while (len--) {
  281. *dstp++ = *srcp++;
  282. }
  283. }
  284. return dst;
  285. #endif /* HAVE_MEMMOVE */
  286. }
  287. int SDL_memcmp(const void *s1, const void *s2, size_t len)
  288. {
  289. #if defined(__vita__)
  290. /*
  291. Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
  292. But, both linux and bsd allow that, with an exception:
  293. zero length strings are always identical, so NULLs are never dereferenced.
  294. sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
  295. */
  296. if (len == 0) {
  297. return 0;
  298. }
  299. return sceClibMemcmp(s1, s2, len);
  300. #elif defined(HAVE_MEMCMP)
  301. return memcmp(s1, s2, len);
  302. #else
  303. char *s1p = (char *)s1;
  304. char *s2p = (char *)s2;
  305. while (len--) {
  306. if (*s1p != *s2p) {
  307. return *s1p - *s2p;
  308. }
  309. ++s1p;
  310. ++s2p;
  311. }
  312. return 0;
  313. #endif /* HAVE_MEMCMP */
  314. }
  315. size_t
  316. SDL_strlen(const char *string)
  317. {
  318. #if defined(HAVE_STRLEN)
  319. return strlen(string);
  320. #else
  321. size_t len = 0;
  322. while (*string++) {
  323. ++len;
  324. }
  325. return len;
  326. #endif /* HAVE_STRLEN */
  327. }
  328. size_t
  329. SDL_wcslen(const wchar_t *string)
  330. {
  331. #if defined(HAVE_WCSLEN)
  332. return wcslen(string);
  333. #else
  334. size_t len = 0;
  335. while (*string++) {
  336. ++len;
  337. }
  338. return len;
  339. #endif /* HAVE_WCSLEN */
  340. }
  341. size_t
  342. SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  343. {
  344. #if defined(HAVE_WCSLCPY)
  345. return wcslcpy(dst, src, maxlen);
  346. #else
  347. size_t srclen = SDL_wcslen(src);
  348. if (maxlen > 0) {
  349. size_t len = SDL_min(srclen, maxlen - 1);
  350. SDL_memcpy(dst, src, len * sizeof(wchar_t));
  351. dst[len] = '\0';
  352. }
  353. return srclen;
  354. #endif /* HAVE_WCSLCPY */
  355. }
  356. size_t
  357. SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  358. {
  359. #if defined(HAVE_WCSLCAT)
  360. return wcslcat(dst, src, maxlen);
  361. #else
  362. size_t dstlen = SDL_wcslen(dst);
  363. size_t srclen = SDL_wcslen(src);
  364. if (dstlen < maxlen) {
  365. SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
  366. }
  367. return dstlen + srclen;
  368. #endif /* HAVE_WCSLCAT */
  369. }
  370. wchar_t *
  371. SDL_wcsdup(const wchar_t *string)
  372. {
  373. size_t len = ((SDL_wcslen(string) + 1) * sizeof(wchar_t));
  374. wchar_t *newstr = (wchar_t *)SDL_malloc(len);
  375. if (newstr) {
  376. SDL_memcpy(newstr, string, len);
  377. }
  378. return newstr;
  379. }
  380. wchar_t *
  381. 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. unsigned char 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 *
  591. SDL_strdup(const char *string)
  592. {
  593. size_t len = SDL_strlen(string) + 1;
  594. char *newstr = (char *)SDL_malloc(len);
  595. if (newstr) {
  596. SDL_memcpy(newstr, string, len);
  597. }
  598. return newstr;
  599. }
  600. char *
  601. SDL_strrev(char *string)
  602. {
  603. #if defined(HAVE__STRREV)
  604. return _strrev(string);
  605. #else
  606. size_t len = SDL_strlen(string);
  607. char *a = &string[0];
  608. char *b = &string[len - 1];
  609. len /= 2;
  610. while (len--) {
  611. char c = *a; /* NOLINT(clang-analyzer-core.uninitialized.Assign) */
  612. *a++ = *b;
  613. *b-- = c;
  614. }
  615. return string;
  616. #endif /* HAVE__STRREV */
  617. }
  618. char *
  619. SDL_strupr(char *string)
  620. {
  621. #if defined(HAVE__STRUPR)
  622. return _strupr(string);
  623. #else
  624. char *bufp = string;
  625. while (*bufp) {
  626. *bufp = SDL_toupper((unsigned char)*bufp);
  627. ++bufp;
  628. }
  629. return string;
  630. #endif /* HAVE__STRUPR */
  631. }
  632. char *
  633. SDL_strlwr(char *string)
  634. {
  635. #if defined(HAVE__STRLWR)
  636. return _strlwr(string);
  637. #else
  638. char *bufp = string;
  639. while (*bufp) {
  640. *bufp = SDL_tolower((unsigned char)*bufp);
  641. ++bufp;
  642. }
  643. return string;
  644. #endif /* HAVE__STRLWR */
  645. }
  646. char *
  647. SDL_strchr(const char *string, int c)
  648. {
  649. #ifdef HAVE_STRCHR
  650. return SDL_const_cast(char *, strchr(string, c));
  651. #elif defined(HAVE_INDEX)
  652. return SDL_const_cast(char *, index(string, c));
  653. #else
  654. while (*string) {
  655. if (*string == c) {
  656. return (char *)string;
  657. }
  658. ++string;
  659. }
  660. if (c == '\0') {
  661. return (char *)string;
  662. }
  663. return NULL;
  664. #endif /* HAVE_STRCHR */
  665. }
  666. char *
  667. SDL_strrchr(const char *string, int c)
  668. {
  669. #ifdef HAVE_STRRCHR
  670. return SDL_const_cast(char *, strrchr(string, c));
  671. #elif defined(HAVE_RINDEX)
  672. return SDL_const_cast(char *, rindex(string, c));
  673. #else
  674. const char *bufp = string + SDL_strlen(string);
  675. while (bufp >= string) {
  676. if (*bufp == c) {
  677. return (char *)bufp;
  678. }
  679. --bufp;
  680. }
  681. return NULL;
  682. #endif /* HAVE_STRRCHR */
  683. }
  684. char *
  685. SDL_strstr(const char *haystack, const char *needle)
  686. {
  687. #if defined(HAVE_STRSTR)
  688. return SDL_const_cast(char *, strstr(haystack, needle));
  689. #else
  690. size_t length = SDL_strlen(needle);
  691. while (*haystack) {
  692. if (SDL_strncmp(haystack, needle, length) == 0) {
  693. return (char *)haystack;
  694. }
  695. ++haystack;
  696. }
  697. return NULL;
  698. #endif /* HAVE_STRSTR */
  699. }
  700. char *
  701. SDL_strcasestr(const char *haystack, const char *needle)
  702. {
  703. #if defined(HAVE_STRCASESTR)
  704. return SDL_const_cast(char *, strcasestr(haystack, needle));
  705. #else
  706. size_t length = SDL_strlen(needle);
  707. while (*haystack) {
  708. if (SDL_strncasecmp(haystack, needle, length) == 0) {
  709. return (char *)haystack;
  710. }
  711. ++haystack;
  712. }
  713. return NULL;
  714. #endif /* HAVE_STRCASESTR */
  715. }
  716. #if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
  717. !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
  718. static const char ntoa_table[] = {
  719. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  720. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  721. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  722. 'U', 'V', 'W', 'X', 'Y', 'Z'
  723. };
  724. #endif /* ntoa() conversion table */
  725. char *
  726. SDL_itoa(int value, char *string, int radix)
  727. {
  728. #ifdef HAVE_ITOA
  729. return itoa(value, string, radix);
  730. #else
  731. return SDL_ltoa((long)value, string, radix);
  732. #endif /* HAVE_ITOA */
  733. }
  734. char *
  735. SDL_uitoa(unsigned int value, char *string, int radix)
  736. {
  737. #ifdef HAVE__UITOA
  738. return _uitoa(value, string, radix);
  739. #else
  740. return SDL_ultoa((unsigned long)value, string, radix);
  741. #endif /* HAVE__UITOA */
  742. }
  743. char *
  744. SDL_ltoa(long value, char *string, int radix)
  745. {
  746. #if defined(HAVE__LTOA)
  747. return _ltoa(value, string, radix);
  748. #else
  749. char *bufp = string;
  750. if (value < 0) {
  751. *bufp++ = '-';
  752. SDL_ultoa(-value, bufp, radix);
  753. } else {
  754. SDL_ultoa(value, bufp, radix);
  755. }
  756. return string;
  757. #endif /* HAVE__LTOA */
  758. }
  759. char *
  760. SDL_ultoa(unsigned long value, char *string, int radix)
  761. {
  762. #if defined(HAVE__ULTOA)
  763. return _ultoa(value, string, radix);
  764. #else
  765. char *bufp = string;
  766. if (value) {
  767. while (value > 0) {
  768. *bufp++ = ntoa_table[value % radix];
  769. value /= radix;
  770. }
  771. } else {
  772. *bufp++ = '0';
  773. }
  774. *bufp = '\0';
  775. /* The numbers went into the string backwards. :) */
  776. SDL_strrev(string);
  777. return string;
  778. #endif /* HAVE__ULTOA */
  779. }
  780. char *
  781. SDL_lltoa(Sint64 value, char *string, int radix)
  782. {
  783. #if defined(HAVE__I64TOA)
  784. return _i64toa(value, string, radix);
  785. #else
  786. char *bufp = string;
  787. if (value < 0) {
  788. *bufp++ = '-';
  789. SDL_ulltoa(-value, bufp, radix);
  790. } else {
  791. SDL_ulltoa(value, bufp, radix);
  792. }
  793. return string;
  794. #endif /* HAVE__I64TOA */
  795. }
  796. char *
  797. SDL_ulltoa(Uint64 value, char *string, int radix)
  798. {
  799. #if defined(HAVE__UI64TOA)
  800. return _ui64toa(value, string, radix);
  801. #else
  802. char *bufp = string;
  803. if (value) {
  804. while (value > 0) {
  805. *bufp++ = ntoa_table[value % radix];
  806. value /= radix;
  807. }
  808. } else {
  809. *bufp++ = '0';
  810. }
  811. *bufp = '\0';
  812. /* The numbers went into the string backwards. :) */
  813. SDL_strrev(string);
  814. return string;
  815. #endif /* HAVE__UI64TOA */
  816. }
  817. int SDL_atoi(const char *string)
  818. {
  819. #ifdef HAVE_ATOI
  820. return atoi(string);
  821. #else
  822. return SDL_strtol(string, NULL, 10);
  823. #endif /* HAVE_ATOI */
  824. }
  825. double SDL_atof(const char *string)
  826. {
  827. #ifdef HAVE_ATOF
  828. return atof(string);
  829. #else
  830. return SDL_strtod(string, NULL);
  831. #endif /* HAVE_ATOF */
  832. }
  833. long SDL_strtol(const char *string, char **endp, int base)
  834. {
  835. #if defined(HAVE_STRTOL)
  836. return strtol(string, endp, base);
  837. #else
  838. size_t len;
  839. long value = 0;
  840. if (!base) {
  841. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  842. base = 16;
  843. } else {
  844. base = 10;
  845. }
  846. }
  847. len = SDL_ScanLong(string, 0, base, &value);
  848. if (endp) {
  849. *endp = (char *)string + len;
  850. }
  851. return value;
  852. #endif /* HAVE_STRTOL */
  853. }
  854. unsigned long
  855. SDL_strtoul(const char *string, char **endp, int base)
  856. {
  857. #if defined(HAVE_STRTOUL)
  858. return strtoul(string, endp, base);
  859. #else
  860. size_t len;
  861. unsigned long value = 0;
  862. if (!base) {
  863. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  864. base = 16;
  865. } else {
  866. base = 10;
  867. }
  868. }
  869. len = SDL_ScanUnsignedLong(string, 0, base, &value);
  870. if (endp) {
  871. *endp = (char *)string + len;
  872. }
  873. return value;
  874. #endif /* HAVE_STRTOUL */
  875. }
  876. Sint64
  877. SDL_strtoll(const char *string, char **endp, int base)
  878. {
  879. #if defined(HAVE_STRTOLL)
  880. return strtoll(string, endp, base);
  881. #else
  882. size_t len;
  883. Sint64 value = 0;
  884. if (!base) {
  885. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  886. base = 16;
  887. } else {
  888. base = 10;
  889. }
  890. }
  891. len = SDL_ScanLongLong(string, 0, base, &value);
  892. if (endp) {
  893. *endp = (char *)string + len;
  894. }
  895. return value;
  896. #endif /* HAVE_STRTOLL */
  897. }
  898. Uint64
  899. SDL_strtoull(const char *string, char **endp, int base)
  900. {
  901. #if defined(HAVE_STRTOULL)
  902. return strtoull(string, endp, base);
  903. #else
  904. size_t len;
  905. Uint64 value = 0;
  906. if (!base) {
  907. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  908. base = 16;
  909. } else {
  910. base = 10;
  911. }
  912. }
  913. len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
  914. if (endp) {
  915. *endp = (char *)string + len;
  916. }
  917. return value;
  918. #endif /* HAVE_STRTOULL */
  919. }
  920. double
  921. SDL_strtod(const char *string, char **endp)
  922. {
  923. #if defined(HAVE_STRTOD)
  924. return strtod(string, endp);
  925. #else
  926. size_t len;
  927. double value = 0.0;
  928. len = SDL_ScanFloat(string, &value);
  929. if (endp) {
  930. *endp = (char *)string + len;
  931. }
  932. return value;
  933. #endif /* HAVE_STRTOD */
  934. }
  935. int SDL_strcmp(const char *str1, const char *str2)
  936. {
  937. #if defined(HAVE_STRCMP)
  938. return strcmp(str1, str2);
  939. #else
  940. int result;
  941. while (1) {
  942. result = ((unsigned char)*str1 - (unsigned char)*str2);
  943. if (result != 0 || (*str1 == '\0' /* && *str2 == '\0'*/)) {
  944. break;
  945. }
  946. ++str1;
  947. ++str2;
  948. }
  949. return result;
  950. #endif /* HAVE_STRCMP */
  951. }
  952. int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
  953. {
  954. #if defined(HAVE_STRNCMP)
  955. return strncmp(str1, str2, maxlen);
  956. #else
  957. int result;
  958. while (maxlen) {
  959. result = (int)(unsigned char)*str1 - (unsigned char)*str2;
  960. if (result != 0 || *str1 == '\0' /* && *str2 == '\0'*/) {
  961. break;
  962. }
  963. ++str1;
  964. ++str2;
  965. --maxlen;
  966. }
  967. if (!maxlen) {
  968. result = 0;
  969. }
  970. return result;
  971. #endif /* HAVE_STRNCMP */
  972. }
  973. int SDL_strcasecmp(const char *str1, const char *str2)
  974. {
  975. #ifdef HAVE_STRCASECMP
  976. return strcasecmp(str1, str2);
  977. #elif defined(HAVE__STRICMP)
  978. return _stricmp(str1, str2);
  979. #else
  980. int a, b, result;
  981. while (1) {
  982. a = SDL_toupper((unsigned char)*str1);
  983. b = SDL_toupper((unsigned char)*str2);
  984. result = a - b;
  985. if (result != 0 || a == 0 /*&& b == 0*/) {
  986. break;
  987. }
  988. ++str1;
  989. ++str2;
  990. }
  991. return result;
  992. #endif /* HAVE_STRCASECMP */
  993. }
  994. int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
  995. {
  996. #ifdef HAVE_STRNCASECMP
  997. return strncasecmp(str1, str2, maxlen);
  998. #elif defined(HAVE__STRNICMP)
  999. return _strnicmp(str1, str2, maxlen);
  1000. #else
  1001. int a, b, result;
  1002. while (maxlen) {
  1003. a = SDL_tolower((unsigned char)*str1);
  1004. b = SDL_tolower((unsigned char)*str2);
  1005. result = a - b;
  1006. if (result != 0 || a == 0 /*&& b == 0*/) {
  1007. break;
  1008. }
  1009. ++str1;
  1010. ++str2;
  1011. --maxlen;
  1012. }
  1013. if (maxlen == 0) {
  1014. result = 0;
  1015. }
  1016. return result;
  1017. #endif /* HAVE_STRNCASECMP */
  1018. }
  1019. int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  1020. {
  1021. int rc;
  1022. va_list ap;
  1023. va_start(ap, fmt);
  1024. rc = SDL_vsscanf(text, fmt, ap);
  1025. va_end(ap);
  1026. return rc;
  1027. }
  1028. #ifdef HAVE_VSSCANF
  1029. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1030. {
  1031. return vsscanf(text, fmt, ap);
  1032. }
  1033. #else
  1034. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1035. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1036. {
  1037. int retval = 0;
  1038. if (text == NULL || !*text) {
  1039. return -1;
  1040. }
  1041. while (*fmt) {
  1042. if (*fmt == ' ') {
  1043. while (SDL_isspace((unsigned char)*text)) {
  1044. ++text;
  1045. }
  1046. ++fmt;
  1047. continue;
  1048. }
  1049. if (*fmt == '%') {
  1050. SDL_bool done = SDL_FALSE;
  1051. long count = 0;
  1052. int radix = 10;
  1053. enum
  1054. {
  1055. DO_SHORT,
  1056. DO_INT,
  1057. DO_LONG,
  1058. DO_LONGLONG,
  1059. DO_SIZE_T
  1060. } inttype = DO_INT;
  1061. size_t advance;
  1062. SDL_bool suppress = SDL_FALSE;
  1063. ++fmt;
  1064. if (*fmt == '%') {
  1065. if (*text == '%') {
  1066. ++text;
  1067. ++fmt;
  1068. continue;
  1069. }
  1070. break;
  1071. }
  1072. if (*fmt == '*') {
  1073. suppress = SDL_TRUE;
  1074. ++fmt;
  1075. }
  1076. fmt += SDL_ScanLong(fmt, 0, 10, &count);
  1077. if (*fmt == 'c') {
  1078. if (!count) {
  1079. count = 1;
  1080. }
  1081. if (suppress) {
  1082. while (count--) {
  1083. ++text;
  1084. }
  1085. } else {
  1086. char *valuep = va_arg(ap, char *);
  1087. while (count--) {
  1088. *valuep++ = *text++;
  1089. }
  1090. ++retval;
  1091. }
  1092. continue;
  1093. }
  1094. while (SDL_isspace((unsigned char)*text)) {
  1095. ++text;
  1096. }
  1097. /* FIXME: implement more of the format specifiers */
  1098. while (!done) {
  1099. switch (*fmt) {
  1100. case '*':
  1101. suppress = SDL_TRUE;
  1102. break;
  1103. case 'h':
  1104. if (inttype > DO_SHORT) {
  1105. ++inttype;
  1106. }
  1107. break;
  1108. case 'l':
  1109. if (inttype < DO_LONGLONG) {
  1110. ++inttype;
  1111. }
  1112. break;
  1113. case 'I':
  1114. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1115. fmt += 2;
  1116. inttype = DO_LONGLONG;
  1117. }
  1118. break;
  1119. case 'z':
  1120. inttype = DO_SIZE_T;
  1121. break;
  1122. case 'i':
  1123. {
  1124. int index = 0;
  1125. if (text[index] == '-') {
  1126. ++index;
  1127. }
  1128. if (text[index] == '0') {
  1129. if (SDL_tolower((unsigned char)text[index + 1]) == 'x') {
  1130. radix = 16;
  1131. } else {
  1132. radix = 8;
  1133. }
  1134. }
  1135. }
  1136. SDL_FALLTHROUGH;
  1137. case 'd':
  1138. if (inttype == DO_LONGLONG) {
  1139. Sint64 value = 0;
  1140. advance = SDL_ScanLongLong(text, count, radix, &value);
  1141. text += advance;
  1142. if (advance && !suppress) {
  1143. Sint64 *valuep = va_arg(ap, Sint64 *);
  1144. *valuep = value;
  1145. ++retval;
  1146. }
  1147. } else if (inttype == DO_SIZE_T) {
  1148. Sint64 value = 0;
  1149. advance = SDL_ScanLongLong(text, count, radix, &value);
  1150. text += advance;
  1151. if (advance && !suppress) {
  1152. size_t *valuep = va_arg(ap, size_t *);
  1153. *valuep = (size_t)value;
  1154. ++retval;
  1155. }
  1156. } else {
  1157. long value = 0;
  1158. advance = SDL_ScanLong(text, count, radix, &value);
  1159. text += advance;
  1160. if (advance && !suppress) {
  1161. switch (inttype) {
  1162. case DO_SHORT:
  1163. {
  1164. short *valuep = va_arg(ap, short *);
  1165. *valuep = (short)value;
  1166. } break;
  1167. case DO_INT:
  1168. {
  1169. int *valuep = va_arg(ap, int *);
  1170. *valuep = (int)value;
  1171. } break;
  1172. case DO_LONG:
  1173. {
  1174. long *valuep = va_arg(ap, long *);
  1175. *valuep = value;
  1176. } break;
  1177. case DO_LONGLONG:
  1178. case DO_SIZE_T:
  1179. /* Handled above */
  1180. break;
  1181. }
  1182. ++retval;
  1183. }
  1184. }
  1185. done = SDL_TRUE;
  1186. break;
  1187. case 'o':
  1188. if (radix == 10) {
  1189. radix = 8;
  1190. }
  1191. SDL_FALLTHROUGH;
  1192. case 'x':
  1193. case 'X':
  1194. if (radix == 10) {
  1195. radix = 16;
  1196. }
  1197. SDL_FALLTHROUGH;
  1198. case 'u':
  1199. if (inttype == DO_LONGLONG) {
  1200. Uint64 value = 0;
  1201. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1202. text += advance;
  1203. if (advance && !suppress) {
  1204. Uint64 *valuep = va_arg(ap, Uint64 *);
  1205. *valuep = value;
  1206. ++retval;
  1207. }
  1208. } else if (inttype == DO_SIZE_T) {
  1209. Uint64 value = 0;
  1210. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1211. text += advance;
  1212. if (advance && !suppress) {
  1213. size_t *valuep = va_arg(ap, size_t *);
  1214. *valuep = (size_t)value;
  1215. ++retval;
  1216. }
  1217. } else {
  1218. unsigned long value = 0;
  1219. advance = SDL_ScanUnsignedLong(text, count, radix, &value);
  1220. text += advance;
  1221. if (advance && !suppress) {
  1222. switch (inttype) {
  1223. case DO_SHORT:
  1224. {
  1225. short *valuep = va_arg(ap, short *);
  1226. *valuep = (short)value;
  1227. } break;
  1228. case DO_INT:
  1229. {
  1230. int *valuep = va_arg(ap, int *);
  1231. *valuep = (int)value;
  1232. } break;
  1233. case DO_LONG:
  1234. {
  1235. long *valuep = va_arg(ap, long *);
  1236. *valuep = value;
  1237. } break;
  1238. case DO_LONGLONG:
  1239. case DO_SIZE_T:
  1240. /* Handled above */
  1241. break;
  1242. }
  1243. ++retval;
  1244. }
  1245. }
  1246. done = SDL_TRUE;
  1247. break;
  1248. case 'p':
  1249. {
  1250. uintptr_t value = 0;
  1251. advance = SDL_ScanUintPtrT(text, 16, &value);
  1252. text += advance;
  1253. if (advance && !suppress) {
  1254. void **valuep = va_arg(ap, void **);
  1255. *valuep = (void *)value;
  1256. ++retval;
  1257. }
  1258. }
  1259. done = SDL_TRUE;
  1260. break;
  1261. case 'f':
  1262. {
  1263. double value = 0.0;
  1264. advance = SDL_ScanFloat(text, &value);
  1265. text += advance;
  1266. if (advance && !suppress) {
  1267. float *valuep = va_arg(ap, float *);
  1268. *valuep = (float)value;
  1269. ++retval;
  1270. }
  1271. }
  1272. done = SDL_TRUE;
  1273. break;
  1274. case 's':
  1275. if (suppress) {
  1276. while (!SDL_isspace((unsigned char)*text)) {
  1277. ++text;
  1278. if (count) {
  1279. if (--count == 0) {
  1280. break;
  1281. }
  1282. }
  1283. }
  1284. } else {
  1285. char *valuep = va_arg(ap, char *);
  1286. while (!SDL_isspace((unsigned char)*text)) {
  1287. *valuep++ = *text++;
  1288. if (count) {
  1289. if (--count == 0) {
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. *valuep = '\0';
  1295. ++retval;
  1296. }
  1297. done = SDL_TRUE;
  1298. break;
  1299. default:
  1300. done = SDL_TRUE;
  1301. break;
  1302. }
  1303. ++fmt;
  1304. }
  1305. continue;
  1306. }
  1307. if (*text == *fmt) {
  1308. ++text;
  1309. ++fmt;
  1310. continue;
  1311. }
  1312. /* Text didn't match format specifier */
  1313. break;
  1314. }
  1315. return retval;
  1316. }
  1317. #endif /* HAVE_VSSCANF */
  1318. int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1319. {
  1320. va_list ap;
  1321. int retval;
  1322. va_start(ap, fmt);
  1323. retval = SDL_vsnprintf(text, maxlen, fmt, ap);
  1324. va_end(ap);
  1325. return retval;
  1326. }
  1327. #if defined(HAVE_LIBC) && defined(__WATCOMC__)
  1328. /* _vsnprintf() doesn't ensure nul termination */
  1329. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1330. {
  1331. int retval;
  1332. if (!fmt) {
  1333. fmt = "";
  1334. }
  1335. retval = _vsnprintf(text, maxlen, fmt, ap);
  1336. if (maxlen > 0) {
  1337. text[maxlen - 1] = '\0';
  1338. }
  1339. if (retval < 0) {
  1340. retval = (int)maxlen;
  1341. }
  1342. return retval;
  1343. }
  1344. #elif defined(HAVE_VSNPRINTF)
  1345. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1346. {
  1347. if (!fmt) {
  1348. fmt = "";
  1349. }
  1350. return vsnprintf(text, maxlen, fmt, ap);
  1351. }
  1352. #else
  1353. #define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
  1354. /* FIXME: implement more of the format specifiers */
  1355. typedef enum
  1356. {
  1357. SDL_CASE_NOCHANGE,
  1358. SDL_CASE_LOWER,
  1359. SDL_CASE_UPPER
  1360. } SDL_letter_case;
  1361. typedef struct
  1362. {
  1363. SDL_bool left_justify; /* for now: ignored. */
  1364. SDL_bool force_sign;
  1365. SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */
  1366. SDL_bool pad_zeroes;
  1367. SDL_letter_case force_case;
  1368. int width;
  1369. int radix;
  1370. int precision;
  1371. } SDL_FormatInfo;
  1372. static size_t
  1373. SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
  1374. {
  1375. size_t length = 0;
  1376. size_t slen, sz;
  1377. if (string == NULL) {
  1378. string = "(null)";
  1379. }
  1380. sz = SDL_strlen(string);
  1381. if (info && info->width > 0 && (size_t)info->width > sz) {
  1382. const char fill = info->pad_zeroes ? '0' : ' ';
  1383. size_t width = info->width - sz;
  1384. size_t filllen;
  1385. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1386. width += sz - (size_t)info->precision;
  1387. }
  1388. filllen = SDL_min(width, maxlen);
  1389. SDL_memset(text, fill, filllen);
  1390. text += filllen;
  1391. maxlen -= filllen;
  1392. length += width;
  1393. }
  1394. SDL_strlcpy(text, string, maxlen);
  1395. length += sz;
  1396. if (info) {
  1397. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1398. slen = (size_t)info->precision;
  1399. if (slen < maxlen) {
  1400. text[slen] = '\0';
  1401. }
  1402. length -= (sz - slen);
  1403. }
  1404. if (maxlen > 1) {
  1405. if (info->force_case == SDL_CASE_LOWER) {
  1406. SDL_strlwr(text);
  1407. } else if (info->force_case == SDL_CASE_UPPER) {
  1408. SDL_strupr(text);
  1409. }
  1410. }
  1411. }
  1412. return length;
  1413. }
  1414. static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
  1415. { /* left-pad num with zeroes. */
  1416. size_t sz, pad, have_sign;
  1417. if (info == NULL) {
  1418. return;
  1419. }
  1420. have_sign = 0;
  1421. if (*num == '-' || *num == '+') {
  1422. have_sign = 1;
  1423. ++num;
  1424. --maxlen;
  1425. }
  1426. sz = SDL_strlen(num);
  1427. if (info->precision > 0 && sz < (size_t)info->precision) {
  1428. pad = (size_t)info->precision - sz;
  1429. if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
  1430. SDL_memmove(num + pad, num, sz + 1);
  1431. SDL_memset(num, '0', pad);
  1432. }
  1433. }
  1434. info->precision = -1; /* so that SDL_PrintString() doesn't make a mess. */
  1435. if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
  1436. /* handle here: spaces are added before the sign
  1437. but zeroes must be placed _after_ the sign. */
  1438. /* sz hasn't changed: we ignore pad_zeroes if a precision is given. */
  1439. pad = (size_t)info->width - sz - have_sign;
  1440. if (pad + sz + 1 <= maxlen) {
  1441. SDL_memmove(num + pad, num, sz + 1);
  1442. SDL_memset(num, '0', pad);
  1443. }
  1444. info->width = 0; /* so that SDL_PrintString() doesn't make a mess. */
  1445. }
  1446. }
  1447. static size_t
  1448. SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
  1449. {
  1450. char num[130], *p = num;
  1451. if (info->force_sign && value >= 0L) {
  1452. *p++ = '+';
  1453. }
  1454. SDL_ltoa(value, p, info ? info->radix : 10);
  1455. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1456. return SDL_PrintString(text, maxlen, info, num);
  1457. }
  1458. static size_t
  1459. SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
  1460. {
  1461. char num[130];
  1462. SDL_ultoa(value, num, info ? info->radix : 10);
  1463. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1464. return SDL_PrintString(text, maxlen, info, num);
  1465. }
  1466. static size_t
  1467. SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
  1468. {
  1469. char num[130], *p = num;
  1470. if (info->force_sign && value >= (Sint64)0) {
  1471. *p++ = '+';
  1472. }
  1473. SDL_lltoa(value, p, info ? info->radix : 10);
  1474. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1475. return SDL_PrintString(text, maxlen, info, num);
  1476. }
  1477. static size_t
  1478. SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
  1479. {
  1480. char num[130];
  1481. SDL_ulltoa(value, num, info ? info->radix : 10);
  1482. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1483. return SDL_PrintString(text, maxlen, info, num);
  1484. }
  1485. static size_t
  1486. SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg)
  1487. {
  1488. size_t length = 0;
  1489. /* This isn't especially accurate, but hey, it's easy. :) */
  1490. unsigned long value;
  1491. if (arg < 0) {
  1492. if (length < maxlen) {
  1493. text[length] = '-';
  1494. }
  1495. ++length;
  1496. arg = -arg;
  1497. } else if (info->force_sign) {
  1498. if (length < maxlen) {
  1499. text[length] = '+';
  1500. }
  1501. ++length;
  1502. }
  1503. value = (unsigned long)arg;
  1504. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
  1505. arg -= value;
  1506. if (info->precision < 0) {
  1507. info->precision = 6;
  1508. }
  1509. if (info->force_type || info->precision > 0) {
  1510. int mult = 10;
  1511. if (length < maxlen) {
  1512. text[length] = '.';
  1513. }
  1514. ++length;
  1515. while (info->precision-- > 0) {
  1516. value = (unsigned long)(arg * mult);
  1517. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, NULL, value);
  1518. arg -= (double)value / mult;
  1519. mult *= 10;
  1520. }
  1521. }
  1522. if (info->width > 0 && (size_t)info->width > length) {
  1523. const char fill = info->pad_zeroes ? '0' : ' ';
  1524. size_t width = info->width - length;
  1525. size_t filllen, movelen;
  1526. filllen = SDL_min(width, maxlen);
  1527. movelen = SDL_min(length, (maxlen - filllen));
  1528. SDL_memmove(&text[filllen], text, movelen);
  1529. SDL_memset(text, fill, filllen);
  1530. length += width;
  1531. }
  1532. return length;
  1533. }
  1534. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1535. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1536. {
  1537. size_t length = 0;
  1538. if (!text) {
  1539. maxlen = 0;
  1540. }
  1541. if (!fmt) {
  1542. fmt = "";
  1543. }
  1544. while (*fmt) {
  1545. if (*fmt == '%') {
  1546. SDL_bool done = SDL_FALSE;
  1547. SDL_bool check_flag;
  1548. SDL_FormatInfo info;
  1549. enum
  1550. {
  1551. DO_INT,
  1552. DO_LONG,
  1553. DO_LONGLONG,
  1554. DO_SIZE_T
  1555. } inttype = DO_INT;
  1556. SDL_zero(info);
  1557. info.radix = 10;
  1558. info.precision = -1;
  1559. check_flag = SDL_TRUE;
  1560. while (check_flag) {
  1561. ++fmt;
  1562. switch (*fmt) {
  1563. case '-':
  1564. info.left_justify = SDL_TRUE;
  1565. break;
  1566. case '+':
  1567. info.force_sign = SDL_TRUE;
  1568. break;
  1569. case '#':
  1570. info.force_type = SDL_TRUE;
  1571. break;
  1572. case '0':
  1573. info.pad_zeroes = SDL_TRUE;
  1574. break;
  1575. default:
  1576. check_flag = SDL_FALSE;
  1577. break;
  1578. }
  1579. }
  1580. if (*fmt >= '0' && *fmt <= '9') {
  1581. info.width = SDL_strtol(fmt, (char **)&fmt, 0);
  1582. } else if (*fmt == '*') {
  1583. ++fmt;
  1584. info.width = va_arg(ap, int);
  1585. }
  1586. if (*fmt == '.') {
  1587. ++fmt;
  1588. if (*fmt >= '0' && *fmt <= '9') {
  1589. info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
  1590. } else if (*fmt == '*') {
  1591. ++fmt;
  1592. info.precision = va_arg(ap, int);
  1593. } else {
  1594. info.precision = 0;
  1595. }
  1596. if (info.precision < 0) {
  1597. info.precision = 0;
  1598. }
  1599. }
  1600. while (!done) {
  1601. switch (*fmt) {
  1602. case '%':
  1603. if (length < maxlen) {
  1604. text[length] = '%';
  1605. }
  1606. ++length;
  1607. done = SDL_TRUE;
  1608. break;
  1609. case 'c':
  1610. /* char is promoted to int when passed through (...) */
  1611. if (length < maxlen) {
  1612. text[length] = (char)va_arg(ap, int);
  1613. }
  1614. ++length;
  1615. done = SDL_TRUE;
  1616. break;
  1617. case 'h':
  1618. /* short is promoted to int when passed through (...) */
  1619. break;
  1620. case 'l':
  1621. if (inttype < DO_LONGLONG) {
  1622. ++inttype;
  1623. }
  1624. break;
  1625. case 'I':
  1626. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1627. fmt += 2;
  1628. inttype = DO_LONGLONG;
  1629. }
  1630. break;
  1631. case 'z':
  1632. inttype = DO_SIZE_T;
  1633. break;
  1634. case 'i':
  1635. case 'd':
  1636. if (info.precision >= 0) {
  1637. info.pad_zeroes = SDL_FALSE;
  1638. }
  1639. switch (inttype) {
  1640. case DO_INT:
  1641. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1642. (long)va_arg(ap, int));
  1643. break;
  1644. case DO_LONG:
  1645. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1646. va_arg(ap, long));
  1647. break;
  1648. case DO_LONGLONG:
  1649. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1650. va_arg(ap, Sint64));
  1651. break;
  1652. case DO_SIZE_T:
  1653. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1654. va_arg(ap, size_t));
  1655. break;
  1656. }
  1657. done = SDL_TRUE;
  1658. break;
  1659. case 'p':
  1660. case 'x':
  1661. info.force_case = SDL_CASE_LOWER;
  1662. SDL_FALLTHROUGH;
  1663. case 'X':
  1664. if (info.force_case == SDL_CASE_NOCHANGE) {
  1665. info.force_case = SDL_CASE_UPPER;
  1666. }
  1667. if (info.radix == 10) {
  1668. info.radix = 16;
  1669. }
  1670. if (*fmt == 'p') {
  1671. inttype = DO_LONG;
  1672. }
  1673. SDL_FALLTHROUGH;
  1674. case 'o':
  1675. if (info.radix == 10) {
  1676. info.radix = 8;
  1677. }
  1678. SDL_FALLTHROUGH;
  1679. case 'u':
  1680. info.force_sign = SDL_FALSE;
  1681. if (info.precision >= 0) {
  1682. info.pad_zeroes = SDL_FALSE;
  1683. }
  1684. switch (inttype) {
  1685. case DO_INT:
  1686. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1687. (unsigned long)
  1688. va_arg(ap, unsigned int));
  1689. break;
  1690. case DO_LONG:
  1691. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  1692. va_arg(ap, unsigned long));
  1693. break;
  1694. case DO_LONGLONG:
  1695. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1696. va_arg(ap, Uint64));
  1697. break;
  1698. case DO_SIZE_T:
  1699. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  1700. va_arg(ap, size_t));
  1701. break;
  1702. }
  1703. done = SDL_TRUE;
  1704. break;
  1705. case 'f':
  1706. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double));
  1707. done = SDL_TRUE;
  1708. break;
  1709. case 'S':
  1710. {
  1711. /* In practice this is used on Windows for WCHAR strings */
  1712. wchar_t *wide_arg = va_arg(ap, wchar_t *);
  1713. if (wide_arg) {
  1714. char *arg = SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(wide_arg), (SDL_wcslen(wide_arg) + 1) * sizeof(*wide_arg));
  1715. info.pad_zeroes = SDL_FALSE;
  1716. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, arg);
  1717. SDL_free(arg);
  1718. } else {
  1719. info.pad_zeroes = SDL_FALSE;
  1720. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, NULL);
  1721. }
  1722. done = SDL_TRUE;
  1723. } break;
  1724. case 's':
  1725. info.pad_zeroes = SDL_FALSE;
  1726. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
  1727. done = SDL_TRUE;
  1728. break;
  1729. default:
  1730. done = SDL_TRUE;
  1731. break;
  1732. }
  1733. ++fmt;
  1734. }
  1735. } else {
  1736. if (length < maxlen) {
  1737. text[length] = *fmt;
  1738. }
  1739. ++fmt;
  1740. ++length;
  1741. }
  1742. }
  1743. if (length < maxlen) {
  1744. text[length] = '\0';
  1745. } else if (maxlen > 0) {
  1746. text[maxlen - 1] = '\0';
  1747. }
  1748. return (int)length;
  1749. }
  1750. #undef TEXT_AND_LEN_ARGS
  1751. #endif /* HAVE_VSNPRINTF */
  1752. int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1753. {
  1754. va_list ap;
  1755. int retval;
  1756. va_start(ap, fmt);
  1757. retval = SDL_vasprintf(strp, fmt, ap);
  1758. va_end(ap);
  1759. return retval;
  1760. }
  1761. int SDL_vasprintf(char **strp, const char *fmt, va_list ap)
  1762. {
  1763. int retval;
  1764. int size = 100; /* Guess we need no more than 100 bytes */
  1765. char *p, *np;
  1766. va_list aq;
  1767. *strp = NULL;
  1768. p = (char *)SDL_malloc(size);
  1769. if (p == NULL) {
  1770. return -1;
  1771. }
  1772. while (1) {
  1773. /* Try to print in the allocated space */
  1774. va_copy(aq, ap);
  1775. retval = SDL_vsnprintf(p, size, fmt, aq);
  1776. va_end(aq);
  1777. /* Check error code */
  1778. if (retval < 0) {
  1779. return retval;
  1780. }
  1781. /* If that worked, return the string */
  1782. if (retval < size) {
  1783. *strp = p;
  1784. return retval;
  1785. }
  1786. /* Else try again with more space */
  1787. size = retval + 1; /* Precisely what is needed */
  1788. np = (char *)SDL_realloc(p, size);
  1789. if (np == NULL) {
  1790. SDL_free(p);
  1791. return -1;
  1792. } else {
  1793. p = np;
  1794. }
  1795. }
  1796. }
  1797. /* vi: set ts=4 sw=4 expandtab: */