PageSourceSearch

https://picam360.github.io/picam360-viewer/js/printf.js?cache=no

js picam360.github.io collected 2026-10-03 09:46:02 UTC 8,714 bytes, 395 lines download raw bytes

1// printf.js - version 1.1.0
2//
3//  Copyright (C) 2000-2002  Masanao Izumo <[email protected]>
4//
5//  This library is free software; you can redistribute it and/or
6//  modify it under the terms of the GNU Lesser General Public
7//  License as published by the Free Software Foundation; either
8//  version 2.1 of the License, or (at your option) any later version.
9//
10//  This library is distributed in the hope that it will be useful,
11//  but WITHOUT ANY WARRANTY; without even the implied warranty of
12//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13//  Lesser General Public License for more details.
14//
15//  You should have received a copy of the GNU Lesser General Public
16//  License along with this library; if not, write to the Free Software
17//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18//
19//
20// SYNOPSIS:
21//   printf("format", ...);
22//   str = sprintf("format", ...);
23//
24// Chages:
25// 2002-02-04  Masanao Izumo <[email protected]>
26//             - Fixed bug about sprintf("%%") will return "%%".
27//             - Evaluate undefined "%" argument.  That is:
28//                   numerical value ===> 0	(%d, %x, %o, etc)
29//                   string value    ===> ''	(%s)
30
31// printf(format, ...);
32function printf() {
33	document.write(va_sprintf(printf.arguments));
34}
35
36// str = sprintf(format, ...);
37function sprintf() {
38	return va_sprintf(sprintf.arguments);
39}
40
41function va_sprintf(args) {
42	var ch;
43	var value;
44	var longflag;
45	var ljust;
46	var len, llen;
47	var zpad;
48	var p;
49	var output;
50	var format_index, arg_index;
51	var argc, argv;
52	var specin;
53	var format;
54
55	output = '';
56	format_index = 0;
57	arg_index = 1;
58	argv = args;
59	argc = args.length;
60	format = args[0];
61
62	while (format_index < format.length) {
63		ch = format.substr(format_index++, 1);
64		if (ch != '%' || format_index == format.length) {
65			output += ch;
66		} else {
67			// ch == '%'
68			ljust = len = zpad = longflag = 0;
69			llen = -1;
70			p = format_index;
71			specin = true;
72
73			while (specin) {
74				ch = format.substr(format_index++, 1);
75				switch (ch) {
76					case '-' :
77						ljust = 1;
78						continue;
79
80					case '0' : // set zero padding if len not set
81						if (len == 0)
82							zpad = 1;
83						// FALLTHROUGH
84					case '1' :
85					case '2' :
86					case '3' :
87					case '4' :
88					case '5' :
89					case '6' :
90					case '7' :
91					case '8' :
92					case '9' :
93						len = len * 10 + parseInt(ch);
94						continue;
95
96					case '.' :
97						llen = len;
98						len = 0;
99						continue;
100
101					case '*' :
102						if (arg_index < argc)
103							len = parseInt(argv[arg_index++]);
104						else
105							len = 0;
106						if (len < 0) {
107							ljust = 1;
108							len = -len;
109						}
110						continue;
111
112					case 'l' :
113						longflag = 1;
114						continue;
115
116					case 'u' :
117					case 'U' :
118						if (arg_index < argc) {
119							if (longflag) {
120								value = parseInt(argv[arg_index++]);
121							} else {
122								value = parseInt(argv[arg_index++]);
123								value %= 4294967296;
124							}
125						} else {
126							value = 0;
127						}
128						output += _dopr_fmtnum(value, 10, 0, ljust, len, zpad);
129						break;
130
131					case 'o' :
132					case 'O' :
133						if (arg_index < argc) {
134							if (longflag) {
135								value = parseInt(argv[arg_index++]);
136							} else {
137								value = parseInt(argv[arg_index++]);
138								value %= 4294967296;
139							}
140						} else {
141							value = 0;
142						}
143						output += _dopr_fmtnum(value, 8, 0, ljust, len, zpad);
144						break;
145
146					case 'f' :
147						if (arg_index < argc) {
148							value = parseFloat(argv[arg_index++]);
149						} else {
150							value = 0;
151						}
152						if (llen < 0) {
153							output += value;
154						} else {
155							output += value.toFixed(len);
156						}
157						break;
158
159					case 'd' :
160					case 'D' :
161						if (arg_index < argc) {
162							if (longflag) {
163								value = parseInt(argv[arg_index++]);
164							} else {
165								value = parseInt(argv[arg_index++]);
166								value %= 4294967296;
167							}
168						} else {
169							value = 0;
170						}
171						output += _dopr_fmtnum(value, 10, 1, ljust, len, zpad);
172						break;
173
174					case 'x' :
175						if (arg_index < argc) {
176							if (longflag) {
177								value = parseInt(argv[arg_index++]);
178							} else {
179								value = parseInt(argv[arg_index++]);
180								value %= 4294967296;
181							}
182						} else {
183							value = 0;
184						}
185						output += _dopr_fmtnum(value, 16, 0, ljust, len, zpad);
186						break;
187
188					case 'X' :
189						if (arg_index < argc) {
190							if (longflag) {
191								value = parseInt(argv[arg_index++]);
192							} else {
193								value = parseInt(argv[arg_index++]);
194								value %= 4294967296;
195							}
196						} else {
197							value = 0;
198						}
199						output += _dopr_fmtnum(value, -16, 0, ljust, len, zpad);
200						break;
201
202					case 's' :
203						if (arg_index < argc) {
204							value = argv[arg_index++];
205							if (value == null)
206								value = "(null)";
207							else
208								value = value + ""; // toString
209						} else {
210							value = '';
211						}
212						output += _dopr_fmtstr(value, ljust, len, llen);
213						break;
214
215					case 'c' :
216						if (arg_index < argc) {
217							value = parseInt(argv[arg_index++]);
218						} else {
219							value = 0;
220						}
221						output += _dopr_fromCharCode(value);
222						break;
223
224					case '%' :
225						output += '%';
226						break;
227
228					/*
229					 * Not supported case 'f': case 'e': case 'E': case 'g':
230					 * case 'G': if (arg_index < argc) { value =
231					 * argv[arg_index++]; } else { value = 0.0; } output +=
232					 * _dopr_fmtdouble(format.substr(p, format_index - p),
233					 * value); break;
234					 */
235
236					default :
237						if (p + 1 == format_index) {
238							output += '%';
239							output += ch;
240						} else {
241							// alert("format error: " + format);
242						}
243						break;
244				}
245				specin = false;
246			}
247		}
248	}
249	return output;
250}
251
252// Private function
253function _dopr_fmtnum(value, base, dosign, ljust, len, zpad) {
254	var signvalue = '';
255	var uvalue;
256	var place = 0;
257	var padlen; // amount to pad
258	var caps = 0;
259	var convert;
260	var output;
261
262	convert = '';
263	output = '';
264
265	if (value >= 0)
266		uvalue = value;
267	else
268		uvalue = (value % 4294967296) + 4294967296;
269
270	if (dosign) {
271		if (value < 0) {
272			signvalue = '-';
273			uvalue = -value;
274		}
275	}
276
277	if (base < 0) {
278		caps = 1;
279		base = -base;
280	}
281
282	if (uvalue == 0) {
283		convert = '0';
284		place = 1;
285	} else {
286		while (uvalue) {
287			if (caps)
288				convert = '0123456789ABCDEF'.substr(uvalue % base, 1) + convert;
289			else
290				convert = '0123456789abcdef'.substr(uvalue % base, 1) + convert;
291			uvalue = parseInt(uvalue / base);
292			place++;
293		}
294	}
295
296	padlen = len - place;
297	if (padlen < 0)
298		padlen = 0;
299	if (ljust)
300		padlen = -padlen;
301
302	if (zpad && padlen > 0) {
303		if (signvalue) {
304			output += signvalue;
305			--padlen;
306			signvalue = 0;
307		}
308
309		while (padlen > 0) {
310			output += '0';
311			--padlen;
312		}
313	}
314
315	while (padlen > 0) {
316		output += ' ';
317		--padlen;
318	}
319	if (signvalue) {
320		output += signvalue;
321	}
322
323	output += convert;
324
325	while (padlen < 0) {
326		output += ' ';
327		++padlen;
328	}
329	return output;
330}
331
332// Private function
333function _dopr_fmtstr(value, ljust, field_len, llen) {
334	var padlen; // amount to pad
335	var slen, truncstr = 0;
336	var output = '';
337
338	slen = value.length;
339
340	if (llen != -1) {
341		var rlen;
342
343		rlen = field_len;
344		if (slen > rlen) {
345			truncstr = 1;
346			slen = rlen;
347		}
348		field_len = llen;
349	}
350	padlen = field_len - slen;
351
352	if (padlen < 0)
353		padlen = 0;
354	if (ljust)
355		padlen = -padlen;
356	while (padlen > 0) {
357		output += ' ';
358		--padlen;
359	}
360	if (truncstr) {
361		output += value.substr(0, slen);
362	} else {
363		output += value;
364	}
365
366	while (padlen < 0) {
367		output += ' ';
368		++padlen;
369	}
370	return output;
371}
372
373// Private function
374var _dopr_fromCharCode_chars = null;
375function _dopr_fromCharCode(code) {
376	if (String.fromCharCode)
377		return String.fromCharCode(code);
378	if (!_dopr_fromCharCode_chars)
379		_dopr_fromCharCode_chars = "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020"
380			+ "\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !\"#$%&"
381			+ "'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghi"
382			+ "jklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211"
383			+ "\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232"
384			+ "\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253"
385			+ "\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274"
386			+ "\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315"
387			+ "\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336"
388			+ "\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
389			+ "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377";
390	if (code < 0)
391		return "";
392	if (code <= 255)
393		return _dopr_fromCharCode_chars.substr(code, 1);
394	return eval(sprintf("\"\\u%04x\"", code));
395}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.