Skip to content

Instantly share code, notes, and snippets.

@mattijs
Created March 1, 2012 10:03
Show Gist options
  • Save mattijs/1948666 to your computer and use it in GitHub Desktop.
Save mattijs/1948666 to your computer and use it in GitHub Desktop.
JavaScript printf
// GNU C printf for JavaScript
// http://www.gnu.org/software/libc/manual/html_mono/libc.html#Formatted-Output
//
// Syntax
// % [ param-no $] flags width . * [ param-no $] type conversion
// Parts
// -----
// Zero or more flag characters that modify the normal behavior of the conversion specification.
//
// '-' | Left-justify the result in the field, right-justification is the default
// '+' | For the signed '%d' and '%i' conversions, print a plus sign if the value is positive
// ' ' | For the signed '%d' and '%i' conversions, if the result doesn't start with a plus or minus sign, prefix it with a space character instead. Since the '+' flag ensures that the result includes a sign, this flag is ignored if you supply both of them
// '#' | For the '%o' conversion, this forces the leading digit to be '0', as if by increasing the precision. For '%x' or '%X', this prefixes a leading '0x' or '0X' (respectively) to the result. This doesn't do anything useful for the '%d', '%i', or '%u' conversions
// '0' | ad the field with zeros instead of spaces. The zeros are placed after any indication of sign or base. This flag is ignored if the '-' flag is also specified, or if a precision is specified
var flags = '(-|\\+|\\s|#|0)?';
// An optional decimal integer specifying the minimum field width. If the normal conversion
// produces fewer characters than this, the field is padded with spaces to the specified width.
// This is a minimum value; if the normal conversion produces more characters than this, the field
// is not truncated. Normally, the output is right-justified within the field. You can also
// specify a field width of '*'. This means that the next argument in the argument list
// (before the actual value to be printed) is used as the field width. The value must be an int.
// If the value is negative, this means to set the '-' flag (see below) and to use the absolute
// value as the field width.
var width = "(\\d+|\\*)?";
//An optional precision to specify the number of digits to be written for the numeric conversions.
//If the precision is specified, it consists of a period ('.') followed optionally by a decimal
//integer (which defaults to zero if omitted). You can also specify a precision of '*'. This means
//that the next argument in the argument list (before the actual value to be printed) is used as
//the precision. The value must be an int, and is ignored if it is negative. If you specify '*'
//for both the field width and precision, the field width argument precedes the precision argument.
//Other C library versions may not recognize this syntax.
var precision = "(\\.\\d+|\\*)?";
// An optional type modifier character, which is used to specify the data type of the corresponding
// argument if it differs from the default type. (For example, the integer conversions assume a
// type of int, but you can specify 'h', 'l', or 'L' for other integer types.)
var types = ['h{1,2}', 'j', 'l{1,2}', 'L', 'q', 't', 'z', 'Z'];
var type = '(' + types.join('|') + ')?';
// A character that specifies the conversion to be applied.
//
// d, i | Print an integer as a signed decimal number
// o | Print an integer as an unsigned octal number
// u | Print an integer as an unsigned decimal number
// x, X | Print an integer as an unsigned hexadecimal number. '%x' uses lower-case letters and '%X' uses upper-case
// f | Print a floating-point number in normal (fixed-point) notation
// e, E | Print a floating-point number in exponential notation. '%e' uses lower-case letters and '%E' uses upper-case
// g, G | Print a floating-point number in either normal or exponential notation, whichever is more appropriate for its magnitude. '%g' uses lower-case letters and '%G' uses upper-case
// a, A | Print a floating-point number in a hexadecimal fractional notation which the exponent to base 2 represented in decimal digits. '%a' uses lower-case letters and '%A' uses upper-case
// c | Print a single character
// C | This is an alias for '%lc' which is supported for compatibility with the Unix standard
// s | Print a string
// S | This is an alias for '%ls' which is supported for compatibility with the Unix standard
// p | Print the value of a pointer
// n | Get the number of characters printed so far. This prints nothing
// % | Print a literal '%' character
var conversions = ['d', 'i', 'o', 'u', 'x', 'X', 'f', 'e', 'E', 'g', 'G', 'a', 'A', 'c', 'C', 's', 'S', 'p', 'n', '\\%'];
var conversion = '(' + conversions.join('|') + ')';
// Build Regular Expression string
var rs = '\\%' + [flags, width, precision, type, conversion].join('');
// Create Regular Expression object
var r = new RegExp(rs, 'g');
// Will produce
// /\%(-|\+|\s|#|0)?(\d+|\*)?(\.\d+|\*)?(h{1,2}|j|l{1,2}|L|q|t|z|Z)?(d|i|o|u|x|X|f|e|E|g|G|a|A|c|C|s|S|p|n|\%)/g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment