annotate static/sprintf.js @ 16:5a9b33dcdb2c default tip

Show how many samples as well as when the last sample was.
author Daniel O'Connor <darius@dons.net.au>
date Fri, 31 May 2013 13:36:26 +0930
parents a0213f0e707b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
1 /**
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
2 *
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
3 * Javascript sprintf
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
4 * http://www.webtoolkit.info/
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
5 *
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
6 *
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
7 **/
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
8
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
9 sprintfWrapper = {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
10 init : function () {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
11 if (typeof arguments == "undefined") { return null; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
12 if (arguments.length < 1) { return null; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
13 if (typeof arguments[0] != "string") { return null; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
14 if (typeof RegExp == "undefined") { return null; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
15
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
16 var string = arguments[0];
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
17 var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
18 var matches = new Array();
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
19 var strings = new Array();
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
20 var convCount = 0;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
21 var stringPosStart = 0;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
22 var stringPosEnd = 0;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
23 var matchPosEnd = 0;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
24 var newString = '';
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
25 var match = null;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
26
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
27 while (match = exp.exec(string)) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
28 if (match[9]) { convCount += 1; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
29
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
30 stringPosStart = matchPosEnd;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
31 stringPosEnd = exp.lastIndex - match[0].length;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
32 strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
33
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
34 matchPosEnd = exp.lastIndex;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
35 matches[matches.length] = {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
36 match: match[0],
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
37 left: match[3] ? true : false,
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
38 sign: match[4] || '',
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
39 pad: match[5] || ' ',
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
40 min: match[6] || 0,
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
41 precision: match[8],
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
42 code: match[9] || '%',
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
43 negative: parseInt(arguments[convCount]) < 0 ? true : false,
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
44 argument: String(arguments[convCount])
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
45 };
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
46 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
47 strings[strings.length] = string.substring(matchPosEnd);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
48
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
49 if (matches.length == 0) { return string; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
50 if ((arguments.length - 1) < convCount) { return null; }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
51
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
52 var code = null;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
53 var match = null;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
54 var i = null;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
55
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
56 for (i=0; i<matches.length; i++) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
57
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
58 if (matches[i].code == '%') { substitution = '%' }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
59 else if (matches[i].code == 'b') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
60 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
61 substitution = sprintfWrapper.convert(matches[i], true);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
62 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
63 else if (matches[i].code == 'c') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
64 matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
65 substitution = sprintfWrapper.convert(matches[i], true);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
66 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
67 else if (matches[i].code == 'd') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
68 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
69 substitution = sprintfWrapper.convert(matches[i]);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
70 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
71 else if (matches[i].code == 'f') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
72 matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
73 substitution = sprintfWrapper.convert(matches[i]);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
74 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
75 else if (matches[i].code == 'o') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
76 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
77 substitution = sprintfWrapper.convert(matches[i]);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
78 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
79 else if (matches[i].code == 's') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
80 matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
81 substitution = sprintfWrapper.convert(matches[i], true);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
82 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
83 else if (matches[i].code == 'x') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
84 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
85 substitution = sprintfWrapper.convert(matches[i]);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
86 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
87 else if (matches[i].code == 'X') {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
88 matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
89 substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
90 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
91 else {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
92 substitution = matches[i].match;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
93 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
94
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
95 newString += strings[i];
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
96 newString += substitution;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
97
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
98 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
99 newString += strings[i];
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
100
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
101 return newString;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
102
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
103 },
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
104
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
105 convert : function(match, nosign){
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
106 if (nosign) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
107 match.sign = '';
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
108 } else {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
109 match.sign = match.negative ? '-' : match.sign;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
110 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
111 var l = match.min - match.argument.length + 1 - match.sign.length;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
112 var pad = new Array(l < 0 ? 0 : l).join(match.pad);
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
113 if (!match.left) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
114 if (match.pad == "0" || nosign) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
115 return match.sign + pad + match.argument;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
116 } else {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
117 return pad + match.sign + match.argument;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
118 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
119 } else {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
120 if (match.pad == "0" || nosign) {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
121 return match.sign + match.argument + pad.replace(/0/g, ' ');
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
122 } else {
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
123 return match.sign + match.argument + pad;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
124 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
125 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
126 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
127 }
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
128
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
129 sprintf = sprintfWrapper.init;
a0213f0e707b - Update for v12.10
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
130