printf-parse.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Parse printf format string.
  2. Copyright (C) 1999, 2002-2003, 2005, 2007, 2010-2022 Free Software
  3. Foundation, Inc.
  4. This file is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. This file is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _PRINTF_PARSE_H
  15. #define _PRINTF_PARSE_H
  16. /* This file can be parametrized with the following macros:
  17. ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
  18. STATIC Set to 'static' to declare the function static. */
  19. #if HAVE_FEATURES_H
  20. # include <features.h> /* for __GLIBC__, __UCLIBC__ */
  21. #endif
  22. #include "printf-args.h"
  23. /* Flags */
  24. #define FLAG_GROUP 1 /* ' flag */
  25. #define FLAG_LEFT 2 /* - flag */
  26. #define FLAG_SHOWSIGN 4 /* + flag */
  27. #define FLAG_SPACE 8 /* space flag */
  28. #define FLAG_ALT 16 /* # flag */
  29. #define FLAG_ZERO 32
  30. #if __GLIBC__ >= 2 && !defined __UCLIBC__
  31. # define FLAG_LOCALIZED 64 /* I flag, uses localized digits */
  32. #endif
  33. /* arg_index value indicating that no argument is consumed. */
  34. #define ARG_NONE (~(size_t)0)
  35. /* xxx_directive: A parsed directive.
  36. xxx_directives: A parsed format string. */
  37. /* Number of directly allocated directives (no malloc() needed). */
  38. #define N_DIRECT_ALLOC_DIRECTIVES 7
  39. /* A parsed directive. */
  40. typedef struct
  41. {
  42. const char* dir_start;
  43. const char* dir_end;
  44. int flags;
  45. const char* width_start;
  46. const char* width_end;
  47. size_t width_arg_index;
  48. const char* precision_start;
  49. const char* precision_end;
  50. size_t precision_arg_index;
  51. char conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  52. size_t arg_index;
  53. }
  54. char_directive;
  55. /* A parsed format string. */
  56. typedef struct
  57. {
  58. size_t count;
  59. char_directive *dir;
  60. size_t max_width_length;
  61. size_t max_precision_length;
  62. char_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  63. }
  64. char_directives;
  65. #if ENABLE_UNISTDIO
  66. /* A parsed directive. */
  67. typedef struct
  68. {
  69. const uint8_t* dir_start;
  70. const uint8_t* dir_end;
  71. int flags;
  72. const uint8_t* width_start;
  73. const uint8_t* width_end;
  74. size_t width_arg_index;
  75. const uint8_t* precision_start;
  76. const uint8_t* precision_end;
  77. size_t precision_arg_index;
  78. uint8_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  79. size_t arg_index;
  80. }
  81. u8_directive;
  82. /* A parsed format string. */
  83. typedef struct
  84. {
  85. size_t count;
  86. u8_directive *dir;
  87. size_t max_width_length;
  88. size_t max_precision_length;
  89. u8_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  90. }
  91. u8_directives;
  92. /* A parsed directive. */
  93. typedef struct
  94. {
  95. const uint16_t* dir_start;
  96. const uint16_t* dir_end;
  97. int flags;
  98. const uint16_t* width_start;
  99. const uint16_t* width_end;
  100. size_t width_arg_index;
  101. const uint16_t* precision_start;
  102. const uint16_t* precision_end;
  103. size_t precision_arg_index;
  104. uint16_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  105. size_t arg_index;
  106. }
  107. u16_directive;
  108. /* A parsed format string. */
  109. typedef struct
  110. {
  111. size_t count;
  112. u16_directive *dir;
  113. size_t max_width_length;
  114. size_t max_precision_length;
  115. u16_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  116. }
  117. u16_directives;
  118. /* A parsed directive. */
  119. typedef struct
  120. {
  121. const uint32_t* dir_start;
  122. const uint32_t* dir_end;
  123. int flags;
  124. const uint32_t* width_start;
  125. const uint32_t* width_end;
  126. size_t width_arg_index;
  127. const uint32_t* precision_start;
  128. const uint32_t* precision_end;
  129. size_t precision_arg_index;
  130. uint32_t conversion; /* d i o u x X f F e E g G a A c s p n U % but not C S */
  131. size_t arg_index;
  132. }
  133. u32_directive;
  134. /* A parsed format string. */
  135. typedef struct
  136. {
  137. size_t count;
  138. u32_directive *dir;
  139. size_t max_width_length;
  140. size_t max_precision_length;
  141. u32_directive direct_alloc_dir[N_DIRECT_ALLOC_DIRECTIVES];
  142. }
  143. u32_directives;
  144. #endif
  145. /* Parses the format string. Fills in the number N of directives, and fills
  146. in directives[0], ..., directives[N-1], and sets directives[N].dir_start
  147. to the end of the format string. Also fills in the arg_type fields of the
  148. arguments and the needed count of arguments. */
  149. #if ENABLE_UNISTDIO
  150. extern int
  151. ulc_printf_parse (const char *format, char_directives *d, arguments *a);
  152. extern int
  153. u8_printf_parse (const uint8_t *format, u8_directives *d, arguments *a);
  154. extern int
  155. u16_printf_parse (const uint16_t *format, u16_directives *d,
  156. arguments *a);
  157. extern int
  158. u32_printf_parse (const uint32_t *format, u32_directives *d,
  159. arguments *a);
  160. #else
  161. # ifdef STATIC
  162. STATIC
  163. # else
  164. extern
  165. # endif
  166. int printf_parse (const char *format, char_directives *d, arguments *a);
  167. #endif
  168. #endif /* _PRINTF_PARSE_H */