SDL_string.c 68 KB

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