SDL_string.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #ifdef SDL_PLATFORM_VITA
  22. #include <psp2/kernel/clib.h>
  23. #endif
  24. #include "SDL_sysstdlib.h"
  25. #include "SDL_casefolding.h"
  26. #if defined(__SIZEOF_WCHAR_T__)
  27. #define SDL_SIZEOF_WCHAR_T __SIZEOF_WCHAR_T__
  28. #elif defined(SDL_PLATFORM_WINDOWS)
  29. #define SDL_SIZEOF_WCHAR_T 2
  30. #else // assume everything else is UTF-32 (add more tests if compiler-assert fails below!)
  31. #define SDL_SIZEOF_WCHAR_T 4
  32. #endif
  33. SDL_COMPILE_TIME_ASSERT(sizeof_wchar_t, sizeof(wchar_t) == SDL_SIZEOF_WCHAR_T);
  34. // this expects `from` and `to` to be UTF-32 encoding!
  35. int SDL_CaseFoldUnicode(const Uint32 from, Uint32 *to)
  36. {
  37. // !!! FIXME: since the hashtable is static, maybe we should binary
  38. // !!! FIXME: search it instead of walking the whole bucket.
  39. if (from < 128) { // low-ASCII, easy!
  40. if ((from >= 'A') && (from <= 'Z')) {
  41. *to = 'a' + (from - 'A');
  42. return 1;
  43. }
  44. } else if (from <= 0xFFFF) { // the Basic Multilingual Plane.
  45. const Uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  46. const Uint16 from16 = (Uint16) from;
  47. // see if it maps to a single char (most common)...
  48. {
  49. const CaseFoldHashBucket1_16 *bucket = &case_fold_hash1_16[hash];
  50. const int count = (int) bucket->count;
  51. for (int i = 0; i < count; i++) {
  52. const CaseFoldMapping1_16 *mapping = &bucket->list[i];
  53. if (mapping->from == from16) {
  54. *to = mapping->to0;
  55. return 1;
  56. }
  57. }
  58. }
  59. // see if it folds down to two chars...
  60. {
  61. const CaseFoldHashBucket2_16 *bucket = &case_fold_hash2_16[hash & 15];
  62. const int count = (int) bucket->count;
  63. for (int i = 0; i < count; i++) {
  64. const CaseFoldMapping2_16 *mapping = &bucket->list[i];
  65. if (mapping->from == from16) {
  66. to[0] = mapping->to0;
  67. to[1] = mapping->to1;
  68. return 2;
  69. }
  70. }
  71. }
  72. // okay, maybe it's _three_ characters!
  73. {
  74. const CaseFoldHashBucket3_16 *bucket = &case_fold_hash3_16[hash & 3];
  75. const int count = (int) bucket->count;
  76. for (int i = 0; i < count; i++) {
  77. const CaseFoldMapping3_16 *mapping = &bucket->list[i];
  78. if (mapping->from == from16) {
  79. to[0] = mapping->to0;
  80. to[1] = mapping->to1;
  81. to[2] = mapping->to2;
  82. return 3;
  83. }
  84. }
  85. }
  86. } else { // codepoint that doesn't fit in 16 bits.
  87. const Uint8 hash = ((from ^ (from >> 8)) & 0xFF);
  88. const CaseFoldHashBucket1_32 *bucket = &case_fold_hash1_32[hash & 15];
  89. const int count = (int) bucket->count;
  90. for (int i = 0; i < count; i++) {
  91. const CaseFoldMapping1_32 *mapping = &bucket->list[i];
  92. if (mapping->from == from) {
  93. *to = mapping->to0;
  94. return 1;
  95. }
  96. }
  97. }
  98. // Not found...there's no folding needed for this codepoint.
  99. *to = from;
  100. return 1;
  101. }
  102. #define UNICODE_STRCASECMP(bits, slen1, slen2, update_slen1, update_slen2) \
  103. Uint32 folded1[3], folded2[3]; \
  104. int head1 = 0, tail1 = 0, head2 = 0, tail2 = 0; \
  105. while (SDL_TRUE) { \
  106. Uint32 cp1, cp2; \
  107. if (head1 != tail1) { \
  108. cp1 = folded1[tail1++]; \
  109. } else { \
  110. const Uint##bits *str1start = (const Uint##bits *) str1; \
  111. head1 = SDL_CaseFoldUnicode(StepUTF##bits(&str1, slen1), folded1); \
  112. update_slen1; \
  113. cp1 = folded1[0]; \
  114. tail1 = 1; \
  115. } \
  116. if (head2 != tail2) { \
  117. cp2 = folded2[tail2++]; \
  118. } else { \
  119. const Uint##bits *str2start = (const Uint##bits *) str2; \
  120. head2 = SDL_CaseFoldUnicode(StepUTF##bits(&str2, slen2), folded2); \
  121. update_slen2; \
  122. cp2 = folded2[0]; \
  123. tail2 = 1; \
  124. } \
  125. if (cp1 < cp2) { \
  126. return -1; \
  127. } else if (cp1 > cp2) { \
  128. return 1; \
  129. } else if (cp1 == 0) { \
  130. break; /* complete match. */ \
  131. } \
  132. } \
  133. return 0
  134. static Uint32 StepUTF8(const char **_str, const size_t slen)
  135. {
  136. /*
  137. * From rfc3629, the UTF-8 spec:
  138. * https://www.ietf.org/rfc/rfc3629.txt
  139. *
  140. * Char. number range | UTF-8 octet sequence
  141. * (hexadecimal) | (binary)
  142. * --------------------+---------------------------------------------
  143. * 0000 0000-0000 007F | 0xxxxxxx
  144. * 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
  145. * 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
  146. * 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  147. */
  148. const Uint8 *str = (const Uint8 *) *_str;
  149. const Uint32 octet = (Uint32) (slen ? *str : 0);
  150. if (octet == 0) { // null terminator, end of string.
  151. return 0; // don't advance `*_str`.
  152. } else if ((octet & 0x80) == 0) { // 0xxxxxxx: one byte codepoint.
  153. (*_str)++;
  154. return octet;
  155. } else if (((octet & 0xE0) == 0xC0) && (slen >= 2)) { // 110xxxxx 10xxxxxx: two byte codepoint.
  156. const Uint8 str1 = str[1];
  157. if ((str1 & 0xC0) == 0x80) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
  158. const Uint32 retval = ((octet & 0x1F) << 6) | (str1 & 0x3F);
  159. if (retval >= 0x0080) { // rfc3629 says you can't use overlong sequences for smaller values.
  160. *_str += 2;
  161. return retval;
  162. }
  163. }
  164. } else if (((octet & 0xF0) == 0xE0) && (slen >= 3)) { // 1110xxxx 10xxxxxx 10xxxxxx: three byte codepoint.
  165. const Uint8 str1 = str[1];
  166. const Uint8 str2 = str[2];
  167. if (((str1 & 0xC0) == 0x80) && ((str2 & 0xC0) == 0x80)) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
  168. const Uint32 octet2 = ((Uint32) (str1 & 0x3F)) << 6;
  169. const Uint32 octet3 = ((Uint32) (str2 & 0x3F));
  170. const Uint32 retval = ((octet & 0x0F) << 12) | octet2 | octet3;
  171. if (retval >= 0x800) { // rfc3629 says you can't use overlong sequences for smaller values.
  172. if ((retval < 0xD800) || (retval > 0xDFFF)) { // UTF-16 surrogate values are illegal in UTF-8.
  173. *_str += 3;
  174. return retval;
  175. }
  176. }
  177. }
  178. } else if (((octet & 0xF8) == 0xF0) && (slen >= 4)) { // 11110xxxx 10xxxxxx 10xxxxxx 10xxxxxx: four byte codepoint.
  179. const Uint8 str1 = str[1];
  180. const Uint8 str2 = str[2];
  181. const Uint8 str3 = str[3];
  182. if (((str1 & 0xC0) == 0x80) && ((str2 & 0xC0) == 0x80) && ((str3 & 0xC0) == 0x80)) { // If trailing bytes aren't 10xxxxxx, sequence is bogus.
  183. const Uint32 octet2 = ((Uint32) (str1 & 0x1F)) << 12;
  184. const Uint32 octet3 = ((Uint32) (str2 & 0x3F)) << 6;
  185. const Uint32 octet4 = ((Uint32) (str3 & 0x3F));
  186. const Uint32 retval = ((octet & 0x07) << 18) | octet2 | octet3 | octet4;
  187. if (retval >= 0x10000) { // rfc3629 says you can't use overlong sequences for smaller values.
  188. *_str += 4;
  189. return retval;
  190. }
  191. }
  192. }
  193. // bogus byte, skip ahead, return a REPLACEMENT CHARACTER.
  194. (*_str)++;
  195. return SDL_INVALID_UNICODE_CODEPOINT;
  196. }
  197. Uint32 SDL_StepUTF8(const char **pstr, size_t *pslen)
  198. {
  199. if (!pslen) {
  200. return StepUTF8(pstr, 4); // 4 == max codepoint size.
  201. }
  202. const char *origstr = *pstr;
  203. const Uint32 retval = StepUTF8(pstr, *pslen);
  204. *pslen -= (size_t) (*pstr - origstr);
  205. return retval;
  206. }
  207. #if (SDL_SIZEOF_WCHAR_T == 2)
  208. static Uint32 StepUTF16(const Uint16 **_str, const size_t slen)
  209. {
  210. const Uint16 *str = *_str;
  211. Uint32 cp = (Uint32) *(str++);
  212. if (cp == 0) {
  213. return 0; // don't advance string pointer.
  214. } else if ((cp >= 0xDC00) && (cp <= 0xDFFF)) {
  215. cp = SDL_INVALID_UNICODE_CODEPOINT; // Orphaned second half of surrogate pair
  216. } else if ((cp >= 0xD800) && (cp <= 0xDBFF)) { // start of surrogate pair!
  217. const Uint32 pair = (Uint32) *str;
  218. if ((pair == 0) || ((pair < 0xDC00) || (pair > 0xDFFF))) {
  219. cp = SDL_INVALID_UNICODE_CODEPOINT;
  220. } else {
  221. str++; // eat the other surrogate.
  222. cp = 0x10000 + (((cp - 0xD800) << 10) | (pair - 0xDC00));
  223. }
  224. }
  225. *_str = str;
  226. return (cp > 0x10FFFF) ? SDL_INVALID_UNICODE_CODEPOINT : cp;
  227. }
  228. #elif (SDL_SIZEOF_WCHAR_T == 4)
  229. static Uint32 StepUTF32(const Uint32 **_str, const size_t slen)
  230. {
  231. if (!slen) {
  232. return 0;
  233. }
  234. const Uint32 *str = *_str;
  235. const Uint32 cp = *str;
  236. if (cp == 0) {
  237. return 0; // don't advance string pointer.
  238. }
  239. (*_str)++;
  240. return (cp > 0x10FFFF) ? SDL_INVALID_UNICODE_CODEPOINT : cp;
  241. }
  242. #endif
  243. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_WCSTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  244. #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F'))
  245. #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f'))
  246. #endif
  247. #define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4)
  248. #define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF)
  249. static size_t UTF8_GetTrailingBytes(unsigned char c)
  250. {
  251. if (c >= 0xC0 && c <= 0xDF) {
  252. return 1;
  253. } else if (c >= 0xE0 && c <= 0xEF) {
  254. return 2;
  255. } else if (c >= 0xF0 && c <= 0xF4) {
  256. return 3;
  257. }
  258. return 0;
  259. }
  260. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOL) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  261. static size_t SDL_ScanLong(const char *text, int count, int radix, long *valuep)
  262. {
  263. const char *textstart = text;
  264. long value = 0;
  265. SDL_bool negative = SDL_FALSE;
  266. if (*text == '-') {
  267. negative = SDL_TRUE;
  268. ++text;
  269. }
  270. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  271. text += 2;
  272. }
  273. for (;;) {
  274. int v;
  275. if (SDL_isdigit((unsigned char)*text)) {
  276. v = *text - '0';
  277. } else if (radix == 16 && SDL_isupperhex(*text)) {
  278. v = 10 + (*text - 'A');
  279. } else if (radix == 16 && SDL_islowerhex(*text)) {
  280. v = 10 + (*text - 'a');
  281. } else {
  282. break;
  283. }
  284. value *= radix;
  285. value += v;
  286. ++text;
  287. if (count > 0 && (text - textstart) == count) {
  288. break;
  289. }
  290. }
  291. if (valuep && text > textstart) {
  292. if (negative && value) {
  293. *valuep = -value;
  294. } else {
  295. *valuep = value;
  296. }
  297. }
  298. return text - textstart;
  299. }
  300. #endif
  301. #if !defined(HAVE_WCSTOL)
  302. static size_t SDL_ScanLongW(const wchar_t *text, int count, int radix, long *valuep)
  303. {
  304. const wchar_t *textstart = text;
  305. long value = 0;
  306. SDL_bool negative = SDL_FALSE;
  307. if (*text == '-') {
  308. negative = SDL_TRUE;
  309. ++text;
  310. }
  311. if (radix == 16 && SDL_wcsncmp(text, L"0x", 2) == 0) {
  312. text += 2;
  313. }
  314. for (;;) {
  315. int v;
  316. if (*text >= '0' && *text <= '9') {
  317. v = *text - '0';
  318. } else if (radix == 16 && SDL_isupperhex(*text)) {
  319. v = 10 + (*text - 'A');
  320. } else if (radix == 16 && SDL_islowerhex(*text)) {
  321. v = 10 + (*text - 'a');
  322. } else {
  323. break;
  324. }
  325. value *= radix;
  326. value += v;
  327. ++text;
  328. if (count > 0 && (text - textstart) == count) {
  329. break;
  330. }
  331. }
  332. if (valuep && text > textstart) {
  333. if (negative && value) {
  334. *valuep = -value;
  335. } else {
  336. *valuep = value;
  337. }
  338. }
  339. return text - textstart;
  340. }
  341. #endif
  342. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOUL) || !defined(HAVE_STRTOD)
  343. static size_t SDL_ScanUnsignedLong(const char *text, int count, int radix, unsigned long *valuep)
  344. {
  345. const char *textstart = text;
  346. unsigned long value = 0;
  347. if (*text == '-') {
  348. return SDL_ScanLong(text, count, radix, (long *)valuep);
  349. }
  350. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  351. text += 2;
  352. }
  353. for (;;) {
  354. int v;
  355. if (SDL_isdigit((unsigned char)*text)) {
  356. v = *text - '0';
  357. } else if (radix == 16 && SDL_isupperhex(*text)) {
  358. v = 10 + (*text - 'A');
  359. } else if (radix == 16 && SDL_islowerhex(*text)) {
  360. v = 10 + (*text - 'a');
  361. } else {
  362. break;
  363. }
  364. value *= radix;
  365. value += v;
  366. ++text;
  367. if (count > 0 && (text - textstart) == count) {
  368. break;
  369. }
  370. }
  371. if (valuep && text > textstart) {
  372. *valuep = value;
  373. }
  374. return text - textstart;
  375. }
  376. #endif
  377. #ifndef HAVE_VSSCANF
  378. static size_t SDL_ScanUintPtrT(const char *text, int radix, uintptr_t *valuep)
  379. {
  380. const char *textstart = text;
  381. uintptr_t value = 0;
  382. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  383. text += 2;
  384. }
  385. for (;;) {
  386. int v;
  387. if (SDL_isdigit((unsigned char)*text)) {
  388. v = *text - '0';
  389. } else if (radix == 16 && SDL_isupperhex(*text)) {
  390. v = 10 + (*text - 'A');
  391. } else if (radix == 16 && SDL_islowerhex(*text)) {
  392. v = 10 + (*text - 'a');
  393. } else {
  394. break;
  395. }
  396. value *= radix;
  397. value += v;
  398. ++text;
  399. }
  400. if (valuep && text > textstart) {
  401. *valuep = value;
  402. }
  403. return text - textstart;
  404. }
  405. #endif
  406. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOLL) || !defined(HAVE_STRTOULL)
  407. static size_t SDL_ScanLongLong(const char *text, int count, int radix, Sint64 *valuep)
  408. {
  409. const char *textstart = text;
  410. Sint64 value = 0;
  411. SDL_bool negative = SDL_FALSE;
  412. if (*text == '-') {
  413. negative = SDL_TRUE;
  414. ++text;
  415. }
  416. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  417. text += 2;
  418. }
  419. for (;;) {
  420. int v;
  421. if (SDL_isdigit((unsigned char)*text)) {
  422. v = *text - '0';
  423. } else if (radix == 16 && SDL_isupperhex(*text)) {
  424. v = 10 + (*text - 'A');
  425. } else if (radix == 16 && SDL_islowerhex(*text)) {
  426. v = 10 + (*text - 'a');
  427. } else {
  428. break;
  429. }
  430. value *= radix;
  431. value += v;
  432. ++text;
  433. if (count > 0 && (text - textstart) == count) {
  434. break;
  435. }
  436. }
  437. if (valuep && text > textstart) {
  438. if (negative && value) {
  439. *valuep = -value;
  440. } else {
  441. *valuep = value;
  442. }
  443. }
  444. return text - textstart;
  445. }
  446. #endif
  447. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOULL)
  448. static size_t SDL_ScanUnsignedLongLong(const char *text, int count, int radix, Uint64 *valuep)
  449. {
  450. const char *textstart = text;
  451. Uint64 value = 0;
  452. if (*text == '-') {
  453. return SDL_ScanLongLong(text, count, radix, (Sint64 *)valuep);
  454. }
  455. if (radix == 16 && SDL_strncmp(text, "0x", 2) == 0) {
  456. text += 2;
  457. }
  458. for (;;) {
  459. int v;
  460. if (SDL_isdigit((unsigned char)*text)) {
  461. v = *text - '0';
  462. } else if (radix == 16 && SDL_isupperhex(*text)) {
  463. v = 10 + (*text - 'A');
  464. } else if (radix == 16 && SDL_islowerhex(*text)) {
  465. v = 10 + (*text - 'a');
  466. } else {
  467. break;
  468. }
  469. value *= radix;
  470. value += v;
  471. ++text;
  472. if (count > 0 && (text - textstart) == count) {
  473. break;
  474. }
  475. }
  476. if (valuep && text > textstart) {
  477. *valuep = value;
  478. }
  479. return text - textstart;
  480. }
  481. #endif
  482. #if !defined(HAVE_VSSCANF) || !defined(HAVE_STRTOD)
  483. static size_t SDL_ScanFloat(const char *text, double *valuep)
  484. {
  485. const char *textstart = text;
  486. unsigned long lvalue = 0;
  487. double value = 0.0;
  488. SDL_bool negative = SDL_FALSE;
  489. if (*text == '-') {
  490. negative = SDL_TRUE;
  491. ++text;
  492. }
  493. text += SDL_ScanUnsignedLong(text, 0, 10, &lvalue);
  494. value += lvalue;
  495. if (*text == '.') {
  496. int mult = 10;
  497. ++text;
  498. while (SDL_isdigit((unsigned char)*text)) {
  499. lvalue = *text - '0';
  500. value += (double)lvalue / mult;
  501. mult *= 10;
  502. ++text;
  503. }
  504. }
  505. if (valuep && text > textstart) {
  506. if (negative && value) {
  507. *valuep = -value;
  508. } else {
  509. *valuep = value;
  510. }
  511. }
  512. return text - textstart;
  513. }
  514. #endif
  515. int SDL_memcmp(const void *s1, const void *s2, size_t len)
  516. {
  517. #ifdef SDL_PLATFORM_VITA
  518. /*
  519. Using memcmp on NULL is UB per POSIX / C99 7.21.1/2.
  520. But, both linux and bsd allow that, with an exception:
  521. zero length strings are always identical, so NULLs are never dereferenced.
  522. sceClibMemcmp on PSVita doesn't allow that, so we check ourselves.
  523. */
  524. if (len == 0) {
  525. return 0;
  526. }
  527. return sceClibMemcmp(s1, s2, len);
  528. #elif defined(HAVE_MEMCMP)
  529. return memcmp(s1, s2, len);
  530. #else
  531. char *s1p = (char *)s1;
  532. char *s2p = (char *)s2;
  533. while (len--) {
  534. if (*s1p != *s2p) {
  535. return *s1p - *s2p;
  536. }
  537. ++s1p;
  538. ++s2p;
  539. }
  540. return 0;
  541. #endif /* HAVE_MEMCMP */
  542. }
  543. size_t SDL_strlen(const char *string)
  544. {
  545. #ifdef HAVE_STRLEN
  546. return strlen(string);
  547. #else
  548. size_t len = 0;
  549. while (*string++) {
  550. ++len;
  551. }
  552. return len;
  553. #endif /* HAVE_STRLEN */
  554. }
  555. size_t SDL_strnlen(const char *string, size_t maxlen)
  556. {
  557. #ifdef HAVE_STRNLEN
  558. return strnlen(string, maxlen);
  559. #else
  560. size_t len = 0;
  561. while (len < maxlen && *string++) {
  562. ++len;
  563. }
  564. return len;
  565. #endif /* HAVE_STRNLEN */
  566. }
  567. size_t SDL_wcslen(const wchar_t *string)
  568. {
  569. #ifdef HAVE_WCSLEN
  570. return wcslen(string);
  571. #else
  572. size_t len = 0;
  573. while (*string++) {
  574. ++len;
  575. }
  576. return len;
  577. #endif /* HAVE_WCSLEN */
  578. }
  579. size_t SDL_wcsnlen(const wchar_t *string, size_t maxlen)
  580. {
  581. #ifdef HAVE_WCSNLEN
  582. return wcsnlen(string, maxlen);
  583. #else
  584. size_t len = 0;
  585. while (len < maxlen && *string++) {
  586. ++len;
  587. }
  588. return len;
  589. #endif /* HAVE_WCSNLEN */
  590. }
  591. size_t SDL_wcslcpy(SDL_OUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  592. {
  593. #ifdef HAVE_WCSLCPY
  594. return wcslcpy(dst, src, maxlen);
  595. #else
  596. size_t srclen = SDL_wcslen(src);
  597. if (maxlen > 0) {
  598. size_t len = SDL_min(srclen, maxlen - 1);
  599. SDL_memcpy(dst, src, len * sizeof(wchar_t));
  600. dst[len] = '\0';
  601. }
  602. return srclen;
  603. #endif /* HAVE_WCSLCPY */
  604. }
  605. size_t SDL_wcslcat(SDL_INOUT_Z_CAP(maxlen) wchar_t *dst, const wchar_t *src, size_t maxlen)
  606. {
  607. #ifdef HAVE_WCSLCAT
  608. return wcslcat(dst, src, maxlen);
  609. #else
  610. size_t dstlen = SDL_wcslen(dst);
  611. size_t srclen = SDL_wcslen(src);
  612. if (dstlen < maxlen) {
  613. SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen);
  614. }
  615. return dstlen + srclen;
  616. #endif /* HAVE_WCSLCAT */
  617. }
  618. wchar_t *SDL_wcsdup(const wchar_t *string)
  619. {
  620. size_t len = ((SDL_wcslen(string) + 1) * sizeof(wchar_t));
  621. wchar_t *newstr = (wchar_t *)SDL_malloc(len);
  622. if (newstr) {
  623. SDL_memcpy(newstr, string, len);
  624. }
  625. return newstr;
  626. }
  627. wchar_t *SDL_wcsnstr(const wchar_t *haystack, const wchar_t *needle, size_t maxlen)
  628. {
  629. size_t length = SDL_wcslen(needle);
  630. if (length == 0) {
  631. return (wchar_t *)haystack;
  632. }
  633. while (maxlen >= length && *haystack) {
  634. if (maxlen >= length && SDL_wcsncmp(haystack, needle, length) == 0) {
  635. return (wchar_t *)haystack;
  636. }
  637. ++haystack;
  638. --maxlen;
  639. }
  640. return NULL;
  641. }
  642. wchar_t *SDL_wcsstr(const wchar_t *haystack, const wchar_t *needle)
  643. {
  644. #ifdef HAVE_WCSSTR
  645. return SDL_const_cast(wchar_t *, wcsstr(haystack, needle));
  646. #else
  647. return SDL_wcsnstr(haystack, needle, SDL_wcslen(haystack));
  648. #endif /* HAVE_WCSSTR */
  649. }
  650. int SDL_wcscmp(const wchar_t *str1, const wchar_t *str2)
  651. {
  652. #ifdef HAVE_WCSCMP
  653. return wcscmp(str1, str2);
  654. #else
  655. while (*str1 && *str2) {
  656. if (*str1 != *str2) {
  657. break;
  658. }
  659. ++str1;
  660. ++str2;
  661. }
  662. return *str1 - *str2;
  663. #endif /* HAVE_WCSCMP */
  664. }
  665. int SDL_wcsncmp(const wchar_t *str1, const wchar_t *str2, size_t maxlen)
  666. {
  667. #ifdef HAVE_WCSNCMP
  668. return wcsncmp(str1, str2, maxlen);
  669. #else
  670. while (*str1 && *str2 && maxlen) {
  671. if (*str1 != *str2) {
  672. break;
  673. }
  674. ++str1;
  675. ++str2;
  676. --maxlen;
  677. }
  678. if (!maxlen) {
  679. return 0;
  680. }
  681. return *str1 - *str2;
  682. #endif /* HAVE_WCSNCMP */
  683. }
  684. int SDL_wcscasecmp(const wchar_t *wstr1, const wchar_t *wstr2)
  685. {
  686. #if (SDL_SIZEOF_WCHAR_T == 2)
  687. const Uint16 *str1 = (const Uint16 *) wstr1;
  688. const Uint16 *str2 = (const Uint16 *) wstr2;
  689. UNICODE_STRCASECMP(16, 2, 2, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
  690. #elif (SDL_SIZEOF_WCHAR_T == 4)
  691. const Uint32 *str1 = (const Uint32 *) wstr1;
  692. const Uint32 *str2 = (const Uint32 *) wstr2;
  693. UNICODE_STRCASECMP(32, 1, 1, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
  694. #else
  695. #error Unexpected wchar_t size
  696. return -1;
  697. #endif
  698. }
  699. int SDL_wcsncasecmp(const wchar_t *wstr1, const wchar_t *wstr2, size_t maxlen)
  700. {
  701. size_t slen1 = maxlen;
  702. size_t slen2 = maxlen;
  703. #if (SDL_SIZEOF_WCHAR_T == 2)
  704. const Uint16 *str1 = (const Uint16 *) wstr1;
  705. const Uint16 *str2 = (const Uint16 *) wstr2;
  706. UNICODE_STRCASECMP(16, slen1, slen2, slen1 -= (size_t) (str1 - str1start), slen2 -= (size_t) (str2 - str2start));
  707. #elif (SDL_SIZEOF_WCHAR_T == 4)
  708. const Uint32 *str1 = (const Uint32 *) wstr1;
  709. const Uint32 *str2 = (const Uint32 *) wstr2;
  710. UNICODE_STRCASECMP(32, slen1, slen2, slen1 -= (size_t) (str1 - str1start), slen2 -= (size_t) (str2 - str2start));
  711. #else
  712. #error Unexpected wchar_t size
  713. return -1;
  714. #endif
  715. }
  716. long SDL_wcstol(const wchar_t *string, wchar_t **endp, int base)
  717. {
  718. #ifdef HAVE_WCSTOL
  719. return wcstol(string, endp, base);
  720. #else
  721. size_t len;
  722. long value = 0;
  723. if (!base) {
  724. if ((SDL_wcslen(string) > 2) && (SDL_wcsncmp(string, L"0x", 2) == 0)) {
  725. base = 16;
  726. } else {
  727. base = 10;
  728. }
  729. }
  730. len = SDL_ScanLongW(string, 0, base, &value);
  731. if (endp) {
  732. *endp = (wchar_t *)string + len;
  733. }
  734. return value;
  735. #endif /* HAVE_WCSTOL */
  736. }
  737. size_t SDL_strlcpy(SDL_OUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  738. {
  739. #ifdef HAVE_STRLCPY
  740. return strlcpy(dst, src, maxlen);
  741. #else
  742. size_t srclen = SDL_strlen(src);
  743. if (maxlen > 0) {
  744. size_t len = SDL_min(srclen, maxlen - 1);
  745. SDL_memcpy(dst, src, len);
  746. dst[len] = '\0';
  747. }
  748. return srclen;
  749. #endif /* HAVE_STRLCPY */
  750. }
  751. size_t SDL_utf8strlcpy(SDL_OUT_Z_CAP(dst_bytes) char *dst, const char *src, size_t dst_bytes)
  752. {
  753. size_t src_bytes = SDL_strlen(src);
  754. size_t bytes = SDL_min(src_bytes, dst_bytes - 1);
  755. size_t i = 0;
  756. size_t trailing_bytes = 0;
  757. if (bytes) {
  758. unsigned char c = (unsigned char)src[bytes - 1];
  759. if (UTF8_IsLeadByte(c)) {
  760. --bytes;
  761. } else if (UTF8_IsTrailingByte(c)) {
  762. for (i = bytes - 1; i != 0; --i) {
  763. c = (unsigned char)src[i];
  764. trailing_bytes = UTF8_GetTrailingBytes(c);
  765. if (trailing_bytes) {
  766. if ((bytes - i) != (trailing_bytes + 1)) {
  767. bytes = i;
  768. }
  769. break;
  770. }
  771. }
  772. }
  773. SDL_memcpy(dst, src, bytes);
  774. }
  775. dst[bytes] = '\0';
  776. return bytes;
  777. }
  778. size_t SDL_utf8strlen(const char *str)
  779. {
  780. size_t retval = 0;
  781. while (SDL_StepUTF8(&str, NULL)) {
  782. retval++;
  783. }
  784. return retval;
  785. }
  786. size_t SDL_utf8strnlen(const char *str, size_t bytes)
  787. {
  788. size_t retval = 0;
  789. while (SDL_StepUTF8(&str, &bytes)) {
  790. retval++;
  791. }
  792. return retval;
  793. }
  794. size_t SDL_strlcat(SDL_INOUT_Z_CAP(maxlen) char *dst, const char *src, size_t maxlen)
  795. {
  796. #ifdef HAVE_STRLCAT
  797. return strlcat(dst, src, maxlen);
  798. #else
  799. size_t dstlen = SDL_strlen(dst);
  800. size_t srclen = SDL_strlen(src);
  801. if (dstlen < maxlen) {
  802. SDL_strlcpy(dst + dstlen, src, maxlen - dstlen);
  803. }
  804. return dstlen + srclen;
  805. #endif /* HAVE_STRLCAT */
  806. }
  807. char *SDL_strdup(const char *string)
  808. {
  809. size_t len = SDL_strlen(string) + 1;
  810. char *newstr = (char *)SDL_malloc(len);
  811. if (newstr) {
  812. SDL_memcpy(newstr, string, len);
  813. }
  814. return newstr;
  815. }
  816. char *SDL_strndup(const char *string, size_t maxlen)
  817. {
  818. size_t len = SDL_strnlen(string, maxlen);
  819. char *newstr = (char *)SDL_malloc(len + 1);
  820. if (newstr) {
  821. SDL_memcpy(newstr, string, len);
  822. newstr[len] = '\0';
  823. }
  824. return newstr;
  825. }
  826. char *SDL_strrev(char *string)
  827. {
  828. #ifdef HAVE__STRREV
  829. return _strrev(string);
  830. #else
  831. size_t len = SDL_strlen(string);
  832. char *a = &string[0];
  833. char *b = &string[len - 1];
  834. len /= 2;
  835. while (len--) {
  836. const char c = *a; /* NOLINT(clang-analyzer-core.uninitialized.Assign) */
  837. *a++ = *b;
  838. *b-- = c;
  839. }
  840. return string;
  841. #endif /* HAVE__STRREV */
  842. }
  843. char *SDL_strupr(char *string)
  844. {
  845. char *bufp = string;
  846. while (*bufp) {
  847. *bufp = (char)SDL_toupper((unsigned char)*bufp);
  848. ++bufp;
  849. }
  850. return string;
  851. }
  852. char *SDL_strlwr(char *string)
  853. {
  854. char *bufp = string;
  855. while (*bufp) {
  856. *bufp = (char)SDL_tolower((unsigned char)*bufp);
  857. ++bufp;
  858. }
  859. return string;
  860. }
  861. char *SDL_strchr(const char *string, int c)
  862. {
  863. #ifdef HAVE_STRCHR
  864. return SDL_const_cast(char *, strchr(string, c));
  865. #elif defined(HAVE_INDEX)
  866. return SDL_const_cast(char *, index(string, c));
  867. #else
  868. while (*string) {
  869. if (*string == c) {
  870. return (char *)string;
  871. }
  872. ++string;
  873. }
  874. if (c == '\0') {
  875. return (char *)string;
  876. }
  877. return NULL;
  878. #endif /* HAVE_STRCHR */
  879. }
  880. char *SDL_strrchr(const char *string, int c)
  881. {
  882. #ifdef HAVE_STRRCHR
  883. return SDL_const_cast(char *, strrchr(string, c));
  884. #elif defined(HAVE_RINDEX)
  885. return SDL_const_cast(char *, rindex(string, c));
  886. #else
  887. const char *bufp = string + SDL_strlen(string);
  888. while (bufp >= string) {
  889. if (*bufp == c) {
  890. return (char *)bufp;
  891. }
  892. --bufp;
  893. }
  894. return NULL;
  895. #endif /* HAVE_STRRCHR */
  896. }
  897. char *SDL_strnstr(const char *haystack, const char *needle, size_t maxlen)
  898. {
  899. #ifdef HAVE_STRNSTR
  900. return SDL_const_cast(char *, strnstr(haystack, needle, maxlen));
  901. #else
  902. size_t length = SDL_strlen(needle);
  903. if (length == 0) {
  904. return (char *)haystack;
  905. }
  906. while (maxlen >= length && *haystack) {
  907. if (SDL_strncmp(haystack, needle, length) == 0) {
  908. return (char *)haystack;
  909. }
  910. ++haystack;
  911. --maxlen;
  912. }
  913. return NULL;
  914. #endif /* HAVE_STRSTR */
  915. }
  916. char *SDL_strstr(const char *haystack, const char *needle)
  917. {
  918. #ifdef HAVE_STRSTR
  919. return SDL_const_cast(char *, strstr(haystack, needle));
  920. #else
  921. return SDL_strnstr(haystack, needle, SDL_strlen(haystack));
  922. #endif /* HAVE_STRSTR */
  923. }
  924. char *SDL_strcasestr(const char *haystack, const char *needle)
  925. {
  926. const size_t length = SDL_strlen(needle);
  927. do {
  928. if (SDL_strncasecmp(haystack, needle, length) == 0) {
  929. return (char *)haystack;
  930. }
  931. } while (SDL_StepUTF8(&haystack, NULL)); // move ahead by a full codepoint at a time, regardless of bytes.
  932. return NULL;
  933. }
  934. #if !defined(HAVE__LTOA) || !defined(HAVE__I64TOA) || \
  935. !defined(HAVE__ULTOA) || !defined(HAVE__UI64TOA)
  936. static const char ntoa_table[] = {
  937. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  938. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
  939. 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
  940. 'U', 'V', 'W', 'X', 'Y', 'Z'
  941. };
  942. #endif /* ntoa() conversion table */
  943. char *SDL_itoa(int value, char *string, int radix)
  944. {
  945. #ifdef HAVE_ITOA
  946. return itoa(value, string, radix);
  947. #else
  948. return SDL_ltoa((long)value, string, radix);
  949. #endif /* HAVE_ITOA */
  950. }
  951. char *SDL_uitoa(unsigned int value, char *string, int radix)
  952. {
  953. #ifdef HAVE__UITOA
  954. return _uitoa(value, string, radix);
  955. #else
  956. return SDL_ultoa((unsigned long)value, string, radix);
  957. #endif /* HAVE__UITOA */
  958. }
  959. char *SDL_ltoa(long value, char *string, int radix)
  960. {
  961. #ifdef HAVE__LTOA
  962. return _ltoa(value, string, radix);
  963. #else
  964. char *bufp = string;
  965. if (value < 0) {
  966. *bufp++ = '-';
  967. SDL_ultoa(-value, bufp, radix);
  968. } else {
  969. SDL_ultoa(value, bufp, radix);
  970. }
  971. return string;
  972. #endif /* HAVE__LTOA */
  973. }
  974. char *SDL_ultoa(unsigned long value, char *string, int radix)
  975. {
  976. #ifdef HAVE__ULTOA
  977. return _ultoa(value, string, radix);
  978. #else
  979. char *bufp = string;
  980. if (value) {
  981. while (value > 0) {
  982. *bufp++ = ntoa_table[value % radix];
  983. value /= radix;
  984. }
  985. } else {
  986. *bufp++ = '0';
  987. }
  988. *bufp = '\0';
  989. /* The numbers went into the string backwards. :) */
  990. SDL_strrev(string);
  991. return string;
  992. #endif /* HAVE__ULTOA */
  993. }
  994. char *SDL_lltoa(Sint64 value, char *string, int radix)
  995. {
  996. #ifdef HAVE__I64TOA
  997. return _i64toa(value, string, radix);
  998. #else
  999. char *bufp = string;
  1000. if (value < 0) {
  1001. *bufp++ = '-';
  1002. SDL_ulltoa(-value, bufp, radix);
  1003. } else {
  1004. SDL_ulltoa(value, bufp, radix);
  1005. }
  1006. return string;
  1007. #endif /* HAVE__I64TOA */
  1008. }
  1009. char *SDL_ulltoa(Uint64 value, char *string, int radix)
  1010. {
  1011. #ifdef HAVE__UI64TOA
  1012. return _ui64toa(value, string, radix);
  1013. #else
  1014. char *bufp = string;
  1015. if (value) {
  1016. while (value > 0) {
  1017. *bufp++ = ntoa_table[value % radix];
  1018. value /= radix;
  1019. }
  1020. } else {
  1021. *bufp++ = '0';
  1022. }
  1023. *bufp = '\0';
  1024. /* The numbers went into the string backwards. :) */
  1025. SDL_strrev(string);
  1026. return string;
  1027. #endif /* HAVE__UI64TOA */
  1028. }
  1029. int SDL_atoi(const char *string)
  1030. {
  1031. #ifdef HAVE_ATOI
  1032. return atoi(string);
  1033. #else
  1034. return SDL_strtol(string, NULL, 10);
  1035. #endif /* HAVE_ATOI */
  1036. }
  1037. double SDL_atof(const char *string)
  1038. {
  1039. #ifdef HAVE_ATOF
  1040. return atof(string);
  1041. #else
  1042. return SDL_strtod(string, NULL);
  1043. #endif /* HAVE_ATOF */
  1044. }
  1045. long SDL_strtol(const char *string, char **endp, int base)
  1046. {
  1047. #ifdef HAVE_STRTOL
  1048. return strtol(string, endp, base);
  1049. #else
  1050. size_t len;
  1051. long value = 0;
  1052. if (!base) {
  1053. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  1054. base = 16;
  1055. } else {
  1056. base = 10;
  1057. }
  1058. }
  1059. len = SDL_ScanLong(string, 0, base, &value);
  1060. if (endp) {
  1061. *endp = (char *)string + len;
  1062. }
  1063. return value;
  1064. #endif /* HAVE_STRTOL */
  1065. }
  1066. unsigned long
  1067. SDL_strtoul(const char *string, char **endp, int base)
  1068. {
  1069. #ifdef HAVE_STRTOUL
  1070. return strtoul(string, endp, base);
  1071. #else
  1072. size_t len;
  1073. unsigned long value = 0;
  1074. if (!base) {
  1075. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  1076. base = 16;
  1077. } else {
  1078. base = 10;
  1079. }
  1080. }
  1081. len = SDL_ScanUnsignedLong(string, 0, base, &value);
  1082. if (endp) {
  1083. *endp = (char *)string + len;
  1084. }
  1085. return value;
  1086. #endif /* HAVE_STRTOUL */
  1087. }
  1088. Sint64 SDL_strtoll(const char *string, char **endp, int base)
  1089. {
  1090. #ifdef HAVE_STRTOLL
  1091. return strtoll(string, endp, base);
  1092. #else
  1093. size_t len;
  1094. Sint64 value = 0;
  1095. if (!base) {
  1096. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  1097. base = 16;
  1098. } else {
  1099. base = 10;
  1100. }
  1101. }
  1102. len = SDL_ScanLongLong(string, 0, base, &value);
  1103. if (endp) {
  1104. *endp = (char *)string + len;
  1105. }
  1106. return value;
  1107. #endif /* HAVE_STRTOLL */
  1108. }
  1109. Uint64 SDL_strtoull(const char *string, char **endp, int base)
  1110. {
  1111. #ifdef HAVE_STRTOULL
  1112. return strtoull(string, endp, base);
  1113. #else
  1114. size_t len;
  1115. Uint64 value = 0;
  1116. if (!base) {
  1117. if ((SDL_strlen(string) > 2) && (SDL_strncmp(string, "0x", 2) == 0)) {
  1118. base = 16;
  1119. } else {
  1120. base = 10;
  1121. }
  1122. }
  1123. len = SDL_ScanUnsignedLongLong(string, 0, base, &value);
  1124. if (endp) {
  1125. *endp = (char *)string + len;
  1126. }
  1127. return value;
  1128. #endif /* HAVE_STRTOULL */
  1129. }
  1130. double SDL_strtod(const char *string, char **endp)
  1131. {
  1132. #ifdef HAVE_STRTOD
  1133. return strtod(string, endp);
  1134. #else
  1135. size_t len;
  1136. double value = 0.0;
  1137. len = SDL_ScanFloat(string, &value);
  1138. if (endp) {
  1139. *endp = (char *)string + len;
  1140. }
  1141. return value;
  1142. #endif /* HAVE_STRTOD */
  1143. }
  1144. int SDL_strcmp(const char *str1, const char *str2)
  1145. {
  1146. #ifdef HAVE_STRCMP
  1147. return strcmp(str1, str2);
  1148. #else
  1149. int result;
  1150. while (1) {
  1151. result = ((unsigned char)*str1 - (unsigned char)*str2);
  1152. if (result != 0 || (*str1 == '\0' /* && *str2 == '\0'*/)) {
  1153. break;
  1154. }
  1155. ++str1;
  1156. ++str2;
  1157. }
  1158. return result;
  1159. #endif /* HAVE_STRCMP */
  1160. }
  1161. int SDL_strncmp(const char *str1, const char *str2, size_t maxlen)
  1162. {
  1163. #ifdef HAVE_STRNCMP
  1164. return strncmp(str1, str2, maxlen);
  1165. #else
  1166. int result = 0;
  1167. while (maxlen) {
  1168. result = (int)(unsigned char)*str1 - (unsigned char)*str2;
  1169. if (result != 0 || *str1 == '\0' /* && *str2 == '\0'*/) {
  1170. break;
  1171. }
  1172. ++str1;
  1173. ++str2;
  1174. --maxlen;
  1175. }
  1176. return result;
  1177. #endif /* HAVE_STRNCMP */
  1178. }
  1179. int SDL_strcasecmp(const char *str1, const char *str2)
  1180. {
  1181. UNICODE_STRCASECMP(8, 4, 4, (void) str1start, (void) str2start); // always NULL-terminated, no need to adjust lengths.
  1182. }
  1183. int SDL_strncasecmp(const char *str1, const char *str2, size_t maxlen)
  1184. {
  1185. size_t slen1 = maxlen;
  1186. size_t slen2 = maxlen;
  1187. UNICODE_STRCASECMP(8, slen1, slen2, slen1 -= (size_t) (str1 - ((const char *) str1start)), slen2 -= (size_t) (str2 - ((const char *) str2start)));
  1188. }
  1189. int SDL_sscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, ...)
  1190. {
  1191. int rc;
  1192. va_list ap;
  1193. va_start(ap, fmt);
  1194. rc = SDL_vsscanf(text, fmt, ap);
  1195. va_end(ap);
  1196. return rc;
  1197. }
  1198. #ifdef HAVE_VSSCANF
  1199. int SDL_vsscanf(const char *text, const char *fmt, va_list ap)
  1200. {
  1201. return vsscanf(text, fmt, ap);
  1202. }
  1203. #else
  1204. static SDL_bool CharacterMatchesSet(char c, const char *set, size_t set_len)
  1205. {
  1206. SDL_bool invert = SDL_FALSE;
  1207. SDL_bool result = SDL_FALSE;
  1208. if (*set == '^') {
  1209. invert = SDL_TRUE;
  1210. ++set;
  1211. --set_len;
  1212. }
  1213. while (set_len > 0 && !result) {
  1214. if (set_len >= 3 && set[1] == '-') {
  1215. char low_char = SDL_min(set[0], set[2]);
  1216. char high_char = SDL_max(set[0], set[2]);
  1217. if (c >= low_char && c <= high_char) {
  1218. result = SDL_TRUE;
  1219. }
  1220. set += 3;
  1221. set_len -= 3;
  1222. } else {
  1223. if (c == *set) {
  1224. result = SDL_TRUE;
  1225. }
  1226. ++set;
  1227. --set_len;
  1228. }
  1229. }
  1230. if (invert) {
  1231. result = !result;
  1232. }
  1233. return result;
  1234. }
  1235. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1236. int SDL_vsscanf(const char *text, SDL_SCANF_FORMAT_STRING const char *fmt, va_list ap)
  1237. {
  1238. int retval = 0;
  1239. if (!text || !*text) {
  1240. return -1;
  1241. }
  1242. while (*fmt) {
  1243. if (*fmt == ' ') {
  1244. while (SDL_isspace((unsigned char)*text)) {
  1245. ++text;
  1246. }
  1247. ++fmt;
  1248. continue;
  1249. }
  1250. if (*fmt == '%') {
  1251. SDL_bool done = SDL_FALSE;
  1252. long count = 0;
  1253. int radix = 10;
  1254. enum
  1255. {
  1256. DO_SHORT,
  1257. DO_INT,
  1258. DO_LONG,
  1259. DO_LONGLONG,
  1260. DO_SIZE_T
  1261. } inttype = DO_INT;
  1262. size_t advance;
  1263. SDL_bool suppress = SDL_FALSE;
  1264. ++fmt;
  1265. if (*fmt == '%') {
  1266. if (*text == '%') {
  1267. ++text;
  1268. ++fmt;
  1269. continue;
  1270. }
  1271. break;
  1272. }
  1273. if (*fmt == '*') {
  1274. suppress = SDL_TRUE;
  1275. ++fmt;
  1276. }
  1277. fmt += SDL_ScanLong(fmt, 0, 10, &count);
  1278. if (*fmt == 'c') {
  1279. if (!count) {
  1280. count = 1;
  1281. }
  1282. if (suppress) {
  1283. while (count--) {
  1284. ++text;
  1285. }
  1286. } else {
  1287. char *valuep = va_arg(ap, char *);
  1288. while (count--) {
  1289. *valuep++ = *text++;
  1290. }
  1291. ++retval;
  1292. }
  1293. continue;
  1294. }
  1295. while (SDL_isspace((unsigned char)*text)) {
  1296. ++text;
  1297. }
  1298. /* FIXME: implement more of the format specifiers */
  1299. while (!done) {
  1300. switch (*fmt) {
  1301. case '*':
  1302. suppress = SDL_TRUE;
  1303. break;
  1304. case 'h':
  1305. if (inttype == DO_INT) {
  1306. inttype = DO_SHORT;
  1307. } else if (inttype > DO_SHORT) {
  1308. ++inttype;
  1309. }
  1310. break;
  1311. case 'l':
  1312. if (inttype < DO_LONGLONG) {
  1313. ++inttype;
  1314. }
  1315. break;
  1316. case 'I':
  1317. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1318. fmt += 2;
  1319. inttype = DO_LONGLONG;
  1320. }
  1321. break;
  1322. case 'z':
  1323. inttype = DO_SIZE_T;
  1324. break;
  1325. case 'i':
  1326. {
  1327. int index = 0;
  1328. if (text[index] == '-') {
  1329. ++index;
  1330. }
  1331. if (text[index] == '0') {
  1332. if (SDL_tolower((unsigned char)text[index + 1]) == 'x') {
  1333. radix = 16;
  1334. } else {
  1335. radix = 8;
  1336. }
  1337. }
  1338. }
  1339. SDL_FALLTHROUGH;
  1340. case 'd':
  1341. if (inttype == DO_LONGLONG) {
  1342. Sint64 value = 0;
  1343. advance = SDL_ScanLongLong(text, count, radix, &value);
  1344. text += advance;
  1345. if (advance && !suppress) {
  1346. Sint64 *valuep = va_arg(ap, Sint64 *);
  1347. *valuep = value;
  1348. ++retval;
  1349. }
  1350. } else if (inttype == DO_SIZE_T) {
  1351. Sint64 value = 0;
  1352. advance = SDL_ScanLongLong(text, count, radix, &value);
  1353. text += advance;
  1354. if (advance && !suppress) {
  1355. size_t *valuep = va_arg(ap, size_t *);
  1356. *valuep = (size_t)value;
  1357. ++retval;
  1358. }
  1359. } else {
  1360. long value = 0;
  1361. advance = SDL_ScanLong(text, count, radix, &value);
  1362. text += advance;
  1363. if (advance && !suppress) {
  1364. switch (inttype) {
  1365. case DO_SHORT:
  1366. {
  1367. short *valuep = va_arg(ap, short *);
  1368. *valuep = (short)value;
  1369. } break;
  1370. case DO_INT:
  1371. {
  1372. int *valuep = va_arg(ap, int *);
  1373. *valuep = (int)value;
  1374. } break;
  1375. case DO_LONG:
  1376. {
  1377. long *valuep = va_arg(ap, long *);
  1378. *valuep = value;
  1379. } break;
  1380. case DO_LONGLONG:
  1381. case DO_SIZE_T:
  1382. /* Handled above */
  1383. break;
  1384. }
  1385. ++retval;
  1386. }
  1387. }
  1388. done = SDL_TRUE;
  1389. break;
  1390. case 'o':
  1391. if (radix == 10) {
  1392. radix = 8;
  1393. }
  1394. SDL_FALLTHROUGH;
  1395. case 'x':
  1396. case 'X':
  1397. if (radix == 10) {
  1398. radix = 16;
  1399. }
  1400. SDL_FALLTHROUGH;
  1401. case 'u':
  1402. if (inttype == DO_LONGLONG) {
  1403. Uint64 value = 0;
  1404. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1405. text += advance;
  1406. if (advance && !suppress) {
  1407. Uint64 *valuep = va_arg(ap, Uint64 *);
  1408. *valuep = value;
  1409. ++retval;
  1410. }
  1411. } else if (inttype == DO_SIZE_T) {
  1412. Uint64 value = 0;
  1413. advance = SDL_ScanUnsignedLongLong(text, count, radix, &value);
  1414. text += advance;
  1415. if (advance && !suppress) {
  1416. size_t *valuep = va_arg(ap, size_t *);
  1417. *valuep = (size_t)value;
  1418. ++retval;
  1419. }
  1420. } else {
  1421. unsigned long value = 0;
  1422. advance = SDL_ScanUnsignedLong(text, count, radix, &value);
  1423. text += advance;
  1424. if (advance && !suppress) {
  1425. switch (inttype) {
  1426. case DO_SHORT:
  1427. {
  1428. short *valuep = va_arg(ap, short *);
  1429. *valuep = (short)value;
  1430. } break;
  1431. case DO_INT:
  1432. {
  1433. int *valuep = va_arg(ap, int *);
  1434. *valuep = (int)value;
  1435. } break;
  1436. case DO_LONG:
  1437. {
  1438. long *valuep = va_arg(ap, long *);
  1439. *valuep = value;
  1440. } break;
  1441. case DO_LONGLONG:
  1442. case DO_SIZE_T:
  1443. /* Handled above */
  1444. break;
  1445. }
  1446. ++retval;
  1447. }
  1448. }
  1449. done = SDL_TRUE;
  1450. break;
  1451. case 'p':
  1452. {
  1453. uintptr_t value = 0;
  1454. advance = SDL_ScanUintPtrT(text, 16, &value);
  1455. text += advance;
  1456. if (advance && !suppress) {
  1457. void **valuep = va_arg(ap, void **);
  1458. *valuep = (void *)value;
  1459. ++retval;
  1460. }
  1461. }
  1462. done = SDL_TRUE;
  1463. break;
  1464. case 'f':
  1465. {
  1466. double value = 0.0;
  1467. advance = SDL_ScanFloat(text, &value);
  1468. text += advance;
  1469. if (advance && !suppress) {
  1470. float *valuep = va_arg(ap, float *);
  1471. *valuep = (float)value;
  1472. ++retval;
  1473. }
  1474. }
  1475. done = SDL_TRUE;
  1476. break;
  1477. case 's':
  1478. if (suppress) {
  1479. while (!SDL_isspace((unsigned char)*text)) {
  1480. ++text;
  1481. if (count) {
  1482. if (--count == 0) {
  1483. break;
  1484. }
  1485. }
  1486. }
  1487. } else {
  1488. char *valuep = va_arg(ap, char *);
  1489. while (!SDL_isspace((unsigned char)*text)) {
  1490. *valuep++ = *text++;
  1491. if (count) {
  1492. if (--count == 0) {
  1493. break;
  1494. }
  1495. }
  1496. }
  1497. *valuep = '\0';
  1498. ++retval;
  1499. }
  1500. done = SDL_TRUE;
  1501. break;
  1502. case '[':
  1503. {
  1504. const char *set = fmt + 1;
  1505. while (*fmt && *fmt != ']') {
  1506. ++fmt;
  1507. }
  1508. if (*fmt) {
  1509. size_t set_len = (fmt - set);
  1510. if (suppress) {
  1511. while (CharacterMatchesSet(*text, set, set_len)) {
  1512. ++text;
  1513. if (count) {
  1514. if (--count == 0) {
  1515. break;
  1516. }
  1517. }
  1518. }
  1519. } else {
  1520. SDL_bool had_match = SDL_FALSE;
  1521. char *valuep = va_arg(ap, char *);
  1522. while (CharacterMatchesSet(*text, set, set_len)) {
  1523. had_match = SDL_TRUE;
  1524. *valuep++ = *text++;
  1525. if (count) {
  1526. if (--count == 0) {
  1527. break;
  1528. }
  1529. }
  1530. }
  1531. *valuep = '\0';
  1532. if (had_match) {
  1533. ++retval;
  1534. }
  1535. }
  1536. }
  1537. }
  1538. done = SDL_TRUE;
  1539. break;
  1540. default:
  1541. done = SDL_TRUE;
  1542. break;
  1543. }
  1544. ++fmt;
  1545. }
  1546. continue;
  1547. }
  1548. if (*text == *fmt) {
  1549. ++text;
  1550. ++fmt;
  1551. continue;
  1552. }
  1553. /* Text didn't match format specifier */
  1554. break;
  1555. }
  1556. return retval;
  1557. }
  1558. #endif /* HAVE_VSSCANF */
  1559. int SDL_snprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  1560. {
  1561. va_list ap;
  1562. int retval;
  1563. va_start(ap, fmt);
  1564. retval = SDL_vsnprintf(text, maxlen, fmt, ap);
  1565. va_end(ap);
  1566. return retval;
  1567. }
  1568. int SDL_swprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const wchar_t *fmt, ...)
  1569. {
  1570. va_list ap;
  1571. int retval;
  1572. va_start(ap, fmt);
  1573. retval = SDL_vswprintf(text, maxlen, fmt, ap);
  1574. va_end(ap);
  1575. return retval;
  1576. }
  1577. #if defined(HAVE_LIBC) && defined(__WATCOMC__)
  1578. /* _vsnprintf() doesn't ensure nul termination */
  1579. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1580. {
  1581. int retval;
  1582. if (!fmt) {
  1583. fmt = "";
  1584. }
  1585. retval = _vsnprintf(text, maxlen, fmt, ap);
  1586. if (maxlen > 0) {
  1587. text[maxlen - 1] = '\0';
  1588. }
  1589. if (retval < 0) {
  1590. retval = (int)maxlen;
  1591. }
  1592. return retval;
  1593. }
  1594. #elif defined(HAVE_VSNPRINTF)
  1595. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, const char *fmt, va_list ap)
  1596. {
  1597. if (!fmt) {
  1598. fmt = "";
  1599. }
  1600. return vsnprintf(text, maxlen, fmt, ap);
  1601. }
  1602. #else
  1603. #define TEXT_AND_LEN_ARGS (length < maxlen) ? &text[length] : NULL, (length < maxlen) ? (maxlen - length) : 0
  1604. /* FIXME: implement more of the format specifiers */
  1605. typedef enum
  1606. {
  1607. SDL_CASE_NOCHANGE,
  1608. SDL_CASE_LOWER,
  1609. SDL_CASE_UPPER
  1610. } SDL_letter_case;
  1611. typedef struct
  1612. {
  1613. SDL_bool left_justify; /* for now: ignored. */
  1614. SDL_bool force_sign;
  1615. SDL_bool force_type; /* for now: used only by float printer, ignored otherwise. */
  1616. SDL_bool pad_zeroes;
  1617. SDL_letter_case force_case;
  1618. int width;
  1619. int radix;
  1620. int precision;
  1621. } SDL_FormatInfo;
  1622. static size_t SDL_PrintString(char *text, size_t maxlen, SDL_FormatInfo *info, const char *string)
  1623. {
  1624. size_t length = 0;
  1625. size_t slen, sz;
  1626. if (!string) {
  1627. string = "(null)";
  1628. }
  1629. sz = SDL_strlen(string);
  1630. if (info && info->width > 0 && (size_t)info->width > sz) {
  1631. const char fill = info->pad_zeroes ? '0' : ' ';
  1632. size_t width = info->width - sz;
  1633. size_t filllen;
  1634. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1635. width += sz - (size_t)info->precision;
  1636. }
  1637. filllen = SDL_min(width, maxlen);
  1638. SDL_memset(text, fill, filllen);
  1639. text += filllen;
  1640. maxlen -= filllen;
  1641. length += width;
  1642. }
  1643. SDL_strlcpy(text, string, maxlen);
  1644. length += sz;
  1645. if (info) {
  1646. if (info->precision >= 0 && (size_t)info->precision < sz) {
  1647. slen = (size_t)info->precision;
  1648. if (slen < maxlen) {
  1649. text[slen] = '\0';
  1650. }
  1651. length -= (sz - slen);
  1652. }
  1653. if (maxlen > 1) {
  1654. if (info->force_case == SDL_CASE_LOWER) {
  1655. SDL_strlwr(text);
  1656. } else if (info->force_case == SDL_CASE_UPPER) {
  1657. SDL_strupr(text);
  1658. }
  1659. }
  1660. }
  1661. return length;
  1662. }
  1663. static size_t SDL_PrintStringW(char *text, size_t maxlen, SDL_FormatInfo *info, const wchar_t *wide_string)
  1664. {
  1665. size_t length = 0;
  1666. if (wide_string) {
  1667. char *string = SDL_iconv_string("UTF-8", "WCHAR_T", (char *)(wide_string), (SDL_wcslen(wide_string) + 1) * sizeof(*wide_string));
  1668. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, string);
  1669. SDL_free(string);
  1670. } else {
  1671. length = SDL_PrintString(TEXT_AND_LEN_ARGS, info, NULL);
  1672. }
  1673. return length;
  1674. }
  1675. static void SDL_IntPrecisionAdjust(char *num, size_t maxlen, SDL_FormatInfo *info)
  1676. { /* left-pad num with zeroes. */
  1677. size_t sz, pad, have_sign;
  1678. if (!info) {
  1679. return;
  1680. }
  1681. have_sign = 0;
  1682. if (*num == '-' || *num == '+') {
  1683. have_sign = 1;
  1684. ++num;
  1685. --maxlen;
  1686. }
  1687. sz = SDL_strlen(num);
  1688. if (info->precision > 0 && sz < (size_t)info->precision) {
  1689. pad = (size_t)info->precision - sz;
  1690. if (pad + sz + 1 <= maxlen) { /* otherwise ignore the precision */
  1691. SDL_memmove(num + pad, num, sz + 1);
  1692. SDL_memset(num, '0', pad);
  1693. }
  1694. }
  1695. info->precision = -1; /* so that SDL_PrintString() doesn't make a mess. */
  1696. if (info->pad_zeroes && info->width > 0 && (size_t)info->width > sz + have_sign) {
  1697. /* handle here: spaces are added before the sign
  1698. but zeroes must be placed _after_ the sign. */
  1699. /* sz hasn't changed: we ignore pad_zeroes if a precision is given. */
  1700. pad = (size_t)info->width - sz - have_sign;
  1701. if (pad + sz + 1 <= maxlen) {
  1702. SDL_memmove(num + pad, num, sz + 1);
  1703. SDL_memset(num, '0', pad);
  1704. }
  1705. info->width = 0; /* so that SDL_PrintString() doesn't make a mess. */
  1706. }
  1707. }
  1708. static size_t SDL_PrintLong(char *text, size_t maxlen, SDL_FormatInfo *info, long value)
  1709. {
  1710. char num[130], *p = num;
  1711. if (info->force_sign && value >= 0L) {
  1712. *p++ = '+';
  1713. }
  1714. SDL_ltoa(value, p, info ? info->radix : 10);
  1715. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1716. return SDL_PrintString(text, maxlen, info, num);
  1717. }
  1718. static size_t SDL_PrintUnsignedLong(char *text, size_t maxlen, SDL_FormatInfo *info, unsigned long value)
  1719. {
  1720. char num[130];
  1721. SDL_ultoa(value, num, info ? info->radix : 10);
  1722. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1723. return SDL_PrintString(text, maxlen, info, num);
  1724. }
  1725. static size_t SDL_PrintLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Sint64 value)
  1726. {
  1727. char num[130], *p = num;
  1728. if (info->force_sign && value >= (Sint64)0) {
  1729. *p++ = '+';
  1730. }
  1731. SDL_lltoa(value, p, info ? info->radix : 10);
  1732. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1733. return SDL_PrintString(text, maxlen, info, num);
  1734. }
  1735. static size_t SDL_PrintUnsignedLongLong(char *text, size_t maxlen, SDL_FormatInfo *info, Uint64 value)
  1736. {
  1737. char num[130];
  1738. SDL_ulltoa(value, num, info ? info->radix : 10);
  1739. SDL_IntPrecisionAdjust(num, sizeof(num), info);
  1740. return SDL_PrintString(text, maxlen, info, num);
  1741. }
  1742. static size_t SDL_PrintFloat(char *text, size_t maxlen, SDL_FormatInfo *info, double arg, SDL_bool g)
  1743. {
  1744. char num[327];
  1745. size_t length = 0;
  1746. size_t integer_length;
  1747. int precision = info->precision;
  1748. /* This isn't especially accurate, but hey, it's easy. :) */
  1749. Uint64 value;
  1750. if (arg < 0) {
  1751. num[length++] = '-';
  1752. arg = -arg;
  1753. } else if (info->force_sign) {
  1754. num[length++] = '+';
  1755. }
  1756. value = (Uint64)arg;
  1757. integer_length = SDL_PrintUnsignedLongLong(&num[length], sizeof(num) - length, NULL, value);
  1758. length += integer_length;
  1759. arg -= value;
  1760. if (precision < 0) {
  1761. precision = 6;
  1762. }
  1763. if (g) {
  1764. /* The precision includes the integer portion */
  1765. precision -= SDL_min((int)integer_length, precision);
  1766. }
  1767. if (info->force_type || precision > 0) {
  1768. const char decimal_separator = '.';
  1769. double integer_value;
  1770. SDL_assert(length < sizeof(num));
  1771. num[length++] = decimal_separator;
  1772. while (precision > 1) {
  1773. arg *= 10.0;
  1774. arg = SDL_modf(arg, &integer_value);
  1775. SDL_assert(length < sizeof(num));
  1776. num[length++] = '0' + (char)integer_value;
  1777. --precision;
  1778. }
  1779. if (precision == 1) {
  1780. arg *= 10.0;
  1781. integer_value = SDL_round(arg);
  1782. if (integer_value == 10.0) {
  1783. /* Carry the one... */
  1784. size_t i;
  1785. for (i = length; i--; ) {
  1786. if (num[i] == decimal_separator) {
  1787. continue;
  1788. }
  1789. if (num[i] == '9') {
  1790. num[i] = '0';
  1791. if (i == 0 || num[i - 1] == '-' || num[i - 1] == '+') {
  1792. SDL_memmove(&num[i+1], &num[i], length - i);
  1793. num[i] = '1';
  1794. ++length;
  1795. break;
  1796. }
  1797. } else {
  1798. ++num[i];
  1799. break;
  1800. }
  1801. }
  1802. SDL_assert(length < sizeof(num));
  1803. num[length++] = '0';
  1804. } else {
  1805. SDL_assert(length < sizeof(num));
  1806. num[length++] = '0' + (char)integer_value;
  1807. }
  1808. }
  1809. if (g) {
  1810. /* Trim trailing zeroes and decimal separator */
  1811. size_t i;
  1812. for (i = length - 1; num[i] != decimal_separator; --i) {
  1813. if (num[i] == '0') {
  1814. --length;
  1815. } else {
  1816. break;
  1817. }
  1818. }
  1819. if (num[i] == decimal_separator) {
  1820. --length;
  1821. }
  1822. }
  1823. }
  1824. num[length] = '\0';
  1825. info->precision = -1;
  1826. length = SDL_PrintString(text, maxlen, info, num);
  1827. if (info->width > 0 && (size_t)info->width > length) {
  1828. const char fill = info->pad_zeroes ? '0' : ' ';
  1829. size_t width = info->width - length;
  1830. size_t filllen, movelen;
  1831. filllen = SDL_min(width, maxlen);
  1832. movelen = SDL_min(length, (maxlen - filllen));
  1833. SDL_memmove(&text[filllen], text, movelen);
  1834. SDL_memset(text, fill, filllen);
  1835. length += width;
  1836. }
  1837. return length;
  1838. }
  1839. static size_t SDL_PrintPointer(char *text, size_t maxlen, SDL_FormatInfo *info, const void *value)
  1840. {
  1841. char num[130];
  1842. size_t length;
  1843. if (!value) {
  1844. return SDL_PrintString(text, maxlen, info, NULL);
  1845. }
  1846. SDL_ulltoa((unsigned long long)(uintptr_t)value, num, 16);
  1847. length = SDL_PrintString(text, maxlen, info, "0x");
  1848. return length + SDL_PrintString(TEXT_AND_LEN_ARGS, info, num);
  1849. }
  1850. /* NOLINTNEXTLINE(readability-non-const-parameter) */
  1851. int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  1852. {
  1853. size_t length = 0;
  1854. if (!text) {
  1855. maxlen = 0;
  1856. }
  1857. if (!fmt) {
  1858. fmt = "";
  1859. }
  1860. while (*fmt) {
  1861. if (*fmt == '%') {
  1862. SDL_bool done = SDL_FALSE;
  1863. SDL_bool check_flag;
  1864. SDL_FormatInfo info;
  1865. enum
  1866. {
  1867. DO_INT,
  1868. DO_LONG,
  1869. DO_LONGLONG,
  1870. DO_SIZE_T
  1871. } inttype = DO_INT;
  1872. SDL_zero(info);
  1873. info.radix = 10;
  1874. info.precision = -1;
  1875. check_flag = SDL_TRUE;
  1876. while (check_flag) {
  1877. ++fmt;
  1878. switch (*fmt) {
  1879. case '-':
  1880. info.left_justify = SDL_TRUE;
  1881. break;
  1882. case '+':
  1883. info.force_sign = SDL_TRUE;
  1884. break;
  1885. case '#':
  1886. info.force_type = SDL_TRUE;
  1887. break;
  1888. case '0':
  1889. info.pad_zeroes = SDL_TRUE;
  1890. break;
  1891. default:
  1892. check_flag = SDL_FALSE;
  1893. break;
  1894. }
  1895. }
  1896. if (*fmt >= '0' && *fmt <= '9') {
  1897. info.width = SDL_strtol(fmt, (char **)&fmt, 0);
  1898. } else if (*fmt == '*') {
  1899. ++fmt;
  1900. info.width = va_arg(ap, int);
  1901. }
  1902. if (*fmt == '.') {
  1903. ++fmt;
  1904. if (*fmt >= '0' && *fmt <= '9') {
  1905. info.precision = SDL_strtol(fmt, (char **)&fmt, 0);
  1906. } else if (*fmt == '*') {
  1907. ++fmt;
  1908. info.precision = va_arg(ap, int);
  1909. } else {
  1910. info.precision = 0;
  1911. }
  1912. if (info.precision < 0) {
  1913. info.precision = 0;
  1914. }
  1915. }
  1916. while (!done) {
  1917. switch (*fmt) {
  1918. case '%':
  1919. if (length < maxlen) {
  1920. text[length] = '%';
  1921. }
  1922. ++length;
  1923. done = SDL_TRUE;
  1924. break;
  1925. case 'c':
  1926. /* char is promoted to int when passed through (...) */
  1927. if (length < maxlen) {
  1928. text[length] = (char)va_arg(ap, int);
  1929. }
  1930. ++length;
  1931. done = SDL_TRUE;
  1932. break;
  1933. case 'h':
  1934. /* short is promoted to int when passed through (...) */
  1935. break;
  1936. case 'l':
  1937. if (inttype < DO_LONGLONG) {
  1938. ++inttype;
  1939. }
  1940. break;
  1941. case 'I':
  1942. if (SDL_strncmp(fmt, "I64", 3) == 0) {
  1943. fmt += 2;
  1944. inttype = DO_LONGLONG;
  1945. }
  1946. break;
  1947. case 'z':
  1948. inttype = DO_SIZE_T;
  1949. break;
  1950. case 'i':
  1951. case 'd':
  1952. if (info.precision >= 0) {
  1953. info.pad_zeroes = SDL_FALSE;
  1954. }
  1955. switch (inttype) {
  1956. case DO_INT:
  1957. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1958. (long)va_arg(ap, int));
  1959. break;
  1960. case DO_LONG:
  1961. length += SDL_PrintLong(TEXT_AND_LEN_ARGS, &info,
  1962. va_arg(ap, long));
  1963. break;
  1964. case DO_LONGLONG:
  1965. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1966. va_arg(ap, Sint64));
  1967. break;
  1968. case DO_SIZE_T:
  1969. length += SDL_PrintLongLong(TEXT_AND_LEN_ARGS, &info,
  1970. va_arg(ap, size_t));
  1971. break;
  1972. }
  1973. done = SDL_TRUE;
  1974. break;
  1975. case 'p':
  1976. info.force_case = SDL_CASE_LOWER;
  1977. length += SDL_PrintPointer(TEXT_AND_LEN_ARGS, &info, va_arg(ap, void *));
  1978. done = SDL_TRUE;
  1979. break;
  1980. case 'x':
  1981. info.force_case = SDL_CASE_LOWER;
  1982. SDL_FALLTHROUGH;
  1983. case 'X':
  1984. if (info.force_case == SDL_CASE_NOCHANGE) {
  1985. info.force_case = SDL_CASE_UPPER;
  1986. }
  1987. if (info.radix == 10) {
  1988. info.radix = 16;
  1989. }
  1990. SDL_FALLTHROUGH;
  1991. case 'o':
  1992. if (info.radix == 10) {
  1993. info.radix = 8;
  1994. }
  1995. SDL_FALLTHROUGH;
  1996. case 'u':
  1997. info.force_sign = SDL_FALSE;
  1998. if (info.precision >= 0) {
  1999. info.pad_zeroes = SDL_FALSE;
  2000. }
  2001. switch (inttype) {
  2002. case DO_INT:
  2003. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  2004. (unsigned long)
  2005. va_arg(ap, unsigned int));
  2006. break;
  2007. case DO_LONG:
  2008. length += SDL_PrintUnsignedLong(TEXT_AND_LEN_ARGS, &info,
  2009. va_arg(ap, unsigned long));
  2010. break;
  2011. case DO_LONGLONG:
  2012. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  2013. va_arg(ap, Uint64));
  2014. break;
  2015. case DO_SIZE_T:
  2016. length += SDL_PrintUnsignedLongLong(TEXT_AND_LEN_ARGS, &info,
  2017. va_arg(ap, size_t));
  2018. break;
  2019. }
  2020. done = SDL_TRUE;
  2021. break;
  2022. case 'f':
  2023. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), SDL_FALSE);
  2024. done = SDL_TRUE;
  2025. break;
  2026. case 'g':
  2027. length += SDL_PrintFloat(TEXT_AND_LEN_ARGS, &info, va_arg(ap, double), SDL_TRUE);
  2028. done = SDL_TRUE;
  2029. break;
  2030. case 'S':
  2031. info.pad_zeroes = SDL_FALSE;
  2032. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  2033. done = SDL_TRUE;
  2034. break;
  2035. case 's':
  2036. info.pad_zeroes = SDL_FALSE;
  2037. if (inttype > DO_INT) {
  2038. length += SDL_PrintStringW(TEXT_AND_LEN_ARGS, &info, va_arg(ap, wchar_t *));
  2039. } else {
  2040. length += SDL_PrintString(TEXT_AND_LEN_ARGS, &info, va_arg(ap, char *));
  2041. }
  2042. done = SDL_TRUE;
  2043. break;
  2044. default:
  2045. done = SDL_TRUE;
  2046. break;
  2047. }
  2048. ++fmt;
  2049. }
  2050. } else {
  2051. if (length < maxlen) {
  2052. text[length] = *fmt;
  2053. }
  2054. ++fmt;
  2055. ++length;
  2056. }
  2057. }
  2058. if (length < maxlen) {
  2059. text[length] = '\0';
  2060. } else if (maxlen > 0) {
  2061. text[maxlen - 1] = '\0';
  2062. }
  2063. return (int)length;
  2064. }
  2065. #undef TEXT_AND_LEN_ARGS
  2066. #endif /* HAVE_VSNPRINTF */
  2067. int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap)
  2068. {
  2069. char *text_utf8 = NULL, *fmt_utf8 = NULL;
  2070. int retval;
  2071. if (fmt) {
  2072. fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
  2073. if (!fmt_utf8) {
  2074. return -1;
  2075. }
  2076. }
  2077. if (!maxlen) {
  2078. /* We still need to generate the text to find the final text length */
  2079. maxlen = 1024;
  2080. }
  2081. text_utf8 = (char *)SDL_malloc(maxlen * 4);
  2082. if (!text_utf8) {
  2083. SDL_free(fmt_utf8);
  2084. return -1;
  2085. }
  2086. retval = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap);
  2087. if (retval >= 0) {
  2088. wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1);
  2089. if (text_wchar) {
  2090. if (text) {
  2091. SDL_wcslcpy(text, text_wchar, maxlen);
  2092. }
  2093. retval = (int)SDL_wcslen(text_wchar);
  2094. SDL_free(text_wchar);
  2095. } else {
  2096. retval = -1;
  2097. }
  2098. }
  2099. SDL_free(text_utf8);
  2100. SDL_free(fmt_utf8);
  2101. return retval;
  2102. }
  2103. int SDL_asprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
  2104. {
  2105. va_list ap;
  2106. int retval;
  2107. va_start(ap, fmt);
  2108. retval = SDL_vasprintf(strp, fmt, ap);
  2109. va_end(ap);
  2110. return retval;
  2111. }
  2112. int SDL_vasprintf(char **strp, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
  2113. {
  2114. int retval;
  2115. int size = 100; /* Guess we need no more than 100 bytes */
  2116. char *p, *np;
  2117. va_list aq;
  2118. *strp = NULL;
  2119. p = (char *)SDL_malloc(size);
  2120. if (!p) {
  2121. return -1;
  2122. }
  2123. while (1) {
  2124. /* Try to print in the allocated space */
  2125. va_copy(aq, ap);
  2126. retval = SDL_vsnprintf(p, size, fmt, aq);
  2127. va_end(aq);
  2128. /* Check error code */
  2129. if (retval < 0) {
  2130. SDL_free(p);
  2131. return retval;
  2132. }
  2133. /* If that worked, return the string */
  2134. if (retval < size) {
  2135. *strp = p;
  2136. return retval;
  2137. }
  2138. /* Else try again with more space */
  2139. size = retval + 1; /* Precisely what is needed */
  2140. np = (char *)SDL_realloc(p, size);
  2141. if (!np) {
  2142. SDL_free(p);
  2143. return -1;
  2144. } else {
  2145. p = np;
  2146. }
  2147. }
  2148. }