Changeset 31954 in webkit


Ignore:
Timestamp:
Apr 16, 2008 12:20:21 PM (16 years ago)
Author:
Adam Roben
Message:

Add String.format

String.format is a more generic form of String.vsprintf that takes a
set of formatter functions, one for each supported format specifier.
Each formatter function is passed the relevant token and substitution
value, and returns the formatted result.

Reviewed by Tim Hatcher.

  • page/inspector/utilities.js: (String.standardFormatters): Added. (String.vsprintf): Changed to call through to String.format. (String.format): Added. Most of the code came from String.vsprintf, except that we use the passed-in formatters to format the substitutions rather than hard-coding how to handle each format specifier.
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r31953 r31954  
     12008-04-15  Adam Roben  <aroben@apple.com>
     2
     3        Add String.format
     4
     5        String.format is a more generic form of String.vsprintf that takes a
     6        set of formatter functions, one for each supported format specifier.
     7        Each formatter function is passed the relevant token and substitution
     8        value, and returns the formatted result.
     9
     10        Reviewed by Tim Hatcher.
     11
     12        * page/inspector/utilities.js:
     13        (String.standardFormatters): Added.
     14        (String.vsprintf): Changed to call through to String.format.
     15        (String.format): Added. Most of the code came from String.vsprintf,
     16        except that we use the passed-in formatters to format the
     17        substitutions rather than hard-coding how to handle each format
     18        specifier.
     19
    1202008-04-15  Adam Roben  <aroben@apple.com>
    221
  • trunk/WebCore/page/inspector/utilities.js

    r31953 r31954  
    815815}
    816816
     817String.standardFormatters = {
     818    d: function(substitution)
     819    {
     820        substitution = parseInt(substitution);
     821        return !isNaN(substitution) ? substitution : 0;
     822    },
     823
     824    f: function(substitution, token)
     825    {
     826        substitution = parseFloat(substitution);
     827        if (substitution && token.precision > -1)
     828            substitution = substitution.toFixed(token.precision);
     829        return !isNaN(substitution) ? substitution : (token.precision > -1 ? Number(0).toFixed(token.precision) : 0);
     830    },
     831
     832    s: function(substitution)
     833    {
     834        return substitution;
     835    },
     836};
     837
    817838String.vsprintf = function(format, substitutions)
     839{
     840    return String.format(format, substitutions, String.standardFormatters);
     841}
     842
     843String.format = function(format, substitutions, formatters)
    818844{
    819845    if (!format || !substitutions || !substitutions.length)
     
    822848    function prettyFunctionName()
    823849    {
    824         return "String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")";
     850        return "String.format(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")";
    825851    }
    826852
     
    859885        }
    860886
    861         switch (token.specifier) {
    862         case "d":
    863             var substitution = parseInt(substitutions[token.substitutionIndex]);
    864             result += (!isNaN(substitution) ? substitution : 0);
    865             break;
    866         case "f":
    867             var substitution = parseFloat(substitutions[token.substitutionIndex]);
    868             if (substitution && token.precision > -1)
    869                 substitution = substitution.toFixed(token.precision);
    870             result += (!isNaN(substitution) ? substitution : (token.precision > -1 ? Number(0).toFixed(token.precision) : 0));
    871             break;
    872         default:
     887        if (!(token.specifier in formatters)) {
    873888            // Encountered an unsupported format character, treat as a string.
    874889            warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");
    875             // Fall through to treat this like a string.
    876         case "s":
    877890            result += substitutions[token.substitutionIndex];
    878             break;
    879         }
     891            continue;
     892        }
     893
     894        result += formatters[token.specifier](substitutions[token.substitutionIndex], token);
    880895    }
    881896
Note: See TracChangeset for help on using the changeset viewer.