SDL_string.c 73 KB

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