d6b0c0d46ae8a9b9a7ddd187a38f4100e69a5068
   1// Copyright (C) 2007, Fredrik Kuivinen <frekui@gmail.com>
   2//               2007, Petr Baudis <pasky@suse.cz>
   3//          2008-2011, Jakub Narebski <jnareb@gmail.com>
   4
   5/**
   6 * @fileOverview Generic JavaScript code (helper functions)
   7 * @license GPLv2 or later
   8 */
   9
  10
  11/* ============================================================ */
  12/* ............................................................ */
  13/* Padding */
  14
  15/**
  16 * pad INPUT on the left with STR that is assumed to have visible
  17 * width of single character (for example nonbreakable spaces),
  18 * to WIDTH characters
  19 *
  20 * example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
  21 *          ('\u00A0' is nonbreakable space)
  22 *
  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)
  27 */
  28function padLeftStr(input, width, str) {
  29        var prefix = '';
  30        if (typeof str === 'undefined') {
  31                ch = '\u00A0'; // using '&nbsp;' doesn't work in all browsers
  32        }
  33
  34        width -= input.toString().length;
  35        while (width > 0) {
  36                prefix += str;
  37                width--;
  38        }
  39        return prefix + input;
  40}
  41
  42/**
  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'))
  46 *
  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'.
  50 *
  51 * @returns {String} Modified string, at least SIZE length.
  52 */
  53function padLeft(input, width, ch) {
  54        var s = input + "";
  55        if (typeof ch === 'undefined') {
  56                ch = '0';
  57        }
  58
  59        while (s.length < width) {
  60                s = ch + s;
  61        }
  62        return s;
  63}
  64
  65
  66/* ............................................................ */
  67/* Ajax */
  68
  69/**
  70 * Create XMLHttpRequest object in cross-browser way
  71 * @returns XMLHttpRequest object, or null
  72 */
  73function createRequestObject() {
  74        try {
  75                return new XMLHttpRequest();
  76        } catch (e) {}
  77        try {
  78                return window.createRequest();
  79        } catch (e) {}
  80        try {
  81                return new ActiveXObject("Msxml2.XMLHTTP");
  82        } catch (e) {}
  83        try {
  84                return new ActiveXObject("Microsoft.XMLHTTP");
  85        } catch (e) {}
  86
  87        return null;
  88}
  89
  90
  91/* ............................................................ */
  92/* unquoting/unescaping filenames */
  93
  94/**#@+
  95 * @constant
  96 */
  97var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
  98var octEscRe = /^[0-7]{1,3}$/;
  99var maybeQuotedRe = /^\"(.*)\"$/;
 100/**#@-*/
 101
 102/**
 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'
 106 *
 107 * @param {String} str: git-quoted string
 108 * @returns {String} Unquoted and unescaped string
 109 *
 110 * @globals escCodeRe, octEscRe, maybeQuotedRe
 111 */
 112function unquote(str) {
 113        function unq(seq) {
 114                var es = {
 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)
 125                };
 126
 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
 132                        return es[seq];
 133                }
 134                // quoted ordinary character
 135                return seq;
 136        }
 137
 138        var match = str.match(maybeQuotedRe);
 139        if (match) {
 140                str = match[1];
 141                // perhaps str = eval('"'+str+'"'); would be enough?
 142                str = str.replace(escCodeRe,
 143                        function (substr, p1, offset, s) { return unq(p1); });
 144        }
 145        return str;
 146}
 147
 148/* end of common-lib.js */