Changeset 31953 in webkit


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

Refactor String.vsprintf to separate tokenizing and substituting

This will eventually be useful for the various console methods that
take a format string.

Reviewed by Tim Hatcher.

  • page/inspector/utilities.js: (String.tokenizeFormatString): Added. This uses the format parsing code from String.vsprintf but just returns an array of "string" and "specifier" tokens. (String.vsprintf): Now calls String.tokenizeFormatString and processes the tokens one at a time.
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r31952 r31953  
     12008-04-15  Adam Roben  <aroben@apple.com>
     2
     3        Refactor String.vsprintf to separate tokenizing and substituting
     4
     5        This will eventually be useful for the various console methods that
     6        take a format string.
     7
     8        Reviewed by Tim Hatcher.
     9
     10        * page/inspector/utilities.js:
     11        (String.tokenizeFormatString): Added. This uses the format parsing
     12        code from String.vsprintf but just returns an array of "string" and
     13        "specifier" tokens.
     14        (String.vsprintf): Now calls String.tokenizeFormatString and processes
     15        the tokens one at a time.
     16
    1172008-04-15  Adam Roben  <aroben@apple.com>
    218
  • trunk/WebCore/page/inspector/utilities.js

    r31889 r31953  
    753753}
    754754
    755 String.vsprintf = function(format, substitutions)
    756 {
    757     if (!format || !substitutions || !substitutions.length)
    758         return format;
    759 
    760     var result = "";
     755String.tokenizeFormatString = function(format)
     756{
     757    var tokens = [];
    761758    var substitutionIndex = 0;
     759
     760    function addStringToken(str)
     761    {
     762        tokens.push({ type: "string", value: str });
     763    }
     764
     765    function addSpecifierToken(specifier, precision, substitutionIndex)
     766    {
     767        tokens.push({ type: "specifier", specifier: specifier, precision: precision, substitutionIndex: substitutionIndex });
     768    }
    762769
    763770    var index = 0;
    764771    for (var precentIndex = format.indexOf("%", index); precentIndex !== -1; precentIndex = format.indexOf("%", index)) {
    765         result += format.substring(index, precentIndex);
     772        addStringToken(format.substring(index, precentIndex));
    766773        index = precentIndex + 1;
    767774
    768775        if (format[index] === "%") {
    769             result += "%";
     776            addStringToken("%");
    770777            ++index;
    771778            continue;
     
    797804        }
    798805
    799         if (substitutionIndex >= substitutions.length) {
     806        addSpecifierToken(format[index], precision, substitutionIndex);
     807
     808        ++substitutionIndex;
     809        ++index;
     810    }
     811
     812    addStringToken(format.substring(index));
     813
     814    return tokens;
     815}
     816
     817String.vsprintf = function(format, substitutions)
     818{
     819    if (!format || !substitutions || !substitutions.length)
     820        return format;
     821
     822    function prettyFunctionName()
     823    {
     824        return "String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\")";
     825    }
     826
     827    function warn(msg)
     828    {
     829        console.warn(prettyFunctionName() + ": " + msg);
     830    }
     831
     832    function error(msg)
     833    {
     834        console.error(prettyFunctionName() + ": " + msg);
     835    }
     836
     837    var result = "";
     838    var tokens = String.tokenizeFormatString(format);
     839
     840    for (var i = 0; i < tokens.length; ++i) {
     841        var token = tokens[i];
     842
     843        if (token.type === "string") {
     844            result += token.value;
     845            continue;
     846        }
     847
     848        if (token.type !== "specifier") {
     849            error("Unknown token type \"" + token.type + "\" found.");
     850            continue;
     851        }
     852
     853        if (token.substitutionIndex >= substitutions.length) {
    800854            // If there are not enough substitutions for the current substitutionIndex
    801855            // just output the format specifier literally and move on.
    802             console.error("String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\"): not enough substitution arguments. Had " + substitutions.length + " but needed " + (substitutionIndex + 1) + ", so substitution was skipped.");
    803             index = precentIndex + 1;
    804             result += "%";
     856            error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped.");
     857            result += "%" + (token.precision > -1 ? token.precision : "") + token.specifier;
    805858            continue;
    806859        }
    807860
    808         switch (format[index]) {
     861        switch (token.specifier) {
    809862        case "d":
    810             var substitution = parseInt(substitutions[substitutionIndex]);
     863            var substitution = parseInt(substitutions[token.substitutionIndex]);
    811864            result += (!isNaN(substitution) ? substitution : 0);
    812865            break;
    813866        case "f":
    814             var substitution = parseFloat(substitutions[substitutionIndex]);
    815             if (substitution && precision > -1)
    816                 substitution = substitution.toFixed(precision);
    817             result += (!isNaN(substitution) ? substitution : (precision > -1 ? Number(0).toFixed(precision) : 0));
     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));
    818871            break;
    819872        default:
    820873            // Encountered an unsupported format character, treat as a string.
    821             console.warn("String.vsprintf(\"" + format + "\", \"" + substitutions.join("\", \"") + "\"): unsupported format character \u201C" + format[index] + "\u201D. Treating as a string.");
     874            warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");
    822875            // Fall through to treat this like a string.
    823876        case "s":
    824             result += substitutions[substitutionIndex];
    825             break;
    826         }
    827 
    828         ++substitutionIndex;
    829         ++index;
    830     }
    831 
    832     result += format.substring(index);
     877            result += substitutions[token.substitutionIndex];
     878            break;
     879        }
     880    }
    833881
    834882    return result;
Note: See TracChangeset for help on using the changeset viewer.