SDL_string.c 71 KB

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