SDL_string.c 52 KB

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