rt_vsnprintf_tiny.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-11-19 Meco Man the first version
  9. */
  10. #include <rtthread.h>
  11. #define _ISDIGIT(c) ((unsigned)((c) - '0') < 10)
  12. /**
  13. * @brief This function will duplicate a string.
  14. *
  15. * @param n is the string to be duplicated.
  16. *
  17. * @param base is support divide instructions value.
  18. *
  19. * @return the duplicated string pointer.
  20. */
  21. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  22. rt_inline int divide(unsigned long long *n, int base)
  23. #else
  24. rt_inline int divide(unsigned long *n, int base)
  25. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  26. {
  27. int res;
  28. /* optimized for processor which does not support divide instructions. */
  29. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  30. res = (int)((*n) % base);
  31. *n = (long long)((*n) / base);
  32. #else
  33. res = (int)((*n) % base);
  34. *n = (long)((*n) / base);
  35. #endif
  36. return res;
  37. }
  38. rt_inline int skip_atoi(const char **s)
  39. {
  40. int i = 0;
  41. while (_ISDIGIT(**s))
  42. i = i * 10 + *((*s)++) - '0';
  43. return i;
  44. }
  45. #define ZEROPAD (1 << 0) /* pad with zero */
  46. #define SIGN (1 << 1) /* unsigned/signed long */
  47. #define PLUS (1 << 2) /* show plus */
  48. #define SPACE (1 << 3) /* space if plus */
  49. #define LEFT (1 << 4) /* left justified */
  50. #define SPECIAL (1 << 5) /* 0x */
  51. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  52. static char *print_number(char *buf,
  53. char *end,
  54. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  55. unsigned long long num,
  56. #else
  57. unsigned long num,
  58. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  59. int base,
  60. int qualifier,
  61. int s,
  62. int precision,
  63. int type)
  64. {
  65. char c = 0, sign = 0;
  66. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  67. char tmp[64] = {0};
  68. #else
  69. char tmp[32] = {0};
  70. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  71. int precision_bak = precision;
  72. const char *digits = RT_NULL;
  73. static const char small_digits[] = "0123456789abcdef";
  74. static const char large_digits[] = "0123456789ABCDEF";
  75. int i = 0;
  76. int size = 0;
  77. size = s;
  78. digits = (type & LARGE) ? large_digits : small_digits;
  79. if (type & LEFT)
  80. {
  81. type &= ~ZEROPAD;
  82. }
  83. c = (type & ZEROPAD) ? '0' : ' ';
  84. /* get sign */
  85. sign = 0;
  86. if (type & SIGN)
  87. {
  88. switch (qualifier)
  89. {
  90. case 'h':
  91. if ((rt_int16_t)num < 0)
  92. {
  93. sign = '-';
  94. num = (rt_uint16_t)-num;
  95. }
  96. break;
  97. case 'L':
  98. case 'l':
  99. if ((long)num < 0)
  100. {
  101. sign = '-';
  102. num = (unsigned long)-num;
  103. }
  104. break;
  105. case 0:
  106. default:
  107. if ((rt_int32_t)num < 0)
  108. {
  109. sign = '-';
  110. num = (rt_uint32_t)-num;
  111. }
  112. break;
  113. }
  114. if (sign != '-')
  115. {
  116. if (type & PLUS)
  117. {
  118. sign = '+';
  119. }
  120. else if (type & SPACE)
  121. {
  122. sign = ' ';
  123. }
  124. }
  125. }
  126. if (type & SPECIAL)
  127. {
  128. if (base == 2 || base == 16)
  129. {
  130. size -= 2;
  131. }
  132. else if (base == 8)
  133. {
  134. size--;
  135. }
  136. }
  137. i = 0;
  138. if (num == 0)
  139. {
  140. tmp[i++] = '0';
  141. }
  142. else
  143. {
  144. while (num != 0)
  145. tmp[i++] = digits[divide(&num, base)];
  146. }
  147. if (i > precision)
  148. {
  149. precision = i;
  150. }
  151. size -= precision;
  152. if (!(type & (ZEROPAD | LEFT)))
  153. {
  154. if ((sign) && (size > 0))
  155. {
  156. size--;
  157. }
  158. while (size-- > 0)
  159. {
  160. if (buf < end)
  161. {
  162. *buf = ' ';
  163. }
  164. ++ buf;
  165. }
  166. }
  167. if (sign)
  168. {
  169. if (buf < end)
  170. {
  171. *buf = sign;
  172. }
  173. -- size;
  174. ++ buf;
  175. }
  176. if (type & SPECIAL)
  177. {
  178. if (base == 2)
  179. {
  180. if (buf < end)
  181. *buf = '0';
  182. ++ buf;
  183. if (buf < end)
  184. *buf = 'b';
  185. ++ buf;
  186. }
  187. else if (base == 8)
  188. {
  189. if (buf < end)
  190. *buf = '0';
  191. ++ buf;
  192. }
  193. else if (base == 16)
  194. {
  195. if (buf < end)
  196. {
  197. *buf = '0';
  198. }
  199. ++ buf;
  200. if (buf < end)
  201. {
  202. *buf = type & LARGE ? 'X' : 'x';
  203. }
  204. ++ buf;
  205. }
  206. }
  207. /* no align to the left */
  208. if (!(type & LEFT))
  209. {
  210. while (size-- > 0)
  211. {
  212. if (buf < end)
  213. {
  214. *buf = c;
  215. }
  216. ++ buf;
  217. }
  218. }
  219. while (i < precision--)
  220. {
  221. if (buf < end)
  222. {
  223. *buf = '0';
  224. }
  225. ++ buf;
  226. }
  227. /* put number in the temporary buffer */
  228. while (i-- > 0 && (precision_bak != 0))
  229. {
  230. if (buf < end)
  231. {
  232. *buf = tmp[i];
  233. }
  234. ++ buf;
  235. }
  236. while (size-- > 0)
  237. {
  238. if (buf < end)
  239. {
  240. *buf = ' ';
  241. }
  242. ++ buf;
  243. }
  244. return buf;
  245. }
  246. #if (defined(__GNUC__) && !defined(__ARMCC_VERSION) /* GCC */) && (__GNUC__ >= 7)
  247. /* Disable "-Wimplicit-fallthrough" below GNUC V7 */
  248. #pragma GCC diagnostic push
  249. /* ignore warning: this statement may fall through */
  250. #pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
  251. #endif /* (defined(__GNUC__) && !defined(__ARMCC_VERSION)) && (__GNUC__ >= 7 */
  252. /**
  253. * @brief This function will fill a formatted string to buffer.
  254. *
  255. * @param buf is the buffer to save formatted string.
  256. *
  257. * @param size is the size of buffer.
  258. *
  259. * @param fmt is the format parameters.
  260. *
  261. * @param args is a list of variable parameters.
  262. *
  263. * @return The number of characters actually written to buffer.
  264. */
  265. int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
  266. {
  267. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  268. unsigned long long num = 0;
  269. #else
  270. unsigned long num = 0;
  271. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  272. int i = 0, len = 0;
  273. char *str = RT_NULL, *end = RT_NULL, c = 0;
  274. const char *s = RT_NULL;
  275. rt_uint8_t base = 0; /* the base of number */
  276. rt_uint8_t flags = 0; /* flags to print number */
  277. rt_uint8_t qualifier = 0; /* 'h', 'l', or 'L' for integer fields */
  278. rt_int32_t field_width = 0; /* width of output field */
  279. int precision = 0; /* min. # of digits for integers and max for a string */
  280. str = buf;
  281. end = buf + size;
  282. /* Make sure end is always >= buf */
  283. if (end < buf)
  284. {
  285. end = ((char *) - 1);
  286. size = end - buf;
  287. }
  288. for (; *fmt ; ++fmt)
  289. {
  290. if (*fmt != '%')
  291. {
  292. if (str < end)
  293. {
  294. *str = *fmt;
  295. }
  296. ++ str;
  297. continue;
  298. }
  299. /* process flags */
  300. flags = 0;
  301. while (1)
  302. {
  303. /* skips the first '%' also */
  304. ++fmt;
  305. if (*fmt == '-') flags |= LEFT;
  306. else if (*fmt == '+') flags |= PLUS;
  307. else if (*fmt == ' ') flags |= SPACE;
  308. else if (*fmt == '#') flags |= SPECIAL;
  309. else if (*fmt == '0') flags |= ZEROPAD;
  310. else break;
  311. }
  312. /* get field width */
  313. field_width = -1;
  314. if (_ISDIGIT(*fmt))
  315. {
  316. field_width = skip_atoi(&fmt);
  317. }
  318. else if (*fmt == '*')
  319. {
  320. ++fmt;
  321. /* it's the next argument */
  322. field_width = va_arg(args, int);
  323. if (field_width < 0)
  324. {
  325. field_width = -field_width;
  326. flags |= LEFT;
  327. }
  328. }
  329. /* get the precision */
  330. precision = -1;
  331. if (*fmt == '.')
  332. {
  333. ++fmt;
  334. if (_ISDIGIT(*fmt))
  335. {
  336. precision = skip_atoi(&fmt);
  337. }
  338. else if (*fmt == '*')
  339. {
  340. ++fmt;
  341. /* it's the next argument */
  342. precision = va_arg(args, int);
  343. }
  344. if (precision < 0)
  345. {
  346. precision = 0;
  347. }
  348. }
  349. qualifier = 0; /* get the conversion qualifier */
  350. if (*fmt == 'h' || *fmt == 'l' ||
  351. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  352. *fmt == 'L' ||
  353. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  354. *fmt == 'z')
  355. {
  356. qualifier = *fmt;
  357. ++fmt;
  358. #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG
  359. if (qualifier == 'l' && *fmt == 'l')
  360. {
  361. qualifier = 'L';
  362. ++fmt;
  363. }
  364. #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */
  365. if (qualifier == 'h' && *fmt == 'h')
  366. {
  367. qualifier = 'H';
  368. ++fmt;
  369. }
  370. }
  371. /* the default base */
  372. base = 10;
  373. switch (*fmt)
  374. {
  375. case 'c':
  376. if (!(flags & LEFT))
  377. {
  378. while (--field_width > 0)
  379. {
  380. if (str < end) *str = ' ';
  381. ++ str;
  382. }
  383. }
  384. /* get character */
  385. c = (rt_uint8_t)va_arg(args, int);
  386. if (str < end)
  387. {
  388. *str = c;
  389. }
  390. ++ str;
  391. /* put width */
  392. while (--field_width > 0)
  393. {
  394. if (str < end) *str = ' ';
  395. ++ str;
  396. }
  397. continue;
  398. case 's':
  399. s = va_arg(args, char *);
  400. if (!s)
  401. {
  402. s = "(null)";
  403. }
  404. for (len = 0; (len != field_width) && (s[len] != '\0'); len++);
  405. if (precision > 0 && len > precision)
  406. {
  407. len = precision;
  408. }
  409. if (!(flags & LEFT))
  410. {
  411. while (len < field_width--)
  412. {
  413. if (str < end) *str = ' ';
  414. ++ str;
  415. }
  416. }
  417. for (i = 0; i < len; ++i)
  418. {
  419. if (str < end) *str = *s;
  420. ++ str;
  421. ++ s;
  422. }
  423. while (len < field_width--)
  424. {
  425. if (str < end) *str = ' ';
  426. ++ str;
  427. }
  428. continue;
  429. case 'p':
  430. if (field_width == -1)
  431. {
  432. field_width = sizeof(void *) << 1;
  433. field_width += 2; /* `0x` prefix */
  434. flags |= SPECIAL;
  435. flags |= ZEROPAD;
  436. }
  437. str = print_number(str, end, (unsigned long)va_arg(args, void *),
  438. 16, qualifier, field_width, precision, flags);
  439. continue;
  440. case '%':
  441. if (str < end)
  442. {
  443. *str = '%';
  444. }
  445. ++ str;
  446. continue;
  447. /* integer number formats - set up the flags and "break" */
  448. case 'b':
  449. base = 2;
  450. break;
  451. case 'o':
  452. base = 8;
  453. break;
  454. case 'X':
  455. flags |= LARGE;
  456. case 'x':
  457. base = 16;
  458. break;
  459. case 'd':
  460. case 'i':
  461. flags |= SIGN;
  462. case 'u':
  463. break;
  464. case 'e':
  465. case 'E':
  466. case 'G':
  467. case 'g':
  468. case 'f':
  469. case 'F':
  470. va_arg(args, double);
  471. default:
  472. if (str < end)
  473. {
  474. *str = '%';
  475. }
  476. ++ str;
  477. if (*fmt)
  478. {
  479. if (str < end)
  480. {
  481. *str = *fmt;
  482. }
  483. ++ str;
  484. }
  485. else
  486. {
  487. -- fmt;
  488. }
  489. continue;
  490. }
  491. if (qualifier == 'L')
  492. {
  493. num = va_arg(args, unsigned long long);
  494. }
  495. else if (qualifier == 'l')
  496. {
  497. num = va_arg(args, unsigned long);
  498. }
  499. else if (qualifier == 'H')
  500. {
  501. num = (rt_int8_t)va_arg(args, rt_int32_t);
  502. if (flags & SIGN)
  503. {
  504. num = (rt_int8_t)num;
  505. }
  506. }
  507. else if (qualifier == 'h')
  508. {
  509. num = (rt_uint16_t)va_arg(args, rt_int32_t);
  510. if (flags & SIGN)
  511. {
  512. num = (rt_int16_t)num;
  513. }
  514. }
  515. else if (qualifier == 'z')
  516. {
  517. num = va_arg(args, rt_size_t);
  518. if (flags & SIGN)
  519. {
  520. num = (rt_ssize_t)num;
  521. }
  522. }
  523. else
  524. {
  525. num = (rt_uint32_t)va_arg(args, unsigned long);
  526. }
  527. str = print_number(str, end, num, base, qualifier, field_width, precision, flags);
  528. }
  529. if (size > 0)
  530. {
  531. if (str < end)
  532. {
  533. *str = '\0';
  534. }
  535. else
  536. {
  537. end[-1] = '\0';
  538. }
  539. }
  540. /* the trailing null byte doesn't count towards the total
  541. * ++str;
  542. */
  543. return str - buf;
  544. }
  545. #if (defined(__GNUC__) && !defined(__ARMCC_VERSION) /* GCC */) && (__GNUC__ >= 7)
  546. #pragma GCC diagnostic pop /* ignored "-Wimplicit-fallthrough" */
  547. #endif /* (defined(__GNUC__) && !defined(__ARMCC_VERSION)) && (__GNUC__ >= 7 */