Hi, all! Just committed to the source tree: an implementation of sprintf() for JS. While much, in terms of string formatting, can be achieved via straightforward string concatenation in JS, sprintf() gives a bit more control in some situations (e.g. padding numbers and specifying the number of decimal places to output).
The back-end is a much-hacked version of the public domain printf() implementation from the sqlite3 source tree. i've been using a variant of it quite heavily in other projects for over a year and i believe it to work quite well. It also inherits some formatting extensions from its sqlite3 heritage, like escaping HTML and SQL, and URL encoding/ decoding. (Despite the sqlite3 source heritage, snprintf() does not need sqlite3 to work.) The source repo is available via: http://wanderinghorse.net/computing/javascript/v8/ To use this in your own code: the tarball i'll post on the above page in the next 1-2 hours will include this code (it'll have the version 20090304). It can be used standalone (it's independent of the other code in the tarball): fish out the files addons/v8-sprintf.cc and addons/include/v8/v8-sprintf.h. :) Example: {{{ var obj = {hi:32}; print(sprintf("hi, %s!","world")); print(sprintf("hi, %s!", obj)); print(sprintf("%d %05d", 42, 42)); }}} step...@jareth:~/cvs/fossil/v8-addons/addons$ ./shell js/sprintf.js hi, world! hi, [object Object]! 42 00042 i would very much appreciate it if someone skilled in the ways of v8's garbage collection would look it over: just the sprintf() function itself, not the ancient, C-oriented framework around it (grep for NEXT_ARG). i have an odd feeling i'm failing to follow some important memory guideline or other. When i just slap a HandleScope in it, i get segfaults when the function exits. Happy hacking! -- ----- stephan beal http://wanderinghorse.net/home/stephan/ --~--~---------~--~----~------------~-------~--~----~ v8-users mailing list [email protected] http://groups.google.com/group/v8-users -~----------~----~----~----~------~----~------~--~---
