Changeset 80:c777d0a94cea


Ignore:
Timestamp:
Oct 28, 2008, 6:22:54 PM (16 years ago)
Author:
fnevgeny
Branch:
default
Phase:
public
Message:

Update to v. 0.4 of sprintf() for JS.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • source/class/pf/util/Format.js

    r79 r80  
    11/*
    2  * Based on sprintf() for JS v.0.1 by Alexandru Marasteanu
     2 * Based on sprintf() for JS v.0.4 by Alexandru Marasteanu
    33 * <http://alexei.417.ro/>
    44 */
    5 
    65
    76qx.Class.define("pf.util.Format",
     
    1716        sprintf : function()
    1817        {
    19             var i = 0, f = arguments[i++], o = [], m, a, p;
    20             while (f) {
    21                 if (m = /^[^\x25]+/.exec(f)) {
    22                     o.push(m[0]);
    23                 } else
    24                 if (m = /^\x25{2}/.exec(f)) {
    25                     o.push('%');
    26                 } else
    27                 if (m = /^\x25(\+)?(0|'[^$])?(-)?(\d+)?(\.\d+)?([b-fosuxX])/.exec(f)) {
    28                     if (!(a = arguments[i++])) {
    29                         throw("Too few arguments.");
    30                     }
    31                     if (/[^s]/.test(m[6]) && (typeof(a) != 'number')) {
    32                         throw("Expecting number but found " + typeof(a));
    33                     }
    34                     switch (m[6]) {
    35                     case 'b':
    36                         a = a.toString(2);
    37                         break;
    38                     case 'c':
    39                         a = String.fromCharCode(a);
    40                         break;
    41                     case 'd':
    42                         a = parseInt(a);
    43                         break;
    44                     case 'e':
    45                         a = m[5] ? a.toExponential(m[5].charAt(1)) : a.toExponential();
    46                         break;
    47                     case 'f':
    48                         a = m[5] ? parseFloat(a).toFixed(m[5].charAt(1)) : parseFloat(a);
    49                         break;
    50                     case 'o':
    51                         a = a.toString(8); break;
    52                     case 's':
    53                         a = ((a = String(a)) && m[5] ? a.substring(0, m[5].charAt(1)) : a);
    54                         break;
    55                     case 'u':
    56                         a = Math.abs(a);
    57                         break;
    58                     case 'x':
    59                         a = a.toString(16);
    60                         break;
    61                     case 'X':
    62                         a = a.toString(16).toUpperCase();
    63                         break;
    64                     }
    65                     a = (/[def]/.test(m[6]) && m[1] && a > 0 ? '+' + a : a);
    66                     p = m[4] ? this._strRepeat(m[2] ? m[2] == '0' ? '0' : m[2].charAt(1) : ' ', m[5] ? m[4] - String(a).length : m[4]) : '';
    67                     o.push(m[3] ? a + p : p + a);
    68                 } else {
    69                     throw("Huh ?");
     18            var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
     19            while (f) {
     20              if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
     21              else if (m = /^\x25{2}/.exec(f)) o.push('%');
     22              else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
     23                if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
     24                if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
     25                  throw("Expecting number but found " + typeof(a));
     26                switch (m[7]) {
     27                  case 'b': a = a.toString(2); break;
     28                  case 'c': a = String.fromCharCode(a); break;
     29                  case 'd': a = parseInt(a); break;
     30                  case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
     31                  case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
     32                  case 'o': a = a.toString(8); break;
     33                  case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
     34                  case 'u': a = Math.abs(a); break;
     35                  case 'x': a = a.toString(16); break;
     36                  case 'X': a = a.toString(16).toUpperCase(); break;
    7037                }
    71                 f = f.substring(m[0].length);
    72             }
    73             return(o.join(''));
     38                a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
     39                c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
     40                x = m[5] - String(a).length;
     41                p = m[5] ? this._strRepeat(c, x) : '';
     42                o.push(m[4] ? a + p : p + a);
     43              }
     44              else throw ("Huh ?!");
     45              f = f.substring(m[0].length);
     46            }
     47            return o.join('');
    7448        },
    7549       
Note: See TracChangeset for help on using the changeset viewer.