stringFunctions.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. char *buffer = (char *) dMalloc_r(dStrlen(src) + 1, fileName, lineNumber);
  174. dStrcpy(buffer, src, dStrlen(src) + 1);
  175. return buffer;
  176. }
  177. char* dStrichr( char* str, char ch )
  178. {
  179. AssertFatal( str != NULL, "dStrichr - NULL string" );
  180. if( !ch )
  181. return dStrchr( str, ch );
  182. char c = dToupper( ch );
  183. while( *str )
  184. {
  185. if( dToupper( *str ) == c )
  186. return str;
  187. ++ str;
  188. }
  189. return NULL;
  190. }
  191. const char* dStrichr( const char* str, char ch )
  192. {
  193. AssertFatal( str != NULL, "dStrichr - NULL string" );
  194. if( !ch )
  195. return dStrchr( str, ch );
  196. char c = dToupper( ch );
  197. while( *str )
  198. {
  199. if( dToupper( *str ) == c )
  200. return str;
  201. ++ str;
  202. }
  203. return NULL;
  204. }
  205. // concatenates a list of src's onto the end of dst
  206. // the list of src's MUST be terminated by a NULL parameter
  207. // dStrcatl(dst, sizeof(dst), src1, src2, NULL);
  208. char* dStrcatl(char *dst, dsize_t dstSize, ...)
  209. {
  210. const char* src = NULL;
  211. char *p = dst;
  212. AssertFatal(dstSize > 0, "dStrcatl: destination size is set zero");
  213. dstSize--; // leave room for string termination
  214. // find end of dst
  215. while (dstSize && *p++)
  216. dstSize--;
  217. va_list args;
  218. va_start(args, dstSize);
  219. // concatenate each src to end of dst
  220. while ( (src = va_arg(args, const char*)) != NULL )
  221. {
  222. while( dstSize && *src )
  223. {
  224. *p++ = *src++;
  225. dstSize--;
  226. }
  227. }
  228. va_end(args);
  229. // make sure the string is terminated
  230. *p = 0;
  231. return dst;
  232. }
  233. // copy a list of src's into dst
  234. // the list of src's MUST be terminated by a NULL parameter
  235. // dStrccpyl(dst, sizeof(dst), src1, src2, NULL);
  236. char* dStrcpyl(char *dst, dsize_t dstSize, ...)
  237. {
  238. const char* src = NULL;
  239. char *p = dst;
  240. AssertFatal(dstSize > 0, "dStrcpyl: destination size is set zero");
  241. dstSize--; // leave room for string termination
  242. va_list args;
  243. va_start(args, dstSize);
  244. // concatenate each src to end of dst
  245. while ( (src = va_arg(args, const char*)) != NULL )
  246. {
  247. while( dstSize && *src )
  248. {
  249. *p++ = *src++;
  250. dstSize--;
  251. }
  252. }
  253. va_end(args);
  254. // make sure the string is terminated
  255. *p = 0;
  256. return dst;
  257. }
  258. S32 dStrcmp( const UTF16 *str1, const UTF16 *str2)
  259. {
  260. #if defined(TORQUE_OS_WIN)
  261. return wcscmp( reinterpret_cast<const wchar_t *>( str1 ), reinterpret_cast<const wchar_t *>( str2 ) );
  262. #else
  263. S32 ret;
  264. const UTF16 *a, *b;
  265. a = str1;
  266. b = str2;
  267. while( ((ret = *a - *b) == 0) && *a && *b )
  268. a++, b++;
  269. return ret;
  270. #endif
  271. }
  272. char* dStrupr(char *str)
  273. {
  274. #if defined(TORQUE_OS_WIN)
  275. return _strupr(str);
  276. #else
  277. if (str == NULL)
  278. return(NULL);
  279. char* saveStr = str;
  280. while (*str)
  281. {
  282. *str = toupper(*str);
  283. str++;
  284. }
  285. return saveStr;
  286. #endif
  287. }
  288. char* dStrlwr(char *str)
  289. {
  290. #if defined(TORQUE_OS_WIN)
  291. return _strlwr(str);
  292. #else
  293. if (str == NULL)
  294. return(NULL);
  295. char* saveStr = str;
  296. while (*str)
  297. {
  298. *str = tolower(*str);
  299. str++;
  300. }
  301. return saveStr;
  302. #endif
  303. }
  304. //------------------------------------------------------------------------------
  305. S32 dStrlcat(char *dst, const char *src, dsize_t dstSize)
  306. {
  307. //TODO: Do other platforms support strlcat in their libc
  308. #ifdef TORQUE_OS_MAC
  309. S32 len = strlcat(dst, src, dstSize);
  310. AssertWarn(len < dstSize, "Buffer too small in call to dStrlcat!");
  311. return len;
  312. #else //TORQUE_OS_MAC
  313. S32 dstLen = dStrlen(dst);
  314. S32 srcLen = dStrlen(src);
  315. S32 copyLen = srcLen;
  316. //Check for buffer overflow and don't allow it. Warn on debug so we can fix it
  317. AssertWarn(dstLen + copyLen < dstSize, "Buffer too small in call to dStrlcat!");
  318. if (dstLen + copyLen + 1 > dstSize)
  319. {
  320. copyLen = dstSize - dstLen - 1;
  321. }
  322. //Copy src after dst and null terminate
  323. memcpy(dst + dstLen, src, copyLen);
  324. dst[dstLen + copyLen] = 0;
  325. //Return the length of the string we would have generated
  326. return dstLen + srcLen;
  327. #endif //TORQUE_OS_MAC
  328. }
  329. S32 dStrlcpy(char *dst, const char *src, dsize_t dstSize)
  330. {
  331. //TODO: Do other platforms support strlcpy in their libc
  332. #ifdef TORQUE_OS_MAC
  333. S32 len = strlcpy(dst, src, dstSize);
  334. AssertWarn(len < dstSize, "Buffer too small in call to dStrlcpy!");
  335. return len;
  336. #else //TORQUE_OS_MAC
  337. S32 srcLen = dStrlen(src);
  338. S32 copyLen = srcLen;
  339. //Check for buffer overflow and don't allow it. Warn on debug so we can fix it
  340. AssertWarn(copyLen < dstSize, "Buffer too small in call to dStrlcpy!");
  341. if (srcLen + 1 > dstSize)
  342. {
  343. copyLen = dstSize - 1;
  344. }
  345. //Copy src and null terminate
  346. memcpy(dst, src, copyLen);
  347. dst[copyLen] = 0;
  348. //Return the length of the string we would have generated
  349. return srcLen;
  350. #endif //TORQUE_OS_MAC
  351. }
  352. //------------------------------------------------------------------------------
  353. // standard I/O functions
  354. void dPrintf(const char *format, ...)
  355. {
  356. va_list args;
  357. va_start(args, format);
  358. vprintf(format, args);
  359. va_end(args);
  360. }
  361. S32 dVprintf(const char *format, va_list arglist)
  362. {
  363. return (S32)vprintf(format, arglist);
  364. }
  365. S32 dSprintf(char *buffer, U32 bufferSize, const char *format, ...)
  366. {
  367. va_list args;
  368. va_start(args, format);
  369. S32 len = vsnprintf(buffer, bufferSize, format, args);
  370. va_end(args);
  371. AssertWarn( len < bufferSize, "Buffer too small in call to dSprintf!" );
  372. return (len);
  373. }
  374. S32 dVsprintf(char *buffer, U32 bufferSize, const char *format, va_list arglist)
  375. {
  376. S32 len = vsnprintf(buffer, bufferSize, format, arglist);
  377. AssertWarn( len < bufferSize, "Buffer too small in call to dVsprintf!" );
  378. return (len);
  379. }
  380. S32 dSscanf(const char *buffer, const char *format, ...)
  381. {
  382. #if defined(TORQUE_OS_WIN)
  383. va_list args;
  384. va_start(args, format);
  385. // Boy is this lame. We have to scan through the format string, and find out how many
  386. // arguments there are. We'll store them off as void*, and pass them to the sscanf
  387. // function through specialized calls. We're going to have to put a cap on the number of args that
  388. // can be passed, 8 for the moment. Sigh.
  389. static void* sVarArgs[20];
  390. U32 numArgs = 0;
  391. for (const char* search = format; *search != '\0'; search++) {
  392. if (search[0] == '%' && search[1] != '%')
  393. numArgs++;
  394. }
  395. AssertFatal(numArgs <= 20, "Error, too many arguments to lame implementation of dSscanf. Fix implmentation");
  396. // Ok, we have the number of arguments...
  397. for (U32 i = 0; i < numArgs; i++)
  398. sVarArgs[i] = va_arg(args, void*);
  399. va_end(args);
  400. switch (numArgs) {
  401. case 0: return 0;
  402. case 1: return sscanf(buffer, format, sVarArgs[0]);
  403. case 2: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1]);
  404. case 3: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2]);
  405. case 4: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3]);
  406. case 5: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4]);
  407. case 6: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5]);
  408. case 7: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6]);
  409. case 8: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7]);
  410. case 9: return sscanf(buffer, format, sVarArgs[0], sVarArgs[1], sVarArgs[2], sVarArgs[3], sVarArgs[4], sVarArgs[5], sVarArgs[6], sVarArgs[7], sVarArgs[8]);
  411. 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]);
  412. 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]);
  413. 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]);
  414. 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]);
  415. 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]);
  416. 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]);
  417. 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]);
  418. 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]);
  419. 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]);
  420. 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]);
  421. 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]);
  422. }
  423. return 0;
  424. #else
  425. va_list args;
  426. va_start(args, format);
  427. S32 res = vsscanf(buffer, format, args);
  428. va_end(args);
  429. return res;
  430. #endif
  431. }
  432. /// Safe form of dStrcmp: checks both strings for NULL before comparing
  433. bool dStrEqual(const char* str1, const char* str2)
  434. {
  435. if (!str1 || !str2)
  436. return false;
  437. else
  438. return (dStrcmp(str1, str2) == 0);
  439. }
  440. /// Check if one string starts with another
  441. bool dStrStartsWith(const char* str1, const char* str2)
  442. {
  443. return !dStrnicmp(str1, str2, dStrlen(str2));
  444. }
  445. /// Check if one string ends with another
  446. bool dStrEndsWith(const char* str1, const char* str2)
  447. {
  448. const char *p = str1 + dStrlen(str1) - dStrlen(str2);
  449. return ((p >= str1) && !dStricmp(p, str2));
  450. }
  451. /// Strip the path from the input filename
  452. char* dStripPath(const char* filename)
  453. {
  454. const char* itr = filename + dStrlen(filename);
  455. while(--itr != filename) {
  456. if (*itr == '/' || *itr == '\\') {
  457. itr++;
  458. break;
  459. }
  460. }
  461. return dStrdup(itr);
  462. }
  463. char* dStristr( char* str1, const char* str2 )
  464. {
  465. if( !str1 || !str2 )
  466. return NULL;
  467. // Slow but at least we have it.
  468. U32 str2len = strlen( str2 );
  469. while( *str1 )
  470. {
  471. if( strncasecmp( str1, str2, str2len ) == 0 )
  472. return str1;
  473. ++ str1;
  474. }
  475. return NULL;
  476. }
  477. const char* dStristr( const char* str1, const char* str2 )
  478. {
  479. return dStristr( const_cast< char* >( str1 ), str2 );
  480. }
  481. int dStrrev(char* str)
  482. {
  483. int l=dStrlen(str)-1; //get the string length
  484. for(int x=0;x < l;x++,l--)
  485. {
  486. str[x]^=str[l]; //triple XOR Trick
  487. str[l]^=str[x]; //for not using a temp
  488. str[x]^=str[l];
  489. }
  490. return l;
  491. }
  492. int dItoa(int n, char s[])
  493. {
  494. int i, sign;
  495. if ((sign = n) < 0) /* record sign */
  496. n = -n; /* make n positive */
  497. i = 0;
  498. do { /* generate digits in reverse order */
  499. s[i++] = n % 10 + '0'; /* get next digit */
  500. } while ((n /= 10) > 0); /* delete it */
  501. if (sign < 0)
  502. s[i++] = '-';
  503. s[i] = '\0';
  504. dStrrev(s);
  505. return dStrlen(s);
  506. }