This repository was archived by the owner on Sep 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Comparasion of sprintf() implementations
GlitchMr edited this page Mar 17, 2012
·
2 revisions
You may have been wondering what sprint() gives compared to other sprintf() Node.js implementations (perhaps in future I will compare browser-side implementations, but those are harder to find...). This page will compare those. Note that it is in favor of sprintf() as it was written by author, but hopefully it's not biased. sprint() was inspired mainly by Perl implementation of sprintf(), so it contains many features of Perl version of sprintf().
Following function was used to help testing features:
test = ->
console.log "sprint: " + try require("sprint") arguments...
console.log "s: " + try require("s") arguments...
console.log "sprintf: " + try require("sprintf").sprintf arguments...
console.log "printf: " + try require("printf") arguments...
console.log "format: " + try require("format").format arguments...| Feature |
sprint
|
s
|
sprintf
|
format
|
printf
|
|---|---|---|---|---|---|
| Types | |||||
| %% | YES | Buggy | YES | YES | YES |
| %c | YES | No | YES | YES | YES |
| %s | YES | YES | YES | YES | YES |
| %d | YES | Buggy | YES | YES | YES |
| %u | YES | No | Kinda (Math.abs) | YES | No |
| %o | YES | No | YES | YES | YES (but prefix always appears) |
| %x | YES | No | YES | YES | YES (but prefix always appears) |
| %e | YES | No | YES | YES | No |
| %f | YES | No | YES | YES | YES |
| %g | YES | No | No | YES | No |
| %x | YES | No | YES | YES | YES (oddly, prefix never appears unlike %x and %o) |
| %p | No | No | No | No | No |
| %n | No | No | No | No | No |
| %i (alias for %d) | YES | No | No | YES | No |
| Format parameter index | |||||
| %1$ | YES | No | YES | YES | No |
| %(option_key) | No | No | YES | YES | No |
| Flags | |||||
| Space | YES | No | No | YES | No |
| + | YES | No | YES | YES | No |
| - | YES | No | YES | YES | No |
| # | YES | No | No | YES | No |
| Vector flag | |||||
| v | YES | No | No | No | No |
| Width | |||||
| Digit | YES | No | YES | YES | No |
| * (next argument) | YES | No | No | YES | No |
| Precision | |||||
| Digit | YES | No | YES | YES | No |
| * (next argument) | YES | No | No | YES | No |
| Size argument | |||||
| Size argument | YES | No | No | Ignored | No |
At the end, I wanted to note one of more interesting results in test. While it looks that printf is better than sprint, it's also somewhat more buggy. As for simple examples...
> test('<%*.*f>', 4, 1, 3.14)
sprint: < 3.1>
s: <%*.*f>
sprintf: undefined
printf: <4.000>
format: <*.*f>