@@ -17,7 +17,6 @@ function tryStringify(arg) {
17
17
}
18
18
}
19
19
20
- const formatRegExp = / % [ s d j % ] / g;
21
20
exports . format = function ( f ) {
22
21
if ( typeof f !== 'string' ) {
23
22
const objects = new Array ( arguments . length ) ;
@@ -27,30 +26,56 @@ exports.format = function(f) {
27
26
return objects . join ( ' ' ) ;
28
27
}
29
28
30
- if ( arguments . length === 1 ) return f ;
31
-
32
- const len = arguments . length ;
33
- const args = new Array ( len ) ;
34
- var i ;
35
- for ( i = 0 ; i < len ; i ++ ) {
36
- args [ i ] = arguments [ i ] ;
37
- }
38
-
39
- i = 1 ;
40
- var str = f . replace ( formatRegExp , function ( x ) {
41
- if ( x === '%%' ) return '%' ;
42
- if ( i >= len ) return x ;
43
- switch ( x ) {
44
- case '%s' : return String ( args [ i ++ ] ) ;
45
- case '%d' : return Number ( args [ i ++ ] ) ;
46
- case '%j' : return tryStringify ( args [ i ++ ] ) ;
47
- // falls through
48
- default :
49
- return x ;
29
+ var argLen = arguments . length ;
30
+
31
+ if ( argLen === 1 ) return f ;
32
+
33
+ var str = '' ;
34
+ var a = 1 ;
35
+ var lastPos = 0 ;
36
+ for ( var i = 0 ; i < f . length ; ) {
37
+ if ( f . charCodeAt ( i ) === 37 /*'%'*/ && i + 1 < f . length ) {
38
+ switch ( f . charCodeAt ( i + 1 ) ) {
39
+ case 100 : // 'd'
40
+ if ( a >= argLen )
41
+ break ;
42
+ if ( lastPos < i )
43
+ str += f . slice ( lastPos , i ) ;
44
+ str += Number ( arguments [ a ++ ] ) ;
45
+ lastPos = i = i + 2 ;
46
+ continue ;
47
+ case 106 : // 'j'
48
+ if ( a >= argLen )
49
+ break ;
50
+ if ( lastPos < i )
51
+ str += f . slice ( lastPos , i ) ;
52
+ str += tryStringify ( arguments [ a ++ ] ) ;
53
+ lastPos = i = i + 2 ;
54
+ continue ;
55
+ case 115 : // 's'
56
+ if ( a >= argLen )
57
+ break ;
58
+ if ( lastPos < i )
59
+ str += f . slice ( lastPos , i ) ;
60
+ str += String ( arguments [ a ++ ] ) ;
61
+ lastPos = i = i + 2 ;
62
+ continue ;
63
+ case 37 : // '%'
64
+ if ( lastPos < i )
65
+ str += f . slice ( lastPos , i ) ;
66
+ str += '%' ;
67
+ lastPos = i = i + 2 ;
68
+ continue ;
69
+ }
50
70
}
51
- } ) ;
52
- while ( i < len ) {
53
- const x = args [ i ++ ] ;
71
+ ++ i ;
72
+ }
73
+ if ( lastPos === 0 )
74
+ str = f ;
75
+ else if ( lastPos < f . length )
76
+ str += f . slice ( lastPos ) ;
77
+ while ( a < argLen ) {
78
+ const x = arguments [ a ++ ] ;
54
79
if ( x === null || ( typeof x !== 'object' && typeof x !== 'symbol' ) ) {
55
80
str += ' ' + x ;
56
81
} else {
0 commit comments