Changeset 242014 in webkit


Ignore:
Timestamp:
Feb 24, 2019 3:57:25 PM (5 years ago)
Author:
Darin Adler
Message:

Finish removing String::format
https://bugs.webkit.org/show_bug.cgi?id=194893

Reviewed by Daniel Bates.

Source/JavaScriptCore:

  • bytecode/CodeBlock.cpp:

(JSC::CodeBlock::nameForRegister): Use makeString instead of String::format,
using the new "pad" function.

Source/WebCore:

  • dom/Document.cpp:

(WebCore::Document::lastModified const): Use makeString and pad.

  • html/FTPDirectoryDocument.cpp:

(WebCore::processFileDateString): Ditto.

  • mathml/MathMLElement.cpp:

(WebCore::convertToPercentageIfNeeded): Use makeString and FormattedNumber.

  • page/cocoa/ResourceUsageOverlayCocoa.mm:

(WebCore::ResourceUsageOverlay::platformDraw): Use makeString and pad.

  • page/linux/ResourceUsageOverlayLinux.cpp:

(WebCore::cpuUsageString): Use makeString, FormattedNumber, and pad.
(WebCore::gcTimerString): Use String::number.

  • platform/DateComponents.cpp:

(WebCore::DateComponents::toStringForTime const): Use makeString and pad.
(WebCore::DateComponents::toString const): Ditto.

  • platform/LocalizedStrings.cpp: Removed comment that mentioned String::format,

and that was also inaccurate.

  • platform/audio/HRTFElevation.cpp:

(WebCore::HRTFElevation::calculateKernelsForAzimuthElevation):
Use makeString and pad.

  • platform/mock/MockRealtimeVideoSource.cpp:

(WebCore::MockRealtimeVideoSource::drawText): Ditto.

  • rendering/RenderLayerCompositor.cpp:

(WebCore::RenderLayerCompositor::logLayerInfo): Ditto.

  • rendering/RenderTheme.cpp:

(WebCore::RenderTheme::formatMediaControlsTime const): Ditto.

Source/WebKit:

  • UIProcess/WebAuthentication/Cocoa/LocalAuthenticator.mm:

(WebKit::LocalAuthenticator::getAssertion): Use makeString, attempting to fix
a problem where we passed an NSData * to format with a "%s"."

Source/WebKitLegacy/win:

  • FullscreenVideoController.cpp:

(timeToString): Use makeString and pad.

Source/WTF:

  • wtf/Assertions.cpp:

(WTF::createWithFormatAndArguments): Moved this here from WTFString.cpp.
(WTFLog): Use WTF::createWithFormatAndArguments instead of String::format.

  • wtf/HexNumber.h: Deleted unneeded toString function.
  • wtf/text/StringConcatenate.h: Got rid of unneeded forward declaration of

StringTypeAdapter, since that's now in Forward.h. Tweaked formatting of templates
a bit. Use function templates for writeTo functions rather than having two of each.
Removed unused toString functions. Optimized case where we use have a UChar* and
a length of zero to not force the result to be 16-bit. Also gets rid of a small
NO_RETURN_DUE_TO_CRASH mess that we don't need. Refactored constructors to use some
static member helper functions to compute string lengths. Added the pad function
and the PaddingSpecification struct template, so we can add padding to anything
we can turn into a string. Got rid of the special case overload for single
arguments, since it only worked for things that the String constructor can handle.
Instead we will now use StringTypeAdapter, which works for more types. Possibly
less optimal for some special cases, which we could specialize for later if we like.

  • wtf/text/StringConcatenateNumbers.h: Ditto.
  • wtf/text/StringOperators.h: Ditto.
  • wtf/text/StringView.h: Ditto.
  • wtf/text/WTFString.cpp:

(WTF::createWithFormatAndArguments): Deleted.
(WTF::String::format): Deleted.

  • wtf/text/WTFString.h: Deleted declaration of String::format.
