lj_strfmt.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. ** String formatting.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #ifndef _LJ_STRFMT_H
  6. #define _LJ_STRFMT_H
  7. #include "lj_obj.h"
  8. typedef uint32_t SFormat; /* Format indicator. */
  9. /* Format parser state. */
  10. typedef struct FormatState {
  11. const uint8_t *p; /* Current format string pointer. */
  12. const uint8_t *e; /* End of format string. */
  13. const char *str; /* Returned literal string. */
  14. MSize len; /* Size of literal string. */
  15. } FormatState;
  16. /* Format types (max. 16). */
  17. typedef enum FormatType {
  18. STRFMT_EOF, STRFMT_ERR, STRFMT_LIT,
  19. STRFMT_INT, STRFMT_UINT, STRFMT_NUM, STRFMT_STR, STRFMT_CHAR, STRFMT_PTR
  20. } FormatType;
  21. /* Format subtypes (bits are reused). */
  22. #define STRFMT_T_HEX 0x0010 /* STRFMT_UINT */
  23. #define STRFMT_T_OCT 0x0020 /* STRFMT_UINT */
  24. #define STRFMT_T_FP_A 0x0000 /* STRFMT_NUM */
  25. #define STRFMT_T_FP_E 0x0010 /* STRFMT_NUM */
  26. #define STRFMT_T_FP_F 0x0020 /* STRFMT_NUM */
  27. #define STRFMT_T_FP_G 0x0030 /* STRFMT_NUM */
  28. #define STRFMT_T_QUOTED 0x0010 /* STRFMT_STR */
  29. /* Format flags. */
  30. #define STRFMT_F_LEFT 0x0100
  31. #define STRFMT_F_PLUS 0x0200
  32. #define STRFMT_F_ZERO 0x0400
  33. #define STRFMT_F_SPACE 0x0800
  34. #define STRFMT_F_ALT 0x1000
  35. #define STRFMT_F_UPPER 0x2000
  36. /* Format indicator fields. */
  37. #define STRFMT_SH_WIDTH 16
  38. #define STRFMT_SH_PREC 24
  39. #define STRFMT_TYPE(sf) ((FormatType)((sf) & 15))
  40. #define STRFMT_WIDTH(sf) (((sf) >> STRFMT_SH_WIDTH) & 255u)
  41. #define STRFMT_PREC(sf) ((((sf) >> STRFMT_SH_PREC) & 255u) - 1u)
  42. #define STRFMT_FP(sf) (((sf) >> 4) & 3)
  43. /* Formats for conversion characters. */
  44. #define STRFMT_A (STRFMT_NUM|STRFMT_T_FP_A)
  45. #define STRFMT_C (STRFMT_CHAR)
  46. #define STRFMT_D (STRFMT_INT)
  47. #define STRFMT_E (STRFMT_NUM|STRFMT_T_FP_E)
  48. #define STRFMT_F (STRFMT_NUM|STRFMT_T_FP_F)
  49. #define STRFMT_G (STRFMT_NUM|STRFMT_T_FP_G)
  50. #define STRFMT_I STRFMT_D
  51. #define STRFMT_O (STRFMT_UINT|STRFMT_T_OCT)
  52. #define STRFMT_P (STRFMT_PTR)
  53. #define STRFMT_Q (STRFMT_STR|STRFMT_T_QUOTED)
  54. #define STRFMT_S (STRFMT_STR)
  55. #define STRFMT_U (STRFMT_UINT)
  56. #define STRFMT_X (STRFMT_UINT|STRFMT_T_HEX)
  57. #define STRFMT_G14 (STRFMT_G | ((14+1) << STRFMT_SH_PREC))
  58. /* Maximum buffer sizes for conversions. */
  59. #define STRFMT_MAXBUF_XINT (1+22) /* '0' prefix + uint64_t in octal. */
  60. #define STRFMT_MAXBUF_INT (1+10) /* Sign + int32_t in decimal. */
  61. #define STRFMT_MAXBUF_NUM 32 /* Must correspond with STRFMT_G14. */
  62. #define STRFMT_MAXBUF_PTR (2+2*sizeof(ptrdiff_t)) /* "0x" + hex ptr. */
  63. /* Format parser. */
  64. LJ_FUNC SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs);
  65. static LJ_AINLINE void lj_strfmt_init(FormatState *fs, const char *p, MSize len)
  66. {
  67. fs->p = (const uint8_t *)p;
  68. fs->e = (const uint8_t *)p + len;
  69. /* Must be NUL-terminated. May have NULs inside, too. */
  70. lj_assertX(*fs->e == 0, "format not NUL-terminated");
  71. }
  72. /* Raw conversions. */
  73. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k);
  74. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v);
  75. LJ_FUNC char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v);
  76. LJ_FUNC const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp);
  77. /* Unformatted conversions to buffer. */
  78. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k);
  79. #if LJ_HASJIT
  80. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o);
  81. #endif
  82. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v);
  83. #if LJ_HASJIT
  84. LJ_FUNC SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str);
  85. #endif
  86. /* Formatted conversions to buffer. */
  87. LJ_FUNC SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k);
  88. LJ_FUNC SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n);
  89. LJ_FUNC SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n);
  90. LJ_FUNC SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat, lua_Number n);
  91. LJ_FUNC SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat, int32_t c);
  92. #if LJ_HASJIT
  93. LJ_FUNC SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat, GCstr *str);
  94. #endif
  95. LJ_FUNC int lj_strfmt_putarg(lua_State *L, SBuf *sb, int arg, int retry);
  96. /* Conversions to strings. */
  97. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k);
  98. LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o);
  99. LJ_FUNCA GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o);
  100. #if LJ_HASJIT
  101. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c);
  102. #endif
  103. LJ_FUNC GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o);
  104. /* Internal string formatting. */
  105. LJ_FUNC const char *lj_strfmt_pushvf(lua_State *L, const char *fmt,
  106. va_list argp);
  107. LJ_FUNC const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)
  108. #if defined(__GNUC__) || defined(__clang__)
  109. __attribute__ ((format (printf, 2, 3)))
  110. #endif
  111. ;
  112. #endif