SDL_string.c 70 KB

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