1 // Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
2 // 2007, Petr Baudis <pasky@suse.cz>
3 // 2008-2011, Jakub Narebski <jnareb@gmail.com>
6 * @fileOverview Generic JavaScript code (helper functions)
7 * @license GPLv2 or later
11 /* ============================================================ */
12 /* ............................................................ */
16 * pad INPUT on the left with STR that is assumed to have visible
17 * width of single character (for example nonbreakable spaces),
20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
21 * ('\u00A0' is nonbreakable space)
23 * @param {Number|String} input: number to pad
24 * @param {Number} width: visible width of output
25 * @param {String} str: string to prefix to string, defaults to '\u00A0'
26 * @returns {String} INPUT prefixed with STR x (WIDTH - INPUT.length)
28 function padLeftStr(input, width, str) {
30 if (typeof str === 'undefined') {
31 ch = '\u00A0'; // using ' ' doesn't work in all browsers
34 width -= input.toString().length;
39 return prefix + input;
43 * Pad INPUT on the left to WIDTH, using given padding character CH,
44 * for example padLeft('a', 3, '_') is '__a'
45 * padLeft(4, 2) is '04' (same as padLeft(4, 2, '0'))
47 * @param {String} input: input value converted to string.
48 * @param {Number} width: desired length of output.
49 * @param {String} ch: single character to prefix to string, defaults to '0'.
51 * @returns {String} Modified string, at least SIZE length.
53 function padLeft(input, width, ch) {
55 if (typeof ch === 'undefined') {
59 while (s.length < width) {
66 /* ............................................................ */
70 * Create XMLHttpRequest object in cross-browser way
71 * @returns XMLHttpRequest object, or null
73 function createRequestObject() {
75 return new XMLHttpRequest();
78 return window.createRequest();
81 return new ActiveXObject("Msxml2.XMLHTTP");
84 return new ActiveXObject("Microsoft.XMLHTTP");
91 /* ............................................................ */
92 /* unquoting/unescaping filenames */
97 var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
98 var octEscRe = /^[0-7]{1,3}$/;
99 var maybeQuotedRe = /^\"(.*)\"$/;
103 * unquote maybe C-quoted filename (as used by git, i.e. it is
104 * in double quotes '"' if there is any escape character used)
105 * e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
107 * @param {String} str: git-quoted string
108 * @returns {String} Unquoted and unescaped string
110 * @globals escCodeRe, octEscRe, maybeQuotedRe
112 function unquote(str) {
115 // character escape codes, aka escape sequences (from C)
116 // replacements are to some extent JavaScript specific
117 t: "\t", // tab (HT, TAB)
118 n: "\n", // newline (NL)
119 r: "\r", // return (CR)
120 f: "\f", // form feed (FF)
121 b: "\b", // backspace (BS)
122 a: "\x07", // alarm (bell) (BEL)
123 e: "\x1B", // escape (ESC)
124 v: "\v" // vertical tab (VT)
127 if (seq.search(octEscRe) !== -1) {
128 // octal char sequence
129 return String.fromCharCode(parseInt(seq, 8));
130 } else if (seq in es) {
131 // C escape sequence, aka character escape code
134 // quoted ordinary character
138 var match = str.match(maybeQuotedRe);
141 // perhaps str = eval('"'+str+'"'); would be enough?
142 str = str.replace(escCodeRe,
143 function (substr, p1, offset, s) { return unq(p1); });
148 /* end of common-lib.js */