MCPcopy Create free account
hub / github.com/buke/quickjs-go / js_printf_internal

Function js_printf_internal

deps/quickjs/quickjs-libc.c:235–449  ·  view source on GitHub ↗

__GNUC__ || __clang__

Source from the content-addressed store, hash-verified

233#pragma GCC diagnostic ignored "-Wformat-nonliteral"
234#endif // __GNUC__ || __clang__
235static JSValue js_printf_internal(JSContext *ctx,
236 int argc, JSValueConst *argv, FILE *fp)
237{
238 char fmtbuf[32];
239 uint8_t cbuf[UTF8_CHAR_LEN_MAX+1];
240 JSValue res;
241 DynBuf dbuf;
242 const char *fmt_str = NULL;
243 const uint8_t *fmt, *fmt_end;
244 const uint8_t *p;
245 char *q;
246 int i, c, len, mod;
247 size_t fmt_len;
248 int32_t int32_arg;
249 int64_t int64_arg;
250 double double_arg;
251 const char *string_arg;
252
253 js_std_dbuf_init(ctx, &dbuf);
254
255 if (argc > 0) {
256 fmt_str = JS_ToCStringLen(ctx, &fmt_len, argv[0]);
257 if (!fmt_str)
258 goto fail;
259
260 i = 1;
261 fmt = (const uint8_t *)fmt_str;
262 fmt_end = fmt + fmt_len;
263 while (fmt < fmt_end) {
264 for (p = fmt; fmt < fmt_end && *fmt != '%'; fmt++)
265 continue;
266 dbuf_put(&dbuf, p, fmt - p);
267 if (fmt >= fmt_end)
268 break;
269 q = fmtbuf;
270 *q++ = *fmt++; /* copy '%' */
271
272 /* flags */
273 for(;;) {
274 c = *fmt;
275 if (c == '0' || c == '#' || c == '+' || c == '-' || c == ' ' ||
276 c == '\'') {
277 if (q >= fmtbuf + sizeof(fmtbuf) - 1)
278 goto invalid;
279 *q++ = c;
280 fmt++;
281 } else {
282 break;
283 }
284 }
285 /* width */
286 if (*fmt == '*') {
287 if (i >= argc)
288 goto missing;
289 if (JS_ToInt32(ctx, &int32_arg, argv[i++]))
290 goto fail;
291 q += snprintf(q, fmtbuf + sizeof(fmtbuf) - q, "%d", int32_arg);
292 fmt++;

Callers 3

js_std_sprintfFunction · 0.85
js_std_printfFunction · 0.85
js_std_file_printfFunction · 0.85

Calls 12

js_std_dbuf_initFunction · 0.85
JS_ToCStringLenFunction · 0.85
dbuf_putFunction · 0.85
my_isdigitFunction · 0.85
JS_IsStringFunction · 0.85
JS_ToCStringFunction · 0.85
utf8_decodeFunction · 0.85
utf8_encodeFunction · 0.85
dbuf_printfFunction · 0.85
dbuf_putcFunction · 0.85
JS_NewInt32Function · 0.85
dbuf_freeFunction · 0.85

Tested by

no test coverage detected