stringFunctions.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include "core/strings/stringFunctions.h"
  25. #include "platform/platform.h"
  26. #if defined(TORQUE_OS_WIN)
  27. // This standard function is not defined when compiling with VC7...
  28. #define vsnprintf _vsnprintf
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. // Original code from: http://sourcefrog.net/projects/natsort/
  32. // Somewhat altered here.
  33. //TODO: proper UTF8 support; currently only working for single-byte characters
  34. /* -*- mode: c; c-file-style: "k&r" -*-
  35. strnatcmp.c -- Perform 'natural order' comparisons of strings in C.
  36. Copyright (C) 2000, 2004 by Martin Pool <mbp sourcefrog net>
  37. This software is provided 'as-is', without any express or implied
  38. warranty. In no event will the authors be held liable for any damages
  39. arising from the use of this software.
  40. Permission is granted to anyone to use this software for any purpose,
  41. including commercial applications, and to alter it and redistribute it
  42. freely, subject to the following restrictions:
  43. 1. The origin of this software must not be misrepresented; you must not
  44. claim that you wrote the original software. If you use this software
  45. in a product, an acknowledgment in the product documentation would be
  46. appreciated but is not required.
  47. 2. Altered source versions must be plainly marked as such, and must not be
  48. misrepresented as being the original software.
  49. 3. This notice may not be removed or altered from any source distribution.
  50. */
  51. /* partial change history:
  52. *
  53. * 2004-10-10 mbp: Lift out character type dependencies into macros.
  54. *
  55. * Eric Sosman pointed out that ctype functions take a parameter whose
  56. * value must be that of an unsigned int, even on platforms that have
  57. * negative chars in their default char type.
  58. */
  59. typedef char nat_char;
  60. /* These are defined as macros to make it easier to adapt this code to
  61. * different characters types or comparison functions. */
  62. static inline int
  63. nat_isdigit( nat_char a )
  64. {
  65. return dIsdigit( a );
  66. }
  67. static inline int
  68. nat_isspace( nat_char a )
  69. {
  70. return dIsspace( a );
  71. }
  72. static inline nat_char
  73. nat_toupper( nat_char a )
  74. {
  75. return dToupper( a );
  76. }
  77. static S32
  78. compare_right(const nat_char* a, const nat_char* b)
  79. {
  80. S32 bias = 0;
  81. /* The longest run of digits wins. That aside, the greatest
  82. value wins, but we can't know that it will until we've scanned
  83. both numbers to know that they have the same magnitude, so we
  84. remember it in BIAS. */
  85. for (;; a++, b++) {
  86. if (!nat_isdigit(*a) && !nat_isdigit(*b))
  87. break;
  88. else if (!nat_isdigit(*a))
  89. return -1;
  90. else if (!nat_isdigit(*b))
  91. return +1;
  92. else if (*a < *b) {
  93. if (!bias)
  94. bias = -1;
  95. } else if (*a > *b) {
  96. if (!bias)
  97. bias = +1;
  98. } else if (!*a && !*b)
  99. return bias;
  100. }
  101. return bias;
  102. }
  103. static int
  104. compare_left(const nat_char* a, const nat_char* b)
  105. {
  106. /* Compare two left-aligned numbers: the first to have a
  107. different value wins. */
  108. for (;; a++, b++) {
  109. if (!nat_isdigit(*a) && !nat_isdigit(*b))
  110. break;
  111. else if (!nat_isdigit(*a))
  112. return -1;
  113. else if (!nat_isdigit(*b))
  114. return +1;
  115. else if (*a < *b)
  116. return -1;
  117. else if (*a > *b)
  118. return +1;
  119. }
  120. return 0;
  121. }
  122. static S32 strnatcmp0(const nat_char* a, const nat_char* b, S32 fold_case)
  123. {
  124. S32 ai, bi;
  125. nat_char ca, cb;
  126. S32 fractional, result;
  127. ai = bi = 0;
  128. while (1) {
  129. ca = a[ai]; cb = b[bi];
  130. /* skip over leading spaces or zeros */
  131. while (nat_isspace(ca))
  132. ca = a[++ai];
  133. while (nat_isspace(cb))
  134. cb = b[++bi];
  135. /* process run of digits */
  136. if (nat_isdigit(ca) && nat_isdigit(cb)) {
  137. fractional = (ca == '0' || cb == '0');
  138. if (fractional) {
  139. if ((result = compare_left(a+ai, b+bi)) != 0)
  140. return result;
  141. } else {
  142. if ((result = compare_right(a+ai, b+bi)) != 0)
  143. return result;
  144. }
  145. }
  146. if (!ca && !cb) {
  147. /* The strings compare the same. Perhaps the caller
  148. will want to call strcmp to break the tie. */
  149. return 0;
  150. }
  151. if (fold_case) {
  152. ca = nat_toupper(ca);
  153. cb = nat_toupper(cb);
  154. }
  155. if (ca < cb)
  156. return -1;
  157. else if (ca > cb)
  158. return +1;
  159. ++ai; ++bi;
  160. }
  161. }
  162. S32 dStrnatcmp(const nat_char* a, const nat_char* b) {
  163. return strnatcmp0(a, b, 0);
  164. }
  165. /* Compare, recognizing numeric string and ignoring case. */
  166. S32 dStrnatcasecmp(const nat_char* a, const nat_char* b) {
  167. return strnatcmp0(a, b, 1);
  168. }
  169. //------------------------------------------------------------------------------
  170. // non-standard string functions
  171. char *dStrdup_r(const char *src, const char *fileName, dsize_t lineNumber)
  172. {
  173. dsize_t bufferLen = dStrlen(src) + 1;
  174. char *buffer = (char *) dMalloc_r(bufferLen, fileName, lineNumber);
  175. dStrcpy(buffer, src, bufferLen);
  176. return buffer;
  177. }
  178. char* dStrichr( char* str, char ch )
  179. {
  180. AssertFatal( str != NULL, "dStrichr - NULL string" );
  181. if( !ch )
  182. return dStrchr( str, ch );
  183. char c = dToupper( ch );
  184. while( *str )
  185. {
  186. if( dToupper( *str ) == c )
  187. return str;
  188. ++ str;
  189. }
  190. return NULL;
  191. }
  192. const char* dStrichr( const char* str, char ch )
  193. {
  194. AssertFatal( str != NULL, "dStrichr - NULL string" );
  195. if( !ch )
  196. return dStrchr( str, ch );
  197. char c = dToupper( ch );
  198. while( *str )
  199. {
  200. if( dToupper( *str ) == c )
  201. return str;
  202. ++ str;
  203. }
  204. return NULL;
  205. }
  206. // concatenates a list of src's onto the end of dst
  207. // the list of src's MUST be terminated by a NULL parameter
  208. // dStrcatl(dst, sizeof(dst), src1, src2, NULL);
  209. char* dStrcatl(char *dst, dsize_t dstSize, ...)
  210. {
  211. const char* src = NULL;
  212. char *p = dst;
  213. AssertFatal(dstSize > 0, "dStrcatl: destination size is set zero");
  214. dstSize--; // leave room for string termination
  215. // find end of dst
  216. while (dstSize && *p++)
  217. dstSize--;
  218. va_list args;
  219. va_start(args, dstSize);
  220. // concatenate each src to end of dst
  221. while ( (src = va_arg(args, const char*)) != NULL )
  222. {
  223. while( dstSize && *src )
  224. {
  225. *p++ = *src++;
  226. dstSize--;
  227. }
  228. }
  229. va_end(args);
  230. // make sure the string is terminated
  231. *p = 0;
  232. return dst;
  233. }
  234. // copy a list of src's into dst
  235. // the list of src's MUST be terminated by a NULL parameter
  236. // dStrccpyl(dst, sizeof(dst), src1, src2, NULL);
  237. char* dStrcpyl(char *dst, dsize_t dstSize, ...)
  238. {
  239. const char* src = NULL;
  240. char *p = dst;
  241. AssertFatal(dstSize > 0, "dStrcpyl: destination size is set zero");
  242. dstSize--; // leave room for string termination
  243. va_list args;
  244. va_start(args, dstSize);
  245. // concatenate each src to end of dst
  246. while ( (src = va_arg(args, const char*)) != NULL )
  247. {
  248. while( dstSize && *src )
  249. {
  250. *p++ = *src++;
  251. dstSize--;
  252. }
  253. }
  254. va_end(args);
  255. // make sure the string is terminated
  256. *p = 0;
  257. return dst;
  258. }
  259. S32 dStrcmp(const UTF16 *str1, const UTF16 *str2)
  260. {
  261. #if defined(TORQUE_OS_WIN)
  262. return wcscmp( reinterpret_cast<const wchar_t *>( str1 ), reinterpret_cast<const wchar_t *>( str2 ) );
  263. #else
  264. S32 ret;
  265. const UTF16 *a, *b;
  266. a = str1;
  267. b = str2;
  268. while( ((ret = *a - *b) == 0) && *a && *b )
  269. a++, b++;
  270. return ret;
  271. #endif
  272. }
  273. char* dStrupr(char *str)
  274. {
  275. #if defined(TORQUE_OS_WIN)
  276. return _strupr(str);
  277. #else
  278. if (str == NULL)
  279. return(NULL);
  280. char* saveStr = str;
  281. while (*str)
  282. {
  283. *str = toupper(*str);
  284. str++;
  285. }
  286. return saveStr;
  287. #endif
  288. }
  289. char* dStrlwr(char *str)
  290. {
  291. #if defined(TORQUE_OS_WIN)
  292. return _strlwr(str);
  293. #else
  294. if (str == NULL)
  295. return(NULL);
  296. char* saveStr = str;
  297. while (*str)
  298. {
  299. *str = tolower(*str);
  300. str++;
  301. }
  302. return saveStr;
  303. #endif
  304. }
  305. //------------------------------------------------------------------------------
  306. S32 dStrlcat(char *dst, const char *src, dsize_t dstSize)
  307. {
  308. //TODO: Do other platforms support strlcat in their libc
  309. #ifdef TORQUE_OS_MAC
  310. S32 len = strlcat(dst, src, dstSize);
  311. AssertWarn(len < dstSize, "Buffer too small in call to dStrlcat!");
  312. return len;
  313. #else //TORQUE_OS_MAC
  314. S32 dstLen = dStrlen(dst);
  315. S32 srcLen = dStrlen(src);
  316. S32 copyLen = srcLen;
  317. //Check for buffer overflow and don't allow it. Warn on debug so we can fix it
  318. AssertWarn(dstLen + copyLen < dstSize, "Buffer too small in call to dStrlcat!");
  319. if (dstLen + copyLen + 1 > dstSize)
  320. {
  321. copyLen = dstSize - dstLen - 1;
  322. }
  323. //Copy src after dst and null terminate
  324. memcpy(dst + dstLen, src, copyLen);
  325. dst[dstLen + copyLen] = 0;
  326. //Return the length of the string we would have generated
  327. return dstLen + srcLen;
  328. #endif //TORQUE_OS_MAC
  329. }
  330. S32 dStrlcpy(char *dst, const char *src, dsize_t dstSize)
  331. {
  332. //TODO: Do other platforms support strlcpy in their libc
  333. #ifdef TORQUE_OS_MAC
  334. S32 len = strlcpy(dst, src, dstSize);
  335. AssertWarn(len < dstSize, "Buffer too small in call to dStrlcpy!");
  336. return len;
  337. #else //TORQUE_OS_MAC
  338. S32 srcLen = dStrlen(src);
  339. S32 copyLen = srcLen;
  340. //Check for buffer overflow and don't allow it. Warn on debug so we can fix it
  341. AssertFatal(copyLen < dstSize, "Buffer too small in call to dStrlcpy!");
  342. if (srcLen + 1 > dstSize)
  343. {
  344. copyLen = dstSize - 1;
  345. }
  346. //Copy src and null terminate
  347. memcpy(dst, src, copyLen);
  348. dst[copyLen] = 0;
  349. //Return the length of the string we would have generated
  350. return srcLen;
  351. #endif //TORQUE_OS_MAC
  352. }
  353. //------------------------------------------------------------------------------
  354. // standard I/O functions
  355. void dPrintf(const char *format, ...)
  356. {
  357. va_list args;
  358. va_start(args, format);
  359. vprintf(format, args);
  360. va_end(args);
  361. }
  362. S32 dVprintf(const char *format, va_list arglist)
  363. {
  364. return (S32)vprintf(format, arglist);
  365. }
  366. S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...)
  367. {
  368. va_list args;
  369. va_start(args, format);
  370. S32 len = vsnprintf(buffer, bufferSize, format, args);
  371. va_end(args);
  372. AssertWarn( len < bufferSize, "Buffer too small in call to dSprintf!" );
  373. return (len);
  374. }
  375. S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist)
  376. {
  377. S32 len = vsnprintf(buffer, bufferSize, format, arglist);
  378. AssertWarn( len < bufferSize, "Buffer too small in call to dVsprintf!" );
  379. return (len);
  380. }
  381. S32 dSscanf(const char *buffer, const char *format, ...)
  382. {
  383. #if defined(TORQUE_OS_WIN)
  384. va_list args;
  385. va_start(args, format);
  386. // Boy is this lame. We have to scan through the format string, and find out how many
  387. // arguments there are. We'll store them off as void*, and pass them to the sscanf
  388. // function through specialized calls. We're going to have to put a cap on the number of args that
  389. // can be passed, 8 for the moment. Sigh.
  390. static void* sVarArgs[20];
  391. U32 numArgs = 0;
  392. for (const char* search = format; *search != '\0'; search++) {
  393. if (search[0] == '%' && search[1] != '%')
  394. numArgs++;
  395. }
  396. AssertFatal(numArgs <= 20, "Error, too many arguments to lame implementation of dSscanf. Fix implmentation");
  397. // Ok, we have the number of arguments...
  398. for (U32 i = 0; i < numArgs; i++)
  399. sVarArgs[i] = va_arg(args, void*);
  400. va_end(args);
  401. switch (numArgs) {
  402. case 0: return 0;
  403. case 1: return sscanf(buffer, format, sVarArgs[0]);
  404. case 2: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1]);
  405. case 3: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2]);
  406. case 4: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3]);
  407. case 5: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4]);
  408. case 6: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5]);
  409. case 7: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6]);
  410. case 8: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7]);
  411. case 9: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8]);
  412. case 10: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9]);
  413. case 11: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10]);
  414. case 12: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11]);
  415. case 13: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12]);
  416. case 14: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13]);
  417. case 15: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14]);
  418. case 16: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14], sVarArgs[15]);
  419. case 17: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14], sVarArgs[15], sVarArgs[16]);
  420. case 18: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14], sVarArgs[15], sVarArgs[16], sVarArgs[17]);
  421. case 19: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14], sVarArgs[15], sVarArgs[16], sVarArgs[17], sVarArgs[18]);
  422. case 20: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8], sVarArgs[9], sVarArgs[10], sVarArgs[11], sVarArgs[12], sVarArgs[13], sVarArgs[14], sVarArgs[15], sVarArgs[16], sVarArgs[17], sVarArgs[18], sVarArgs[19]);
  423. }
  424. return 0;
  425. #else
  426. va_list args;
  427. va_start(args, format);
  428. S32 res = vsscanf(buffer, format, args);
  429. va_end(args);
  430. return res;
  431. #endif
  432. }
  433. /// Safe form of dStrcmp: checks both strings for NULL before comparing
  434. bool dStrEqual(const char* str1, const char* str2)
  435. {
  436. if (!str1 || !str2)
  437. return false;
  438. else
  439. return (String::compare(str1, str2) == 0);
  440. }
  441. /// Check if one string starts with another
  442. bool dStrStartsWith(const char* str1, const char* str2)
  443. {
  444. return !dStrnicmp(str1, str2, dStrlen(str2));
  445. }
  446. /// Check if one string ends with another
  447. bool dStrEndsWith(const char* str1, const char* str2)
  448. {
  449. const char *p = str1 + dStrlen(str1) - dStrlen(str2);
  450. return ((p >= str1) && !dStricmp(p, str2));
  451. }
  452. /// Strip the path from the input filename
  453. char* dStripPath(const char* filename)
  454. {
  455. const char* itr = filename + dStrlen(filename);
  456. while(--itr != filename) {
  457. if (*itr == '/' || *itr == '\\') {
  458. itr++;
  459. break;
  460. }
  461. }
  462. return dStrdup(itr);
  463. }
  464. char* dStristr( char* str1, const char* str2 )
  465. {
  466. if( !str1 || !str2 )
  467. return NULL;
  468. // Slow but at least we have it.
  469. U32 str2len = strlen( str2 );
  470. while( *str1 )
  471. {
  472. if( strncasecmp( str1, str2, str2len ) == 0 )
  473. return str1;
  474. ++ str1;
  475. }
  476. return NULL;
  477. }
  478. const char* dStristr( const char* str1, const char* str2 )
  479. {
  480. return dStristr( const_cast< char* >( str1 ), str2 );
  481. }
  482. int dStrrev(char* str)
  483. {
  484. int l=dStrlen(str)-1; //get the string length
  485. for(int x=0;x < l;x++,l--)
  486. {
  487. str[x]^=str[l]; //triple XOR Trick
  488. str[l]^=str[x]; //for not using a temp
  489. str[x]^=str[l];
  490. }
  491. return l;
  492. }
  493. int dItoa(int n, char s[])
  494. {
  495. int i, sign;
  496. if ((sign = n) < 0) /* record sign */
  497. n = -n; /* make n positive */
  498. i = 0;
  499. do { /* generate digits in reverse order */
  500. s[i++] = n % 10 + '0'; /* get next digit */
  501. } while ((n /= 10) > 0); /* delete it */
  502. if (sign < 0)
  503. s[i++] = '-';
  504. s[i] = '\0';
  505. dStrrev(s);
  506. return dStrlen(s);
  507. }