SDL_string.c 53 KB

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