SDL_string.c 53 KB

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