11/**
2- sprintf() for JavaScript 0.5
2+ sprintf() for JavaScript 0.6
33
44Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
55All rights reserved.
@@ -40,11 +40,17 @@ Changelog:
4040 - bug fix: 0 is now preceeded with a + sign
4141 - bug fix: the sign was not at the right position on padded results (Kamal Abdali)
4242 - switched from GPL to BSD license
43+ 2010.05.22 - 0.6:
44+ - reverted to 0.4 and fixed the bug regarding the sign of the number 0
45+ Note:
46+ Thanks to Raphael Pigulla <raph (at] n3rd [dot) org> (http://www.n3rd.org/)
47+ who warned me about a bug in 0.5, I discovered that the last update was
48+ a regress. I appologize for that.
4349**/
4450
4551function str_repeat ( i , m ) {
4652for ( var o = [ ] ; m > 0 ; o [ -- m ] = i ) ;
47- return ( o . join ( "" ) ) ;
53+ return o . join ( '' ) ;
4854}
4955
5056function sprintf ( ) {
@@ -54,14 +60,14 @@ function sprintf() {
5460o . push ( m [ 0 ] ) ;
5561}
5662else if ( m = / ^ \x25 { 2 } / . exec ( f ) ) {
57- o . push ( "%" ) ;
63+ o . push ( '%' ) ;
5864}
5965else if ( m = / ^ \x25 (?: ( \d + ) \$ ) ? ( \+ ) ? ( 0 | ' [ ^ $ ] ) ? ( - ) ? ( \d + ) ? (?: \. ( \d + ) ) ? ( [ b - f o s u x X ] ) / . exec ( f ) ) {
6066if ( ( ( a = arguments [ m [ 1 ] || i ++ ] ) == null ) || ( a == undefined ) ) {
61- throw ( " Too few arguments." ) ;
67+ throw ( ' Too few arguments.' ) ;
6268}
63- if ( / [ ^ s ] / . test ( m [ 7 ] ) && ( typeof ( a ) != " number" ) ) {
64- throw ( " Expecting number but found" + typeof ( a ) ) ;
69+ if ( / [ ^ s ] / . test ( m [ 7 ] ) && ( typeof ( a ) != ' number' ) ) {
70+ throw ( ' Expecting number but found' + typeof ( a ) ) ;
6571}
6672switch ( m [ 7 ] ) {
6773case 'b' :a = a . toString ( 2 ) ; break ;
@@ -75,19 +81,16 @@ function sprintf() {
7581case 'x' :a = a . toString ( 16 ) ; break ;
7682case 'X' :a = a . toString ( 16 ) . toUpperCase ( ) ; break ;
7783}
78- if ( / [ d e f ] / . test ( m [ 7 ] ) ) {
79- s = ( a >= 0 ?( m [ 2 ] ?'+' :'' ) :'-' ) ;
80- a = Math . abs ( a ) ;
81- }
84+ a = ( / [ d e f ] / . test ( m [ 7 ] ) && m [ 2 ] && a >= 0 ?'+' + a :a ) ;
8285c = m [ 3 ] ?m [ 3 ] == '0' ?'0' :m [ 3 ] . charAt ( 1 ) :' ' ;
8386x = m [ 5 ] - String ( a ) . length - s . length ;
8487p = m [ 5 ] ?str_repeat ( c , x ) :'' ;
8588o . push ( s + ( m [ 4 ] ?a + p :p + a ) ) ;
8689}
8790else {
88- throw ( " Huh ?!" ) ;
91+ throw ( ' Huh ?!' ) ;
8992}
9093f = f . substring ( m [ 0 ] . length ) ;
9194}
92- return o . join ( "" ) ;
95+ return o . join ( '' ) ;
9396}