You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

39 lines
1.3 KiB

name: duk_push_sprintf
proto: |
const char *duk_push_sprintf(duk_context *ctx, const char *fmt, ...);
stack: |
[ ... ] -> [ ... str! ]
summary: |
<p>Format a string like <code>sprintf()</code> (but safely) and push the result
to the value stack. Returns a non-<code>NULL</code> pointer to the resulting
string.</p>
<p>If <code>fmt</code> is <code>NULL</code>, an empty string is pushed to the stack
and a non-<code>NULL</code> pointer to an empty string is returned (this
behavior mimics what <code>sprintf()</code> does for a <code>NULL</code> format
string, at least on Linux). The returned pointer can be dereferenced and a
NUL terminator character is guaranteed.</p>
<p>Unlike <code>sprintf()</code> the string formatting is safe with respect
to result length. Concretely, the implementation will try increasing temporary
buffer sizes until a large enough buffer is found for the temporary formatted
value.</p>
<div class="note">
There may be platform specific behavior for some format specifiers. For
example, the behavior of <code>%s</code> with a <code>NULL</code> argument
has officially undefined behavior, so actual behavior may vary across
platforms and may even be memory unsafe.
</div>
example: |
duk_push_sprintf(ctx, "meaning of life: %d, name: %s", 42, "Zaphod");
tags:
- stack
- string
introduced: 1.0.0