1 /* Elementary Unicode string functions.
2 Copyright (C) 2002, 2005-2007, 2009-2024 Free Software Foundation, Inc.
4 This file is free software.
5 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
6 You can redistribute it and/or modify it under either
7 - the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation, either version 3, or (at your
9 option) any later version, or
10 - the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option)
13 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15 This file is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License and the GNU General Public License
21 You should have received a copy of the GNU Lesser General Public
22 License and of the GNU General Public License along with this
23 program. If not, see <https://www.gnu.org/licenses/>. */
43 /* These work like the printf function family.
45 The format directive 'U' takes an UTF-8 string (const uint8_t *).
46 The format directive 'lU' takes an UTF-16 string (const uint16_t *).
47 The format directive 'llU' takes an UTF-32 string (const uint32_t *).
49 The prefix (ulc_, u8_, u16_, u16_) indicates the type of the resulting
50 string. The prefix 'ulc' stands for "locale encoded".
52 An infix 'v' indicates that a va_list is passed instead of multiple
55 The functions *sprintf have a 'buf' argument that is assumed to be large
56 enough. (DANGEROUS! Overflowing the buffer will crash the program.)
57 The functions *snprintf have a 'buf' argument that is assumed to be 'size'
58 units large. (DANGEROUS! The resulting string might be truncated in the
59 middle of a multibyte character.)
60 The functions *asprintf have a 'resultp' argument. The result will be
61 freshly allocated and stored in *resultp.
62 The functions *asnprintf have a (resultbuf, lengthp) argument pair. If
63 resultbuf is not NULL and the result fits into *lengthp units, it is put
64 in resultbuf, and resultbuf is returned. Otherwise, a freshly allocated
65 string is returned. In both cases, *lengthp is set to the length (number
66 of units) of the returned string. In case of error, NULL is returned and
70 /* ASCII format string, result in locale dependent encoded 'char *'. */
72 ulc_sprintf (char *_UC_RESTRICT buf
,
73 const char *format
, ...);
75 ulc_snprintf (char *_UC_RESTRICT buf
, size_t size
,
76 const char *format
, ...);
78 ulc_asprintf (char **resultp
,
79 const char *format
, ...);
81 ulc_asnprintf (char *_UC_RESTRICT resultbuf
, size_t *lengthp
,
82 const char *format
, ...);
84 ulc_vsprintf (char *_UC_RESTRICT buf
,
85 const char *format
, va_list ap
);
87 ulc_vsnprintf (char *_UC_RESTRICT buf
, size_t size
,
88 const char *format
, va_list ap
);
90 ulc_vasprintf (char **resultp
,
91 const char *format
, va_list ap
);
93 ulc_vasnprintf (char *_UC_RESTRICT resultbuf
, size_t *lengthp
,
94 const char *format
, va_list ap
);
96 /* ASCII format string, result in UTF-8 format. */
98 u8_sprintf (uint8_t *buf
,
99 const char *format
, ...);
101 u8_snprintf (uint8_t *buf
, size_t size
,
102 const char *format
, ...);
104 u8_asprintf (uint8_t **resultp
,
105 const char *format
, ...);
107 u8_asnprintf (uint8_t *resultbuf
, size_t *lengthp
,
108 const char *format
, ...);
110 u8_vsprintf (uint8_t *buf
,
111 const char *format
, va_list ap
);
113 u8_vsnprintf (uint8_t *buf
, size_t size
,
114 const char *format
, va_list ap
);
116 u8_vasprintf (uint8_t **resultp
,
117 const char *format
, va_list ap
);
119 u8_vasnprintf (uint8_t *resultbuf
, size_t *lengthp
,
120 const char *format
, va_list ap
);
122 /* UTF-8 format string, result in UTF-8 format. */
124 u8_u8_sprintf (uint8_t *_UC_RESTRICT buf
,
125 const uint8_t *format
, ...);
127 u8_u8_snprintf (uint8_t *_UC_RESTRICT buf
, size_t size
,
128 const uint8_t *format
, ...);
130 u8_u8_asprintf (uint8_t **resultp
,
131 const uint8_t *format
, ...);
133 u8_u8_asnprintf (uint8_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
134 const uint8_t *format
, ...);
136 u8_u8_vsprintf (uint8_t *_UC_RESTRICT buf
,
137 const uint8_t *format
, va_list ap
);
139 u8_u8_vsnprintf (uint8_t *_UC_RESTRICT buf
, size_t size
,
140 const uint8_t *format
, va_list ap
);
142 u8_u8_vasprintf (uint8_t **resultp
,
143 const uint8_t *format
, va_list ap
);
145 u8_u8_vasnprintf (uint8_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
146 const uint8_t *format
, va_list ap
);
148 /* ASCII format string, result in UTF-16 format. */
150 u16_sprintf (uint16_t *buf
,
151 const char *format
, ...);
153 u16_snprintf (uint16_t *buf
, size_t size
,
154 const char *format
, ...);
156 u16_asprintf (uint16_t **resultp
,
157 const char *format
, ...);
159 u16_asnprintf (uint16_t *resultbuf
, size_t *lengthp
,
160 const char *format
, ...);
162 u16_vsprintf (uint16_t *buf
,
163 const char *format
, va_list ap
);
165 u16_vsnprintf (uint16_t *buf
, size_t size
,
166 const char *format
, va_list ap
);
168 u16_vasprintf (uint16_t **resultp
,
169 const char *format
, va_list ap
);
171 u16_vasnprintf (uint16_t *resultbuf
, size_t *lengthp
,
172 const char *format
, va_list ap
);
174 /* UTF-16 format string, result in UTF-16 format. */
176 u16_u16_sprintf (uint16_t *_UC_RESTRICT buf
,
177 const uint16_t *format
, ...);
179 u16_u16_snprintf (uint16_t *_UC_RESTRICT buf
, size_t size
,
180 const uint16_t *format
, ...);
182 u16_u16_asprintf (uint16_t **resultp
,
183 const uint16_t *format
, ...);
185 u16_u16_asnprintf (uint16_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
186 const uint16_t *format
, ...);
188 u16_u16_vsprintf (uint16_t *_UC_RESTRICT buf
,
189 const uint16_t *format
, va_list ap
);
191 u16_u16_vsnprintf (uint16_t *_UC_RESTRICT buf
, size_t size
,
192 const uint16_t *format
, va_list ap
);
194 u16_u16_vasprintf (uint16_t **resultp
,
195 const uint16_t *format
, va_list ap
);
197 u16_u16_vasnprintf (uint16_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
198 const uint16_t *format
, va_list ap
);
200 /* ASCII format string, result in UTF-32 format. */
202 u32_sprintf (uint32_t *buf
,
203 const char *format
, ...);
205 u32_snprintf (uint32_t *buf
, size_t size
,
206 const char *format
, ...);
208 u32_asprintf (uint32_t **resultp
,
209 const char *format
, ...);
211 u32_asnprintf (uint32_t *resultbuf
, size_t *lengthp
,
212 const char *format
, ...);
214 u32_vsprintf (uint32_t *buf
,
215 const char *format
, va_list ap
);
217 u32_vsnprintf (uint32_t *buf
, size_t size
,
218 const char *format
, va_list ap
);
220 u32_vasprintf (uint32_t **resultp
,
221 const char *format
, va_list ap
);
223 u32_vasnprintf (uint32_t *resultbuf
, size_t *lengthp
,
224 const char *format
, va_list ap
);
226 /* UTF-32 format string, result in UTF-32 format. */
228 u32_u32_sprintf (uint32_t *_UC_RESTRICT buf
,
229 const uint32_t *format
, ...);
231 u32_u32_snprintf (uint32_t *_UC_RESTRICT buf
, size_t size
,
232 const uint32_t *format
, ...);
234 u32_u32_asprintf (uint32_t **resultp
,
235 const uint32_t *format
, ...);
237 u32_u32_asnprintf (uint32_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
238 const uint32_t *format
, ...);
240 u32_u32_vsprintf (uint32_t *_UC_RESTRICT buf
,
241 const uint32_t *format
, va_list ap
);
243 u32_u32_vsnprintf (uint32_t *_UC_RESTRICT buf
, size_t size
,
244 const uint32_t *format
, va_list ap
);
246 u32_u32_vasprintf (uint32_t **resultp
,
247 const uint32_t *format
, va_list ap
);
249 u32_u32_vasnprintf (uint32_t *_UC_RESTRICT resultbuf
, size_t *lengthp
,
250 const uint32_t *format
, va_list ap
);
252 /* ASCII format string, output to FILE in locale dependent encoding. */
254 ulc_fprintf (FILE *stream
,
255 const char *format
, ...);
257 ulc_vfprintf (FILE *stream
,
258 const char *format
, va_list ap
);
264 #endif /* _UNISTDIO_H */