Location:
trunk/Source
Files:
27 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r242011 r242014  
     12019-02-20  Darin Adler  <darin@apple.com>
     2
     3        Finish removing String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=194893
     5
     6        Reviewed by Daniel Bates.
     7
     8        * bytecode/CodeBlock.cpp:
     9        (JSC::CodeBlock::nameForRegister): Use makeString instead of String::format,
     10        using the new "pad" function.
     11
    1122019-02-23  Robin Morisset  <rmorisset@apple.com>
    213
  • trunk/Source/JavaScriptCore/bytecode/CodeBlock.cpp

    r241613 r242014  
    9393#include <wtf/SimpleStats.h>
    9494#include <wtf/StringPrintStream.h>
     95#include <wtf/text/StringConcatenateNumbers.h>
    9596#include <wtf/text/UniquedStringImpl.h>
    9697
     
    28892890        return "this"_s;
    28902891    if (virtualRegister.isArgument())
    2891         return String::format("arguments[%3d]", virtualRegister.toArgument());
    2892 
    2893     return "";
     2892        return makeString("arguments[", pad(' ', 3, virtualRegister.toArgument()), ']');
     2893
     2894    return emptyString();
    28942895}
    28952896
  • trunk/Source/WTF/ChangeLog

    r241998 r242014  
     12019-02-20  Darin Adler  <darin@apple.com>
     2
     3        Finish removing String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=194893
     5
     6        Reviewed by Daniel Bates.
     7
     8        * wtf/Assertions.cpp:
     9        (WTF::createWithFormatAndArguments): Moved this here from WTFString.cpp.
     10        (WTFLog): Use WTF::createWithFormatAndArguments instead of String::format.
     11
     12        * wtf/HexNumber.h: Deleted unneeded toString function.
     13
     14        * wtf/text/StringConcatenate.h: Got rid of unneeded forward declaration of
     15        StringTypeAdapter, since that's now in Forward.h. Tweaked formatting of templates
     16        a bit. Use function templates for writeTo functions rather than having two of each.
     17        Removed unused toString functions. Optimized case where we use have a UChar* and
     18        a length of zero to not force the result to be 16-bit. Also gets rid of a small
     19        NO_RETURN_DUE_TO_CRASH mess that we don't need. Refactored constructors to use some
     20        static member helper functions to compute string lengths. Added the pad function
     21        and the PaddingSpecification struct template, so we can add padding to anything
     22        we can turn into a string. Got rid of the special case overload for single
     23        arguments, since it only worked for things that the String constructor can handle.
     24        Instead we will now use StringTypeAdapter, which works for more types. Possibly
     25        less optimal for some special cases, which we could specialize for later if we like.
     26        * wtf/text/StringConcatenateNumbers.h: Ditto.
     27        * wtf/text/StringOperators.h: Ditto.
     28        * wtf/text/StringView.h: Ditto.
     29
     30        * wtf/text/WTFString.cpp:
     31        (WTF::createWithFormatAndArguments): Deleted.
     32        (WTF::String::format): Deleted.
     33        * wtf/text/WTFString.h: Deleted declaration of String::format.
     34
    1352019-02-23  Michael Catanzaro  <mcatanzaro@igalia.com>
    236
  • trunk/Source/WTF/wtf/Assertions.cpp

    r237099 r242014  
    7575#endif
    7676
     77namespace WTF {
     78
     79WTF_ATTRIBUTE_PRINTF(1, 0) static String createWithFormatAndArguments(const char* format, va_list args)
     80{
     81    va_list argsCopy;
     82    va_copy(argsCopy, args);
     83
     84    ALLOW_NONLITERAL_FORMAT_BEGIN
     85
     86#if USE(CF) && !OS(WINDOWS)
     87    if (strstr(format, "%@")) {
     88        auto cfFormat = adoptCF(CFStringCreateWithCString(kCFAllocatorDefault, format, kCFStringEncodingUTF8));
     89        auto result = adoptCF(CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, nullptr, cfFormat.get(), args));
     90        va_end(argsCopy);
     91        return result.get();
     92    }
     93#endif
     94
     95    // Do the format once to get the length.
     96#if COMPILER(MSVC)
     97    int result = _vscprintf(format, args);
     98#else
     99    char ch;
     100    int result = vsnprintf(&ch, 1, format, args);
     101#endif
     102
     103    if (!result) {
     104        va_end(argsCopy);
     105        return emptyString();
     106    }
     107    if (result < 0) {
     108        va_end(argsCopy);
     109        return { };
     110    }
     111
     112    Vector<char, 256> buffer;
     113    unsigned length = result;
     114    buffer.grow(length + 1);
     115
     116    // Now do the formatting again, guaranteed to fit.
     117    vsnprintf(buffer.data(), buffer.size(), format, argsCopy);
     118    va_end(argsCopy);
     119
     120    ALLOW_NONLITERAL_FORMAT_END
     121
     122    return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), length);
     123}
     124
     125}
     126
    77127extern "C" {
    78128
     
    400450
    401451    ALLOW_NONLITERAL_FORMAT_BEGIN
    402     String loggingString = String::format(format, args);
     452    String loggingString = WTF::createWithFormatAndArguments(format, args);
    403453    ALLOW_NONLITERAL_FORMAT_END
    404454
  • trunk/Source/WTF/wtf/HexNumber.h

    r241751 r242014  
    103103    bool is8Bit() const { return true; }
    104104    template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, characters(), length()); }
    105     String toString() const { return { characters(), length() }; }
    106105
    107106private:
  • trunk/Source/WTF/wtf/text/StringConcatenate.h

    r237099 r242014  
    2626#pragma once
    2727
    28 #include <string.h>
     28#include <cstring>
    2929#include <wtf/CheckedArithmetic.h>
    3030#include <wtf/text/AtomicString.h>
     
    3939namespace WTF {
    4040
    41 template<typename StringType, typename>
    42 class StringTypeAdapter;
    43 
    44 template<>
    45 class StringTypeAdapter<char, void> {
     41template<> class StringTypeAdapter<char, void> {
    4642public:
    4743    StringTypeAdapter(char character)
    48         : m_character(character)
     44        : m_character { character }
    4945    {
    5046    }
     
    5248    unsigned length() { return 1; }
    5349    bool is8Bit() { return true; }
     50    template<typename CharacterType> void writeTo(CharacterType* destination) const { *destination = m_character; }
     51
     52private:
     53    char m_character;
     54};
     55
     56template<> class StringTypeAdapter<UChar, void> {
     57public:
     58    StringTypeAdapter(UChar character)
     59        : m_character { character }
     60    {
     61    }
     62
     63    unsigned length() const { return 1; }
     64    bool is8Bit() const { return isLatin1(m_character); }
    5465
    5566    void writeTo(LChar* destination) const
    5667    {
     68        ASSERT(is8Bit());
    5769        *destination = m_character;
    5870    }
    5971
    60     void writeTo(UChar* destination) const
    61     {
    62         *destination = m_character;
    63     }
    64 
    65     String toString() const { return String(&m_character, 1); }
    66 
    67 private:
    68     char m_character;
    69 };
    70 
    71 template<>
    72 class StringTypeAdapter<UChar, void> {
    73 public:
    74     StringTypeAdapter(UChar character)
    75         : m_character(character)
    76     {
    77     }
    78 
    79     unsigned length() const { return 1; }
    80     bool is8Bit() const { return m_character <= 0xff; }
    81 
    82     void writeTo(LChar* destination) const
    83     {
    84         ASSERT(is8Bit());
    85         *destination = static_cast<LChar>(m_character);
    86     }
    87 
    88     void writeTo(UChar* destination) const
    89     {
    90         *destination = m_character;
    91     }
    92 
    93     String toString() const { return String(&m_character, 1); }
     72    void writeTo(UChar* destination) const { *destination = m_character; }
    9473
    9574private:
     
    9776};
    9877
    99 template<>
    100 class StringTypeAdapter<const LChar*, void> {
     78template<> class StringTypeAdapter<const LChar*, void> {
    10179public:
    10280    StringTypeAdapter(const LChar* characters)
    103         : m_characters(characters)
    104     {
    105         size_t length = strlen(reinterpret_cast<const char*>(characters));
    106         RELEASE_ASSERT(length <= String::MaxLength);
    107         m_length = static_cast<unsigned>(length);
     81        : m_characters { characters }
     82        , m_length { computeLength(characters) }
     83    {
    10884    }
    10985
    11086    unsigned length() const { return m_length; }
    11187    bool is8Bit() const { return true; }
    112 
    113     void writeTo(LChar* destination) const
    114     {
    115         StringView(m_characters, m_length).getCharactersWithUpconvert(destination);
    116     }
    117 
    118     void writeTo(UChar* destination) const
    119     {
    120         StringView(m_characters, m_length).getCharactersWithUpconvert(destination);
    121     }
    122 
    123     String toString() const { return String(m_characters, m_length); }
    124 
    125 private:
     88    template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, m_characters, m_length); }
     89
     90private:
     91    static unsigned computeLength(const LChar* characters)
     92    {
     93        size_t length = std::strlen(reinterpret_cast<const char*>(characters));
     94        RELEASE_ASSERT(length <= String::MaxLength);
     95        return static_cast<unsigned>(length);
     96    }
     97
    12698    const LChar* m_characters;
    12799    unsigned m_length;
    128100};
    129101
    130 template<>
    131 class StringTypeAdapter<const UChar*, void> {
     102template<> class StringTypeAdapter<const UChar*, void> {
    132103public:
    133104    StringTypeAdapter(const UChar* characters)
    134         : m_characters(characters)
     105        : m_characters { characters }
     106        , m_length { computeLength(characters) }
     107    {
     108    }
     109
     110    unsigned length() const { return m_length; }
     111    bool is8Bit() const { return !m_length; }
     112    void writeTo(LChar*) const { ASSERT(!m_length); }
     113    void writeTo(UChar* destination) const { StringImpl::copyCharacters(destination, m_characters, m_length); }
     114
     115private:
     116    static unsigned computeLength(const UChar* characters)
    135117    {
    136118        size_t length = 0;
    137         while (m_characters[length])
     119        while (characters[length])
    138120            ++length;
    139121        RELEASE_ASSERT(length <= String::MaxLength);
    140         m_length = static_cast<unsigned>(length);
    141     }
    142 
    143     unsigned length() const { return m_length; }
    144     bool is8Bit() const { return false; }
    145 
    146     NO_RETURN_DUE_TO_CRASH void writeTo(LChar*) const
    147     {
    148         CRASH(); // FIXME make this a compile-time failure https://bugs.webkit.org/show_bug.cgi?id=165791
    149     }
    150 
    151     void writeTo(UChar* destination) const
    152     {
    153         memcpy(destination, m_characters, m_length * sizeof(UChar));
    154     }
    155 
    156     String toString() const { return String(m_characters, m_length); }
    157 
    158 private:
     122        return static_cast<unsigned>(length);
     123    }
     124
    159125    const UChar* m_characters;
    160126    unsigned m_length;
    161127};
    162128
    163 template<>
    164 class StringTypeAdapter<const char*, void> : public StringTypeAdapter<const LChar*, void> {
     129template<> class StringTypeAdapter<const char*, void> : public StringTypeAdapter<const LChar*, void> {
    165130public:
    166131    StringTypeAdapter(const char* characters)
    167         : StringTypeAdapter<const LChar*, void>(reinterpret_cast<const LChar*>(characters))
    168     {
    169     }
    170 };
    171 
    172 template<>
    173 class StringTypeAdapter<char*, void> : public StringTypeAdapter<const char*, void> {
     132        : StringTypeAdapter<const LChar*, void> { reinterpret_cast<const LChar*>(characters) }
     133    {
     134    }
     135};
     136
     137template<> class StringTypeAdapter<char*, void> : public StringTypeAdapter<const char*, void> {
    174138public:
    175139    StringTypeAdapter(const char* characters)
    176         : StringTypeAdapter<const char*, void>(characters)
    177     {
    178     }
    179 };
    180 
    181 template<>
    182 class StringTypeAdapter<ASCIILiteral, void> : public StringTypeAdapter<const char*, void> {
     140        : StringTypeAdapter<const char*, void> { characters }
     141    {
     142    }
     143};
     144
     145template<> class StringTypeAdapter<ASCIILiteral, void> : public StringTypeAdapter<const char*, void> {
    183146public:
    184147    StringTypeAdapter(ASCIILiteral characters)
    185         : StringTypeAdapter<const char*, void>(characters)
    186     {
    187     }
    188 };
    189 
    190 template<>
    191 class StringTypeAdapter<Vector<char>, void> {
     148        : StringTypeAdapter<const char*, void> { characters }
     149    {
     150    }
     151};
     152
     153template<> class StringTypeAdapter<Vector<char>, void> {
    192154public:
    193155    StringTypeAdapter(const Vector<char>& vector)
    194         : m_vector(vector)
     156        : m_vector { vector }
    195157    {
    196158    }
     
    198160    size_t length() const { return m_vector.size(); }
    199161    bool is8Bit() const { return true; }
    200 
    201     void writeTo(LChar* destination) const
    202     {
    203         StringView(reinterpret_cast<const LChar*>(m_vector.data()), m_vector.size()).getCharactersWithUpconvert(destination);
    204     }
    205 
    206     void writeTo(UChar* destination) const
    207     {
    208         StringView(reinterpret_cast<const LChar*>(m_vector.data()), m_vector.size()).getCharactersWithUpconvert(destination);
    209     }
    210 
    211     String toString() const { return String(m_vector.data(), m_vector.size()); }
    212 
    213 private:
     162    template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, characters(), length()); }
     163
     164private:
     165    const LChar* characters() const
     166    {
     167        return reinterpret_cast<const LChar*>(m_vector.data());
     168    }
     169
    214170    const Vector<char>& m_vector;
    215171};
    216172
    217 template<>
    218 class StringTypeAdapter<String, void> {
     173template<> class StringTypeAdapter<String, void> {
    219174public:
    220175    StringTypeAdapter(const String& string)
    221         : m_string(string)
     176        : m_string { string }
    222177    {
    223178    }
     
    225180    unsigned length() const { return m_string.length(); }
    226181    bool is8Bit() const { return m_string.isNull() || m_string.is8Bit(); }
    227 
    228     void writeTo(LChar* destination) const
    229     {
    230         StringView(m_string).getCharactersWithUpconvert(destination);
     182    template<typename CharacterType> void writeTo(CharacterType* destination) const
     183    {
     184        StringView { m_string }.getCharactersWithUpconvert(destination);
    231185        WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
    232186    }
    233187
    234     void writeTo(UChar* destination) const
    235     {
    236         StringView(m_string).getCharactersWithUpconvert(destination);
    237         WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
    238     }
    239 
    240     String toString() const { return m_string; }
    241 
    242188private:
    243189    const String& m_string;
    244190};
    245191
    246 template<>
    247 class StringTypeAdapter<AtomicString, void> : public StringTypeAdapter<String, void> {
     192template<> class StringTypeAdapter<AtomicString, void> : public StringTypeAdapter<String, void> {
    248193public:
    249194    StringTypeAdapter(const AtomicString& string)
    250         : StringTypeAdapter<String, void>(string.string())
    251     {
    252     }
     195        : StringTypeAdapter<String, void> { string.string() }
     196    {
     197    }
     198};
     199
     200template<typename UnderlyingAdapterType> struct PaddingSpecification {
     201    LChar character;
     202    unsigned length;
     203    UnderlyingAdapterType underlyingAdapter;
     204};
     205
     206template<typename StringType> PaddingSpecification<StringTypeAdapter<StringType>> pad(char character, unsigned length, StringType adapter)
     207{
     208    return { static_cast<LChar>(character), length, StringTypeAdapter<StringType> { adapter } };
     209}
     210
     211template<typename UnderlyingAdapterType> class StringTypeAdapter<PaddingSpecification<UnderlyingAdapterType>> {
     212public:
     213    StringTypeAdapter(const PaddingSpecification<UnderlyingAdapterType>& padding)
     214        : m_padding { padding }
     215    {
     216    }
     217
     218    unsigned length() const { return std::max(m_padding.length, m_padding.underlyingAdapter.length()); }
     219    bool is8Bit() const { return m_padding.underlyingAdapter.is8Bit(); }
     220    template<typename CharacterType> void writeTo(CharacterType* destination) const
     221    {
     222        unsigned underlyingLength = m_padding.underlyingAdapter.length();
     223        unsigned count = 0;
     224        if (underlyingLength < m_padding.length) {
     225            count = m_padding.length - underlyingLength;
     226            for (unsigned i = 0; i < count; ++i)
     227                destination[i] = m_padding.character;
     228        }
     229        m_padding.underlyingAdapter.writeTo(destination + count);
     230    }
     231
     232private:
     233    const PaddingSpecification<UnderlyingAdapterType>& m_padding;
    253234};
    254235
     
    315296}
    316297
    317 // Convenience only.
    318 template<typename StringType>
    319 String makeString(StringType string)
    320 {
    321     return String(string);
    322 }
    323 
    324298template<typename... StringTypes>
    325299String makeString(StringTypes... strings)
     
    334308
    335309using WTF::makeString;
     310using WTF::pad;
    336311using WTF::tryMakeString;
    337312
  • trunk/Source/WTF/wtf/text/StringConcatenateNumbers.h

    r241751 r242014  
    4343    bool is8Bit() const { return true; }
    4444    template<typename CharacterType> void writeTo(CharacterType* destination) const { writeNumberToBufferSigned(m_number, destination); }
    45     String toString() const { return String::number(m_number); }
    4645
    4746private:
     
    6059    bool is8Bit() const { return true; }
    6160    template<typename CharacterType> void writeTo(CharacterType* destination) const { writeNumberToBufferUnsigned(m_number, destination); }
    62     String toString() const { return String::number(m_number); }
    6361
    6462private:
     
    7270    {
    7371        numberToString(number, m_buffer);
    74         m_length = strlen(m_buffer);
     72        m_length = std::strlen(m_buffer);
    7573    }
    7674
     
    7876    bool is8Bit() const { return true; }
    7977    template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, buffer(), m_length); }
    80     String toString() const { return { buffer(), m_length }; }
    8178
    8279private:
     
    9390        FormattedNumber numberFormatter;
    9491        numberToFixedPrecisionString(number, significantFigures, numberFormatter.m_buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros);
    95         numberFormatter.m_length = strlen(numberFormatter.m_buffer);
     92        numberFormatter.m_length = std::strlen(numberFormatter.m_buffer);
    9693        return numberFormatter;
    9794    }
     
    10198        FormattedNumber numberFormatter;
    10299        numberToFixedWidthString(number, decimalPlaces, numberFormatter.m_buffer);
    103         numberFormatter.m_length = strlen(numberFormatter.m_buffer);
     100        numberFormatter.m_length = std::strlen(numberFormatter.m_buffer);
    104101        return numberFormatter;
    105102    }
     
    113110};
    114111
    115 template<>
    116 class StringTypeAdapter<FormattedNumber> {
     112template<> class StringTypeAdapter<FormattedNumber> {
    117113public:
    118114    StringTypeAdapter(const FormattedNumber& number)
     
    124120    bool is8Bit() const { return true; }
    125121    template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, m_number.buffer(), m_number.length()); }
    126     String toString() const { return { m_number.buffer(), m_number.length() }; }
    127122
    128123private:
  • trunk/Source/WTF/wtf/text/StringOperators.h

    r239282 r242014  
    2424namespace WTF {
    2525
    26 template<typename StringType1, typename StringType2>
    27 class StringAppend {
     26template<typename StringType1, typename StringType2> class StringAppend {
    2827public:
    2928    StringAppend(StringType1 string1, StringType2 string2)
    30         : m_string1(string1)
    31         , m_string2(string2)
     29        : m_string1 { string1 }
     30        , m_string2 { string2 }
    3231    {
    3332    }
     
    8685public:
    8786    StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<StringType1, StringType2>& buffer)
    88         : m_buffer(buffer)
     87        : m_buffer { buffer }
    8988    {
    9089    }
    9190
    92     unsigned length() { return m_buffer.length(); }
    93 
    94     bool is8Bit() { return m_buffer.is8Bit(); }
    95 
    96     void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
    97     void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
    98 
    99     String toString() const { return m_buffer; }
     91    unsigned length() const { return m_buffer.length(); }
     92    bool is8Bit() const { return m_buffer.is8Bit(); }
     93    template<typename CharacterType> void writeTo(CharacterType* destination) const { m_buffer.writeTo(destination); }
    10094
    10195private:
  • trunk/Source/WTF/wtf/text/StringView.h

    r239427 r242014  
    566566
    567567#if !CHECK_STRINGVIEW_LIFETIME
     568
    568569inline void StringView::invalidate(const StringImpl&)
    569570{
    570571}
     572
    571573#endif
    572574
    573 template<typename StringType, typename> class StringTypeAdapter;
    574 
    575575template<> class StringTypeAdapter<StringView, void> {
    576576public:
    577577    StringTypeAdapter(StringView string)
    578         : m_string(string)
     578        : m_string { string }
    579579    {
    580580    }
     
    582582    unsigned length() { return m_string.length(); }
    583583    bool is8Bit() { return m_string.is8Bit(); }
    584     void writeTo(LChar* destination) { m_string.getCharactersWithUpconvert(destination); }
    585     void writeTo(UChar* destination) { m_string.getCharactersWithUpconvert(destination); }
    586 
    587     String toString() const { return m_string.toString(); }
     584    template<typename CharacterType> void writeTo(CharacterType* destination) { m_string.getCharactersWithUpconvert(destination); }
    588585
    589586private:
  • trunk/Source/WTF/wtf/text/WTFString.cpp

    r241233 r242014  
    450450}
    451451
    452 WTF_ATTRIBUTE_PRINTF(1, 0) static String createWithFormatAndArguments(const char *format, va_list args)
    453 {
    454     va_list argsCopy;
    455     va_copy(argsCopy, args);
    456 
    457     ALLOW_NONLITERAL_FORMAT_BEGIN
    458 
    459 #if USE(CF) && !OS(WINDOWS)
    460     if (strstr(format, "%@")) {
    461         auto cfFormat = adoptCF(CFStringCreateWithCString(kCFAllocatorDefault, format, kCFStringEncodingUTF8));
    462         auto result = adoptCF(CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, nullptr, cfFormat.get(), args));
    463         va_end(argsCopy);
    464         return result.get();
    465     }
    466 #endif
    467 
    468     // Do the format once to get the length.
    469 #if COMPILER(MSVC)
    470     int result = _vscprintf(format, args);
    471 #else
    472     char ch;
    473     int result = vsnprintf(&ch, 1, format, args);
    474 #endif
    475 
    476     if (!result) {
    477         va_end(argsCopy);
    478         return emptyString();
    479     }
    480     if (result < 0) {
    481         va_end(argsCopy);
    482         return String();
    483     }
    484 
    485     Vector<char, 256> buffer;
    486     unsigned len = result;
    487     buffer.grow(len + 1);
    488 
    489     // Now do the formatting again, guaranteed to fit.
    490     vsnprintf(buffer.data(), buffer.size(), format, argsCopy);
    491     va_end(argsCopy);
    492 
    493     ALLOW_NONLITERAL_FORMAT_END
    494 
    495     return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), len);
    496 }
    497 
    498 String String::format(const char *format, ...)
    499 {
    500     va_list args;
    501     va_start(args, format);
    502     String result = createWithFormatAndArguments(format, args);
    503     va_end(args);
    504     return result;
    505 }
    506 
    507452String String::number(int number)
    508453{
  • trunk/Source/WTF/wtf/text/WTFString.h

    r239439 r242014  
    261261    // Use convertToASCIILowercase instead if ASCII case insensitive comparison is desired.
    262262    WTF_EXPORT_PRIVATE String foldCase() const;
    263 
    264     WTF_EXPORT_PRIVATE static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
    265263
    266264    // Returns an uninitialized string. The characters needs to be written
  • trunk/Source/WebCore/ChangeLog

    r242013 r242014  
     12019-02-20  Darin Adler  <darin@apple.com>
     2
     3        Finish removing String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=194893
     5
     6        Reviewed by Daniel Bates.
     7
     8        * dom/Document.cpp:
     9        (WebCore::Document::lastModified const): Use makeString and pad.
     10        * html/FTPDirectoryDocument.cpp:
     11        (WebCore::processFileDateString): Ditto.
     12
     13        * mathml/MathMLElement.cpp:
     14        (WebCore::convertToPercentageIfNeeded): Use makeString and FormattedNumber.
     15
     16        * page/cocoa/ResourceUsageOverlayCocoa.mm:
     17        (WebCore::ResourceUsageOverlay::platformDraw): Use makeString and pad.
     18
     19        * page/linux/ResourceUsageOverlayLinux.cpp:
     20        (WebCore::cpuUsageString): Use makeString, FormattedNumber, and pad.
     21        (WebCore::gcTimerString): Use String::number.
     22
     23        * platform/DateComponents.cpp:
     24        (WebCore::DateComponents::toStringForTime const): Use makeString and pad.
     25        (WebCore::DateComponents::toString const): Ditto.
     26
     27        * platform/LocalizedStrings.cpp: Removed comment that mentioned String::format,
     28        and that was also inaccurate.
     29
     30        * platform/audio/HRTFElevation.cpp:
     31        (WebCore::HRTFElevation::calculateKernelsForAzimuthElevation):
     32        Use makeString and pad.
     33        * platform/mock/MockRealtimeVideoSource.cpp:
     34        (WebCore::MockRealtimeVideoSource::drawText): Ditto.
     35        * rendering/RenderLayerCompositor.cpp:
     36        (WebCore::RenderLayerCompositor::logLayerInfo): Ditto.
     37        * rendering/RenderTheme.cpp:
     38        (WebCore::RenderTheme::formatMediaControlsTime const): Ditto.
     39
    1402019-02-24  Michael Catanzaro  <mcatanzaro@igalia.com>
    241
  • trunk/Source/WebCore/dom/Document.cpp

    r241932 r242014  
    50105010        dateTime = loader()->response().lastModified();
    50115011
    5012     // FIXME: If this document came from the file system, the HTML5
    5013     // specification tells us to read the last modification date from the file
    5014     // system.
     5012    // FIXME: If this document came from the file system, the HTML specification tells
     5013    // us to read the last modification date from the file system.
    50155014    if (!dateTime)
    50165015        dateTime = WallTime::now();
     
    50185017    auto ctime = dateTime.value().secondsSinceEpoch().secondsAs<time_t>();
    50195018    auto localDateTime = std::localtime(&ctime);
    5020     return String::format("%02d/%02d/%04d %02d:%02d:%02d", localDateTime->tm_mon + 1, localDateTime->tm_mday, 1900 + localDateTime->tm_year, localDateTime->tm_hour, localDateTime->tm_min, localDateTime->tm_sec);
     5019    return makeString(pad('0', 2, localDateTime->tm_mon + 1), '/',
     5020        pad('0', 2, localDateTime->tm_mday), '/',
     5021        pad('0', 4, 1900 + localDateTime->tm_year), ' ',
     5022        pad('0', 2, localDateTime->tm_hour), ':',
     5023        pad('0', 2, localDateTime->tm_min), ':',
     5024        pad('0', 2, localDateTime->tm_sec));
    50215025}
    50225026
  • trunk/Source/WebCore/html/FTPDirectoryDocument.cpp

    r241244 r242014  
    211211            if (hour == 0)
    212212                hour = 12;
    213             timeOfDay = String::format(", %i:%02i AM", hour, fileTime.tm_min);
     213            timeOfDay = makeString(", ", hour, ':', pad('0', 2, fileTime.tm_min), " AM");
    214214        } else {
    215215            hour = hour - 12;
    216216            if (hour == 0)
    217217                hour = 12;
    218             timeOfDay = String::format(", %i:%02i PM", hour, fileTime.tm_min);
     218            timeOfDay = makeString(", ", hour, ':', pad('0', 2, fileTime.tm_min), " PM");
    219219        }
    220220    }
  • trunk/Source/WebCore/mathml/MathMLElement.cpp

    r241947 r242014  
    4040#include "RenderTableCell.h"
    4141#include <wtf/IsoMallocInlines.h>
     42#include <wtf/text/StringConcatenateNumbers.h>
    4243
    4344namespace WebCore {
     
    100101static String convertToPercentageIfNeeded(const AtomicString& value)
    101102{
     103    // FIXME: Might be better to use double than float.
     104    // FIXME: Might be better to use "shortest" numeric formatting instead of fixed width.
    102105    bool ok = false;
    103106    float unitlessValue = value.toFloat(&ok);
    104     if (ok)
    105         return String::format("%.3f%%", unitlessValue * 100.0);
    106     return value;
     107    if (!ok)
     108        return value;
     109    return makeString(FormattedNumber::fixedWidth(unitlessValue * 100, 3), '%');
    107110}
    108111
     
    117120    } else if (name == mathcolorAttr)
    118121        addPropertyToPresentationAttributeStyle(style, CSSPropertyColor, value);
    119     // FIXME: deprecated attributes that should loose in a conflict with a non deprecated attribute
     122    // FIXME: The following are deprecated attributes that should lose if there is a conflict with a non-deprecated attribute.
    120123    else if (name == fontsizeAttr)
    121124        addPropertyToPresentationAttributeStyle(style, CSSPropertyFontSize, value);
     
    135138    }  else {
    136139        ASSERT(!isPresentationAttribute(name));
    137         StyledElement::collectStyleForPresentationAttribute(name, value
    138         , style);
     140        StyledElement::collectStyleForPresentationAttribute(name, value, style);
    139141    }
    140142}
  • trunk/Source/WebCore/page/cocoa/ResourceUsageOverlayCocoa.mm

    r241244 r242014  
    469469        size_t external = category.externalSize.last();
    470470       
    471         String label = String::format("% 11s: %s", category.name.ascii().data(), formatByteNumber(dirty).ascii().data());
     471        String label = makeString(pad(' ', 11, category.name), ": ", formatByteNumber(dirty));
    472472        if (external)
    473473            label = label + makeString(" + ", formatByteNumber(external));
  • trunk/Source/WebCore/page/linux/ResourceUsageOverlayLinux.cpp

    r241751 r242014  
    4747    if (cpuUsage < 0)
    4848        return "<unknown>"_s;
    49     return String::format("%.1f%%", cpuUsage);
     49    return makeString(FormattedNumber::fixedWidth(cpuUsage, 1), '%');
    5050}
    5151
     
    6565    if (std::isnan(timerFireDate))
    6666        return "[not scheduled]"_s;
    67     return String::format("%g", (timerFireDate - now).seconds());
     67    return String::number((timerFireDate - now).seconds());
    6868}
    6969
  • trunk/Source/WebCore/platform/DateComponents.cpp

    r237990 r242014  
    3636#include <wtf/DateMath.h>
    3737#include <wtf/MathExtras.h>
    38 #include <wtf/text/WTFString.h>
     38#include <wtf/text/StringConcatenateNumbers.h>
    3939
    4040namespace WebCore {
     
    694694#endif
    695695    case None:
    696         return String::format("%02d:%02d", m_hour, m_minute);
     696        return makeString(pad('0', 2, m_hour), ':', pad('0', 2, m_minute));
    697697    case Second:
    698         return String::format("%02d:%02d:%02d", m_hour, m_minute, m_second);
     698        return makeString(pad('0', 2, m_hour), ':', pad('0', 2, m_minute), ':', pad('0', 2, m_second));
    699699    case Millisecond:
    700         return String::format("%02d:%02d:%02d.%03d", m_hour, m_minute, m_second, m_millisecond);
     700        return makeString(pad('0', 2, m_hour), ':', pad('0', 2, m_minute), ':', pad('0', 2, m_second), '.', pad('0', 3, m_millisecond));
    701701    }
    702702}
     
    706706    switch (m_type) {
    707707    case Date:
    708         return String::format("%04d-%02d-%02d", m_year, m_month + 1, m_monthDay);
     708        return makeString(pad('0', 4, m_year), '-', pad('0', 2, m_month + 1), '-', pad('0', 2, m_monthDay));
    709709    case DateTime:
    710         return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
    711             + toStringForTime(format) + "Z"_str;
     710        return makeString(pad('0', 4, m_year), '-', pad('0', 2, m_month + 1), '-', pad('0', 2, m_monthDay), 'T', toStringForTime(format), 'Z');
    712711    case DateTimeLocal:
    713         return String::format("%04d-%02d-%02dT", m_year, m_month + 1, m_monthDay)
    714             + toStringForTime(format);
     712        return makeString(pad('0', 4, m_year), '-', pad('0', 2, m_month + 1), '-', pad('0', 2, m_monthDay), 'T', toStringForTime(format));
    715713    case Month:
    716         return String::format("%04d-%02d", m_year, m_month + 1);
     714        return makeString(pad('0', 4, m_year), '-', pad('0', 2, m_month + 1));
    717715    case Time:
    718716        return toStringForTime(format);
    719717    case Week:
    720         return String::format("%04d-W%02d", m_year, m_week);
     718        return makeString(pad('0', 4, m_year), "-W", pad('0', 2, m_week));
    721719    case Invalid:
    722720        break;
  • trunk/Source/WebCore/platform/LocalizedStrings.cpp

    r240710 r242014  
    4444namespace WebCore {
    4545
    46 // We can't use String::format for two reasons:
    47 //  1) It doesn't handle non-ASCII characters in the format string.
    48 //  2) It doesn't handle the %2$d syntax.
    49 // Note that because |format| is used as the second parameter to va_start, it cannot be a reference
     46// Because |format| is used as the second parameter to va_start, it cannot be a reference
    5047// type according to section 18.7/3 of the C++ N1905 standard.
    5148String formatLocalizedString(String format, ...)
     
    7471
    7572#if !USE(CF)
     73
    7674String localizedString(const char* key)
    7775{
    7876    return String::fromUTF8(key, strlen(key));
    7977}
     78
    8079#endif
    8180
  • trunk/Source/WebCore/platform/audio/HRTFElevation.cpp

    r239535 r242014  
    177177    AudioChannel* rightEarImpulseResponse = response->channel(AudioBus::ChannelRight);
    178178#else
    179     String resourceName = String::format("IRC_%s_C_R0195_T%03d_P%03d", subjectName.utf8().data(), azimuth, positiveElevation);
     179    String resourceName = makeString("IRC_", subjectName, "_C_R0195_T", pad('0', 3, azimuth), "_P", pad('0', 3, positiveElevation));
    180180
    181181    RefPtr<AudioBus> impulseResponse(AudioBus::loadPlatformResource(resourceName.utf8().data(), sampleRate));
  • trunk/Source/WebCore/platform/mock/MockRealtimeVideoSource.cpp

    r240892 r242014  
    351351    context.setFillColor(Color::white);
    352352    context.setTextDrawingMode(TextModeFill);
    353     String string = String::format("%02u:%02u:%02u.%03u", hours, minutes, seconds, milliseconds % 1000);
     353    String string = makeString(pad('0', 2, hours), ':', pad('0', 2, minutes), ':', pad('0', 2, seconds), '.', pad('0', 3, milliseconds % 1000));
    354354    context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
    355355
    356     string = String::format("%06u", m_frameNumber++);
     356    string = makeString(pad('0', 6, m_frameNumber++));
    357357    timeLocation.move(0, m_baseFontSize);
    358358    context.drawText(timeFont, TextRun((StringView(string))), timeLocation);
  • trunk/Source/WebCore/rendering/RenderLayerCompositor.cpp

    r241860 r242014  
    6161#include "TiledBacking.h"
    6262#include "TransformState.h"
     63#include <wtf/HexNumber.h>
    6364#include <wtf/MemoryPressureHandler.h>
    6465#include <wtf/SetForScope.h>
     
    13241325   
    13251326    StringBuilder logString;
    1326     logString.append(String::format("%*p id %" PRIu64 " (%.3f,%.3f-%.3f,%.3f) %.2fKB", 12 + depth * 2, &layer, backing->graphicsLayer()->primaryLayerID(),
    1327         absoluteBounds.x().toFloat(), absoluteBounds.y().toFloat(), absoluteBounds.maxX().toFloat(), absoluteBounds.maxY().toFloat(),
    1328         backing->backingStoreMemoryEstimate() / 1024));
    1329    
     1327    logString.append(makeString(pad(' ', 12 + depth * 2, hex(reinterpret_cast<uintptr_t>(&layer))), " id ", backing->graphicsLayer()->primaryLayerID(), " (", FormattedNumber::fixedWidth(absoluteBounds.x().toFloat(), 3), ',', FormattedNumber::fixedWidth(absoluteBounds.y().toFloat(), 3), '-', FormattedNumber::fixedWidth(absoluteBounds.maxX().toFloat(), 3), ',', FormattedNumber::fixedWidth(absoluteBounds.maxY().toFloat(), 3), ") ", FormattedNumber::fixedWidth(backing->backingStoreMemoryEstimate() / 1024, 2), "KB"));
     1328
    13301329    if (!layer.renderer().style().hasAutoZIndex())
    13311330        logString.append(makeString(" z-index: ", layer.renderer().style().zIndex()));
  • trunk/Source/WebCore/rendering/RenderTheme.cpp

    r240628 r242014  
    4747#include <wtf/FileSystem.h>
    4848#include <wtf/NeverDestroyed.h>
     49#include <wtf/text/StringConcatenateNumbers.h>
    4950
    5051#if ENABLE(METER_ELEMENT)
     
    569570    if (!std::isfinite(time))
    570571        time = 0;
    571     int seconds = (int)fabsf(time);
     572    // FIXME: Seems like it would be better to use std::lround here.
     573    int seconds = static_cast<int>(std::abs(time));
    572574    int hours = seconds / (60 * 60);
    573575    int minutes = (seconds / 60) % 60;
    574576    seconds %= 60;
    575     if (hours) {
    576         if (hours > 9)
    577             return String::format("%s%02d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
    578 
    579         return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
    580     }
    581 
    582     return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds);
     577    if (hours)
     578        return makeString((time < 0 ? "-" : ""), hours, ':', pad('0', 2, minutes), ':', pad('0', 2, seconds));
     579    return makeString((time < 0 ? "-" : ""), pad('0', 2, minutes), ':', pad('0', 2, seconds));
    583580}
    584581
  • trunk/Source/WebKit/ChangeLog

    r242012 r242014  
     12019-02-20  Darin Adler  <darin@apple.com>
     2
     3        Finish removing String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=194893
     5
     6        Reviewed by Daniel Bates.
     7
     8        * UIProcess/WebAuthentication/Cocoa/LocalAuthenticator.mm:
     9        (WebKit::LocalAuthenticator::getAssertion): Use makeString, attempting to fix
     10        a problem where we passed an NSData * to format with a "%s"."
     11
    1122019-02-24  Michael Catanzaro  <mcatanzaro@igalia.com>
    213
  • trunk/Source/WebKit/UIProcess/WebAuthentication/Cocoa/LocalAuthenticator.mm

    r240892 r242014  
    401401        weakThis->continueGetAssertionAfterUserConsented(consent, context, credentialId, userhandle);
    402402    };
     403    NSData *idData = selectedCredentialAttributes[(id)kSecAttrApplicationTag];
     404    StringView idStringView { static_cast<const LChar*>([idData bytes]), static_cast<unsigned>([idData length]) };
    403405    m_connection->getUserConsent(
    404         String::format("Log into %s with %s.", requestData().requestOptions.rpId.utf8().data(), selectedCredentialAttributes[(id)kSecAttrApplicationTag]),
     406        makeString("Log into ", requestData().requestOptions.rpId, " with ", idStringView, '.'),
    405407        (__bridge SecAccessControlRef)selectedCredentialAttributes[(id)kSecAttrAccessControl],
    406408        WTFMove(callback));
  • trunk/Source/WebKitLegacy/win/ChangeLog

    r241949 r242014  
     12019-02-20  Darin Adler  <darin@apple.com>
     2
     3        Finish removing String::format
     4        https://bugs.webkit.org/show_bug.cgi?id=194893
     5
     6        Reviewed by Daniel Bates.
     7
     8        * FullscreenVideoController.cpp:
     9        (timeToString): Use makeString and pad.
     10
    1112019-02-22  Wenson Hsieh  <wenson_hsieh@apple.com>
    212
  • trunk/Source/WebKitLegacy/win/FullscreenVideoController.cpp

    r239549 r242014  
    4242#include <windowsx.h>
    4343#include <wtf/StdLibExtras.h>
     44#include <wtf/text/StringConcatenateNumbers.h>
    4445
    4546#if USE(CA)
     
    471472    int minutes = (seconds / 60) % 60;
    472473    seconds %= 60;
    473 
    474     if (hours) {
    475         if (hours > 9)
    476             return String::format("%s%02d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
    477         return String::format("%s%01d:%02d:%02d", (time < 0 ? "-" : ""), hours, minutes, seconds);
    478     }
    479 
    480     return String::format("%s%02d:%02d", (time < 0 ? "-" : ""), minutes, seconds);
     474    if (hours)
     475        return makeString((time < 0 ? "-" : ""), hours, ':', pad('0', 2, minutes), ':', pad('0', 2, seconds));
     476    return makeString((time < 0 ? "-" : ""), pad('0', 2, minutes), ':', pad('0', 2, seconds));
    481477}
    482478
Note: See TracChangeset for help on using the changeset viewer.