Changeset 31955 in webkit


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

Generalize String.format some more

String.format now takes an initial value and an append function which
it uses to build the result. Each time a token is processed,
append(valueSoFar, newValue) is called.

Reviewed by Tim Hatcher.

  • page/inspector/utilities.js: (String.vsprintf): Pass the empty string for the initial value, and a function that concatenates two strings as the append function. (String.format): Use initialValue and append to build the result instead of only working with strings.
Location:
trunk/WebCore
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r31954 r31955  
     12008-04-15  Adam Roben  <aroben@apple.com>
     2
     3        Generalize String.format some more
     4
     5        String.format now takes an initial value and an append function which
     6        it uses to build the result. Each time a token is processed,
     7        append(valueSoFar, newValue) is called.
     8
     9        Reviewed by Tim Hatcher.
     10
     11        * page/inspector/utilities.js:
     12        (String.vsprintf): Pass the empty string for the initial value, and
     13        a function that concatenates two strings as the append function.
     14        (String.format): Use initialValue and append to build the result
     15        instead of only working with strings.
     16
    1172008-04-15  Adam Roben  <aroben@apple.com>
    218
  • trunk/WebCore/page/inspector/utilities.js

    r31954 r31955  
    838838String.vsprintf = function(format, substitutions)
    839839{
    840     return String.format(format, substitutions, String.standardFormatters);
    841 }
    842 
    843 String.format = function(format, substitutions, formatters)
     840    return String.format(format, substitutions, String.standardFormatters, "", function(a, b) { return a + b; });
     841}
     842
     843String.format = function(format, substitutions, formatters, initialValue, append)
    844844{
    845845    if (!format || !substitutions || !substitutions.length)
    846         return format;
     846        return append(initialValue, format);
    847847
    848848    function prettyFunctionName()
     
    861861    }
    862862
    863     var result = "";
     863    var result = initialValue;
    864864    var tokens = String.tokenizeFormatString(format);
    865865
     
    868868
    869869        if (token.type === "string") {
    870             result += token.value;
     870            result = append(result, token.value);
    871871            continue;
    872872        }
     
    881881            // just output the format specifier literally and move on.
    882882            error("not enough substitution arguments. Had " + substitutions.length + " but needed " + (token.substitutionIndex + 1) + ", so substitution was skipped.");
    883             result += "%" + (token.precision > -1 ? token.precision : "") + token.specifier;
     883            result = append(result, "%" + (token.precision > -1 ? token.precision : "") + token.specifier);
    884884            continue;
    885885        }
     
    888888            // Encountered an unsupported format character, treat as a string.
    889889            warn("unsupported format character \u201C" + token.specifier + "\u201D. Treating as a string.");
    890             result += substitutions[token.substitutionIndex];
     890            result = append(result, substitutions[token.substitutionIndex]);
    891891            continue;
    892892        }
    893893
    894         result += formatters[token.specifier](substitutions[token.substitutionIndex], token);
     894        result = append(result, formatters[token.specifier](substitutions[token.substitutionIndex], token));
    895895    }
    896896
Note: See TracChangeset for help on using the changeset viewer.