Add the ability to use numbers in makeString()
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 16 Jan 2017 22:23:55 +0000 (22:23 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 16 Jan 2017 22:23:55 +0000 (22:23 +0000)
https://bugs.webkit.org/show_bug.cgi?id=167087

Reviewed by Darin Adler.

Source/WTF:

Allow numbers to be easily used in makeString() and tryMakeString().

For instance, you can now write:
    int amount = 7;
    auto foo = makeString("There are ", amount, " apples in the cart");

* WTF.xcodeproj/project.pbxproj:
Add new file.

* wtf/text/IntegerToStringConversion.h:
(WTF::writeNumberToBufferImpl):
(WTF::writeNumberToBufferSigned):
(WTF::writeNumberToBufferUnsigned):
(WTF::lengthOfNumberAsStringImpl):
(WTF::lengthOfNumberAsStringSigned):
(WTF::lengthOfNumberAsStringUnsigned):
Add variants of integer writing code that compute the length of the string
that would be produced and writes the string to an existing buffer.

(WTF::IntegerToStringConversionTrait<AtomicString>::flush): Deleted.
(WTF::IntegerToStringConversionTrait<String>::flush): Deleted.
(WTF::IntegerToStringConversionTrait<StringBuilder>::flush): Deleted.
Move these traits to their respective classes.

* wtf/text/AtomicString.h:
(WTF::IntegerToStringConversionTrait<AtomicString>::flush):
* wtf/text/StringBuilder.h:
(WTF::IntegerToStringConversionTrait<StringBuilder>::flush):
* wtf/text/WTFString.h:
(WTF::IntegerToStringConversionTrait<String>::flush):
Traits moved here from IntegerToStringConversion.h

* wtf/text/StringConcatenateNumbers.h: Added.
(WTF::StringTypeAdapter<int>::StringTypeAdapter<int>):
(WTF::StringTypeAdapter<int>::length):
(WTF::StringTypeAdapter<int>::is8Bit):
(WTF::StringTypeAdapter<int>::writeTo):
(WTF::StringTypeAdapter<int>::toString):
(WTF::StringTypeAdapter<unsigned>::StringTypeAdapter<unsigned>):
(WTF::StringTypeAdapter<unsigned>::length):
(WTF::StringTypeAdapter<unsigned>::is8Bit):
(WTF::StringTypeAdapter<unsigned>::writeTo):
(WTF::StringTypeAdapter<unsigned>::toString):
(WTF::StringTypeAdapter<float>::StringTypeAdapter<float>):
(WTF::StringTypeAdapter<float>::length):
(WTF::StringTypeAdapter<float>::is8Bit):
(WTF::StringTypeAdapter<float>::writeTo):
(WTF::StringTypeAdapter<float>::toString):
(WTF::StringTypeAdapter<double>::StringTypeAdapter<double>):
(WTF::StringTypeAdapter<double>::length):
(WTF::StringTypeAdapter<double>::is8Bit):
(WTF::StringTypeAdapter<double>::writeTo):
(WTF::StringTypeAdapter<double>::toString):
Add basic adaptors for int, unsigned, float, and double.

(WTF::FormattedNumber::fixedPrecision):
(WTF::FormattedNumber::fixedWidth):
(WTF::FormattedNumber::length):
(WTF::FormattedNumber::buffer):
(WTF::FormattedNumber::stringView):
(WTF::StringTypeAdapter<FormattedNumber>::StringTypeAdapter<FormattedNumber>):
(WTF::StringTypeAdapter<FormattedNumber>::length):
(WTF::StringTypeAdapter<FormattedNumber>::is8Bit):
(WTF::StringTypeAdapter<FormattedNumber>::writeTo):
(WTF::StringTypeAdapter<FormattedNumber>::toString):
Add a special class, FormattedNumber, and an adaptor for it, allowing for
fixedPrecision and fixedWidth representation of doubles.

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WTF/StringConcatenate.cpp: Added.
Add tests for StringConcatenate, focusing on new numeric additions.

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@210790 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/WTF.xcodeproj/project.pbxproj
Source/WTF/wtf/text/AtomicString.h
Source/WTF/wtf/text/IntegerToStringConversion.h
Source/WTF/wtf/text/StringBuilder.h
Source/WTF/wtf/text/StringConcatenateNumbers.h [new file with mode: 0644]
Source/WTF/wtf/text/WTFString.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WTF/StringConcatenate.cpp [new file with mode: 0644]

index 49ca3a8..624101a 100644 (file)
@@ -1,3 +1,78 @@
+2017-01-15  Sam Weinig  <sam@webkit.org>
+
+        Add the ability to use numbers in makeString()
+        https://bugs.webkit.org/show_bug.cgi?id=167087
+
+        Reviewed by Darin Adler.
+
+        Allow numbers to be easily used in makeString() and tryMakeString().
+
+        For instance, you can now write:
+            int amount = 7;
+            auto foo = makeString("There are ", amount, " apples in the cart");
+
+        * WTF.xcodeproj/project.pbxproj:
+        Add new file.
+
+        * wtf/text/IntegerToStringConversion.h:
+        (WTF::writeNumberToBufferImpl):
+        (WTF::writeNumberToBufferSigned):
+        (WTF::writeNumberToBufferUnsigned):
+        (WTF::lengthOfNumberAsStringImpl):
+        (WTF::lengthOfNumberAsStringSigned):
+        (WTF::lengthOfNumberAsStringUnsigned):
+        Add variants of integer writing code that compute the length of the string
+        that would be produced and writes the string to an existing buffer.
+
+        (WTF::IntegerToStringConversionTrait<AtomicString>::flush): Deleted.
+        (WTF::IntegerToStringConversionTrait<String>::flush): Deleted.
+        (WTF::IntegerToStringConversionTrait<StringBuilder>::flush): Deleted.
+        Move these traits to their respective classes.
+
+        * wtf/text/AtomicString.h:
+        (WTF::IntegerToStringConversionTrait<AtomicString>::flush):
+        * wtf/text/StringBuilder.h:
+        (WTF::IntegerToStringConversionTrait<StringBuilder>::flush):
+        * wtf/text/WTFString.h:
+        (WTF::IntegerToStringConversionTrait<String>::flush):
+        Traits moved here from IntegerToStringConversion.h
+
+        * wtf/text/StringConcatenateNumbers.h: Added.
+        (WTF::StringTypeAdapter<int>::StringTypeAdapter<int>):
+        (WTF::StringTypeAdapter<int>::length):
+        (WTF::StringTypeAdapter<int>::is8Bit):
+        (WTF::StringTypeAdapter<int>::writeTo):
+        (WTF::StringTypeAdapter<int>::toString):
+        (WTF::StringTypeAdapter<unsigned>::StringTypeAdapter<unsigned>):
+        (WTF::StringTypeAdapter<unsigned>::length):
+        (WTF::StringTypeAdapter<unsigned>::is8Bit):
+        (WTF::StringTypeAdapter<unsigned>::writeTo):
+        (WTF::StringTypeAdapter<unsigned>::toString):
+        (WTF::StringTypeAdapter<float>::StringTypeAdapter<float>):
+        (WTF::StringTypeAdapter<float>::length):
+        (WTF::StringTypeAdapter<float>::is8Bit):
+        (WTF::StringTypeAdapter<float>::writeTo):
+        (WTF::StringTypeAdapter<float>::toString):
+        (WTF::StringTypeAdapter<double>::StringTypeAdapter<double>):
+        (WTF::StringTypeAdapter<double>::length):
+        (WTF::StringTypeAdapter<double>::is8Bit):
+        (WTF::StringTypeAdapter<double>::writeTo):
+        (WTF::StringTypeAdapter<double>::toString):
+        Add basic adaptors for int, unsigned, float, and double.
+
+        (WTF::FormattedNumber::fixedPrecision):
+        (WTF::FormattedNumber::fixedWidth):
+        (WTF::FormattedNumber::length):
+        (WTF::FormattedNumber::buffer):
+        (WTF::FormattedNumber::stringView):
+        (WTF::StringTypeAdapter<FormattedNumber>::StringTypeAdapter<FormattedNumber>):
+        (WTF::StringTypeAdapter<FormattedNumber>::length):
+        (WTF::StringTypeAdapter<FormattedNumber>::is8Bit):
+        (WTF::StringTypeAdapter<FormattedNumber>::writeTo):
+        (WTF::StringTypeAdapter<FormattedNumber>::toString):
+        Add a special class, FormattedNumber, and an adaptor for it, allowing for
+        fixedPrecision and fixedWidth representation of doubles.
+
 2017-01-14  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         WebAssembly: Suppress warnings & errors in GCC
index 2a78044..fdd8701 100644 (file)
                7C3F723A1D78811A00674E26 /* Brigand.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C3F72391D78811900674E26 /* Brigand.h */; };
                7CBBA07419BB7FDC00BBF025 /* OSObjectPtr.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CBBA07319BB7FDC00BBF025 /* OSObjectPtr.h */; };
                7CD0D5A91D5534A7000CC9E1 /* Variant.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CD0D5A71D55322A000CC9E1 /* Variant.h */; };
+               7CD4C2701E2C82B900929470 /* StringConcatenateNumbers.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CD4C26F1E2C82B900929470 /* StringConcatenateNumbers.h */; };
                7CDD7FF8186D291E007433CD /* IteratorAdaptors.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CDD7FF7186D291E007433CD /* IteratorAdaptors.h */; };
                7CDD7FFA186D2A54007433CD /* IteratorRange.h in Headers */ = {isa = PBXBuildFile; fileRef = 7CDD7FF9186D2A54007433CD /* IteratorRange.h */; };
                7E29C33E15FFD79B00516D61 /* ObjcRuntimeExtras.h in Headers */ = {isa = PBXBuildFile; fileRef = 7E29C33D15FFD79B00516D61 /* ObjcRuntimeExtras.h */; };
                7C3F72391D78811900674E26 /* Brigand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Brigand.h; sourceTree = "<group>"; };
                7CBBA07319BB7FDC00BBF025 /* OSObjectPtr.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSObjectPtr.h; sourceTree = "<group>"; };
                7CD0D5A71D55322A000CC9E1 /* Variant.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Variant.h; sourceTree = "<group>"; };
+               7CD4C26F1E2C82B900929470 /* StringConcatenateNumbers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StringConcatenateNumbers.h; sourceTree = "<group>"; };
                7CDD7FF7186D291E007433CD /* IteratorAdaptors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IteratorAdaptors.h; sourceTree = "<group>"; };
                7CDD7FF9186D2A54007433CD /* IteratorRange.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = IteratorRange.h; sourceTree = "<group>"; };
                7E29C33D15FFD79B00516D61 /* ObjcRuntimeExtras.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjcRuntimeExtras.h; sourceTree = "<group>"; };
                                A8A47325151A825B004123FF /* StringBuilder.h */,
                                430B47871AAAAC1A001223DA /* StringCommon.h */,
                                A8A47326151A825B004123FF /* StringConcatenate.h */,
+                               7CD4C26F1E2C82B900929470 /* StringConcatenateNumbers.h */,
                                A8A47327151A825B004123FF /* StringHash.h */,
                                A8A47328151A825B004123FF /* StringImpl.cpp */,
                                A8A47329151A825B004123FF /* StringImpl.h */,
                                1A428B8C1C8F89DD0051E9EB /* AppKitCompatibilityDeclarations.h in Headers */,
                                A8A47385151A825B004123FF /* ASCIICType.h in Headers */,
                                A8A47434151A825B004123FF /* ASCIIFastPath.h in Headers */,
+                               7CD4C2701E2C82B900929470 /* StringConcatenateNumbers.h in Headers */,
                                A8A47387151A825B004123FF /* Assertions.h in Headers */,
                                A8A47388151A825B004123FF /* Atomics.h in Headers */,
                                A8A47436151A825B004123FF /* AtomicString.h in Headers */,
index 007dca7..91bb20a 100644 (file)
@@ -23,6 +23,7 @@
 
 #include <utility>
 #include <wtf/text/AtomicStringImpl.h>
+#include <wtf/text/IntegerToStringConversion.h>
 #include <wtf/text/WTFString.h>
 
 // Define 'NO_IMPLICIT_ATOMICSTRING' before including this header,
@@ -360,6 +361,12 @@ inline bool equalIgnoringASCIICase(const AtomicString& a, const char* b)
     return equalIgnoringASCIICase(a.string(), b);
 }
 
+template<> struct IntegerToStringConversionTrait<AtomicString> {
+    using ReturnType = AtomicString;
+    using AdditionalArgumentType = void;
+    static AtomicString flush(LChar* characters, unsigned length, void*) { return { characters, length }; }
+};
+
 } // namespace WTF
 
 #ifndef ATOMICSTRING_HIDE_GLOBALS
index 649fb05..563614d 100644 (file)
@@ -22,8 +22,6 @@
 #ifndef IntegerToStringConversion_h
 #define IntegerToStringConversion_h
 
-#include "StringBuilder.h"
-
 namespace WTF {
 
 enum PositiveOrNegativeNumber {
@@ -33,22 +31,6 @@ enum PositiveOrNegativeNumber {
 
 template<typename T> struct IntegerToStringConversionTrait;
 
-template<> struct IntegerToStringConversionTrait<AtomicString> {
-    typedef AtomicString ReturnType;
-    typedef void AdditionalArgumentType;
-    static ReturnType flush(LChar* characters, unsigned length, void*) { return AtomicString(characters, length); }
-};
-template<> struct IntegerToStringConversionTrait<String> {
-    typedef String ReturnType;
-    typedef void AdditionalArgumentType;
-    static ReturnType flush(LChar* characters, unsigned length, void*) { return String(characters, length); }
-};
-template<> struct IntegerToStringConversionTrait<StringBuilder> {
-    typedef void ReturnType;
-    typedef StringBuilder AdditionalArgumentType;
-    static ReturnType flush(LChar* characters, unsigned length, StringBuilder* stringBuilder) { stringBuilder->append(characters, length); }
-};
-
 template<typename T, typename UnsignedIntegerType, PositiveOrNegativeNumber NumberType, typename AdditionalArgumentType>
 static typename IntegerToStringConversionTrait<T>::ReturnType numberToStringImpl(UnsignedIntegerType number, AdditionalArgumentType additionalArgument)
 {
@@ -81,6 +63,72 @@ inline typename IntegerToStringConversionTrait<T>::ReturnType numberToStringUnsi
     return numberToStringImpl<T, UnsignedIntegerType, PositiveNumber>(number, additionalArgument);
 }
 
+
+template<typename CharacterType, typename UnsignedIntegerType, PositiveOrNegativeNumber NumberType>
+static void writeNumberToBufferImpl(UnsignedIntegerType number, CharacterType* destination)
+{
+    LChar buf[sizeof(UnsignedIntegerType) * 3 + 1];
+    LChar* end = buf + WTF_ARRAY_LENGTH(buf);
+    LChar* p = end;
+
+    do {
+        *--p = static_cast<LChar>((number % 10) + '0');
+        number /= 10;
+    } while (number);
+
+    if (NumberType == NegativeNumber)
+        *--p = '-';
+    
+    while (p < end)
+        *destination++ = static_cast<CharacterType>(*p++);
+}
+
+template<typename CharacterType, typename SignedIntegerType>
+inline void writeNumberToBufferSigned(SignedIntegerType number, CharacterType* destination)
+{
+    if (number < 0)
+        return writeNumberToBufferImpl<CharacterType, typename std::make_unsigned<SignedIntegerType>::type, NegativeNumber>(-number, destination);
+    return writeNumberToBufferImpl<CharacterType, typename std::make_unsigned<SignedIntegerType>::type, PositiveNumber>(number, destination);
+}
+
+template<typename CharacterType, typename UnsignedIntegerType>
+inline void writeNumberToBufferUnsigned(UnsignedIntegerType number, CharacterType* destination)
+{
+    return writeNumberToBufferImpl<CharacterType, UnsignedIntegerType, PositiveNumber>(number, destination);
+}
+
+
+template<typename UnsignedIntegerType, PositiveOrNegativeNumber NumberType>
+static unsigned lengthOfNumberAsStringImpl(UnsignedIntegerType number)
+{
+    unsigned length = 0;
+
+    do {
+        ++length;
+        number /= 10;
+    } while (number);
+
+    if (NumberType == NegativeNumber)
+        ++length;
+
+    return length;
+}
+
+template<typename SignedIntegerType>
+inline unsigned lengthOfNumberAsStringSigned(SignedIntegerType number)
+{
+    if (number < 0)
+        return lengthOfNumberAsStringImpl<typename std::make_unsigned<SignedIntegerType>::type, NegativeNumber>(-number);
+    return lengthOfNumberAsStringImpl<typename std::make_unsigned<SignedIntegerType>::type, PositiveNumber>(number);
+}
+
+template<typename UnsignedIntegerType>
+inline unsigned lengthOfNumberAsStringUnsigned(UnsignedIntegerType number)
+{
+    return lengthOfNumberAsStringImpl<UnsignedIntegerType, PositiveNumber>(number);
+}
+
+
 } // namespace WTF
 
 #endif // IntegerToStringConversion_h
index a8dada7..d02737a 100644 (file)
@@ -28,6 +28,7 @@
 #define StringBuilder_h
 
 #include <wtf/text/AtomicString.h>
+#include <wtf/text/IntegerToStringConversion.h>
 #include <wtf/text/StringView.h>
 #include <wtf/text/WTFString.h>
 
@@ -368,6 +369,12 @@ inline bool operator!=(const StringBuilder& a, const String& b) { return !equal(
 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b, a); }
 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal(b, a); }
 
+template<> struct IntegerToStringConversionTrait<StringBuilder> {
+    using ReturnType = void;
+    using AdditionalArgumentType = StringBuilder;
+    static void flush(LChar* characters, unsigned length, StringBuilder* stringBuilder) { stringBuilder->append(characters, length); }
+};
+
 } // namespace WTF
 
 using WTF::StringBuilder;
diff --git a/Source/WTF/wtf/text/StringConcatenateNumbers.h b/Source/WTF/wtf/text/StringConcatenateNumbers.h
new file mode 100644 (file)
index 0000000..293e745
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/dtoa.h>
+#include <wtf/text/IntegerToStringConversion.h>
+#include <wtf/text/StringConcatenate.h>
+
+namespace WTF {
+
+template<>
+class StringTypeAdapter<int> {
+public:
+    StringTypeAdapter<int>(int number)
+        : m_number(number)
+    {
+    }
+
+    unsigned length() const { return lengthOfNumberAsStringSigned(m_number); }
+    bool is8Bit() const { return true; }
+
+    void writeTo(LChar* destination) const { writeNumberToBufferSigned(m_number, destination); }
+    void writeTo(UChar* destination) const { writeNumberToBufferSigned(m_number, destination); }
+
+    String toString() const { return String::number(m_number); }
+
+private:
+    int m_number;
+};
+
+template<>
+class StringTypeAdapter<unsigned> {
+public:
+    StringTypeAdapter<unsigned>(unsigned number)
+        : m_number(number)
+    {
+    }
+
+    unsigned length() const { return lengthOfNumberAsStringUnsigned(m_number); }
+    bool is8Bit() const { return true; }
+
+    void writeTo(LChar* destination) const { writeNumberToBufferUnsigned(m_number, destination); }
+    void writeTo(UChar* destination) const { writeNumberToBufferUnsigned(m_number, destination); }
+
+    String toString() const { return String::number(m_number); }
+
+private:
+    unsigned m_number;
+};
+
+template<>
+class StringTypeAdapter<double> {
+public:
+    StringTypeAdapter<double>(double number)
+    {
+        numberToString(number, m_buffer);
+        m_length = strlen(m_buffer);
+    }
+
+    unsigned length() const { return m_length; }
+    bool is8Bit() const { return true; }
+
+    void writeTo(LChar* destination) const
+    {
+        for (unsigned i = 0; i < m_length; ++i)
+            destination[i] = m_buffer[i];
+    }
+
+    void writeTo(UChar* destination) const
+    {
+        for (unsigned i = 0; i < m_length; ++i)
+            destination[i] = m_buffer[i];
+    }
+
+    String toString() const { return { m_buffer, m_length }; }
+
+private:
+    NumberToStringBuffer m_buffer;
+    unsigned m_length;
+};
+
+template<>
+class StringTypeAdapter<float> : public StringTypeAdapter<double> {
+public:
+    StringTypeAdapter<float>(float number)
+        : StringTypeAdapter<double>(number)
+    {
+    }
+};
+
+class FormattedNumber {
+public:
+    static FormattedNumber fixedPrecision(double number, unsigned significantFigures = 6, bool truncateTrailingZeros = false)
+    {
+        FormattedNumber numberFormatter;
+        numberToFixedPrecisionString(number, significantFigures, numberFormatter.m_buffer, truncateTrailingZeros);
+        numberFormatter.m_length = strlen(numberFormatter.m_buffer);
+        return numberFormatter;
+    }
+
+    static FormattedNumber fixedWidth(double number, unsigned decimalPlaces)
+    {
+        FormattedNumber numberFormatter;
+        numberToFixedWidthString(number, decimalPlaces, numberFormatter.m_buffer);
+        numberFormatter.m_length = strlen(numberFormatter.m_buffer);
+        return numberFormatter;
+    }
+
+    unsigned length() const { return m_length; }
+    const LChar* buffer() const { return reinterpret_cast<const LChar*>(m_buffer); }
+
+private:
+    NumberToStringBuffer m_buffer;
+    unsigned m_length;
+};
+
+template<>
+class StringTypeAdapter<FormattedNumber> {
+public:
+    StringTypeAdapter<FormattedNumber>(const FormattedNumber& numberFormatter)
+        : m_numberFormatter(numberFormatter)
+    {
+    }
+
+    unsigned length() const { return m_numberFormatter.length(); }
+    bool is8Bit() const { return true; }
+
+    void writeTo(LChar* destination) const
+    {
+        auto buffer = m_numberFormatter.buffer();
+        auto length = m_numberFormatter.length();
+        for (unsigned i = 0; i < length; ++i)
+            destination[i] = buffer[i];
+    }
+
+    void writeTo(UChar* destination) const
+    {
+        auto buffer = m_numberFormatter.buffer();
+        auto length = m_numberFormatter.length();
+        for (unsigned i = 0; i < length; ++i)
+            destination[i] = buffer[i];
+    }
+
+    String toString() const { return { m_numberFormatter.buffer(), m_numberFormatter.length() }; }
+
+private:
+    const FormattedNumber& m_numberFormatter;
+};
+
+}
+
+using WTF::FormattedNumber;
index 01c7681..cb4232d 100644 (file)
@@ -26,6 +26,7 @@
 // on systems without case-sensitive file systems.
 
 #include <wtf/text/ASCIIFastPath.h>
+#include <wtf/text/IntegerToStringConversion.h>
 #include <wtf/text/StringImpl.h>
 
 #ifdef __OBJC__
@@ -713,6 +714,12 @@ template<unsigned length> inline bool startsWithLettersIgnoringASCIICase(const S
     return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
 }
 
+template<> struct IntegerToStringConversionTrait<String> {
+    using ReturnType = String;
+    using AdditionalArgumentType = void;
+    static String flush(LChar* characters, unsigned length, void*) { return { characters, length }; }
+};
+
 }
 
 using WTF::CString;
index 11acc13..d6ae048 100644 (file)
@@ -1,3 +1,14 @@
+2017-01-15  Sam Weinig  <sam@webkit.org>
+
+        Add the ability to use numbers in makeString()
+        https://bugs.webkit.org/show_bug.cgi?id=167087
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WTF/StringConcatenate.cpp: Added.
+        Add tests for StringConcatenate, focusing on new numeric additions.
+
 2017-01-15  Michael Catanzaro  <mcatanzaro@igalia.com>
 
         Unreviewed, remove stale comment.
index 032882e..9dbe74b 100644 (file)
                7CCE7F2D1A411B1000447C4C /* UserContentTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = 2943BE84161DFEB800999E3D /* UserContentTest.mm */; };
                7CCE7F2E1A411B1000447C4C /* WKBrowsingContextGroupTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC3C4C7D14587AA60025FB62 /* WKBrowsingContextGroupTest.mm */; };
                7CCE7F2F1A411B1000447C4C /* WKBrowsingContextLoadDelegateTest.mm in Sources */ = {isa = PBXBuildFile; fileRef = BC3C4C7014575B6A0025FB62 /* WKBrowsingContextLoadDelegateTest.mm */; };
+               7CD4C26E1E2C0E6E00929470 /* StringConcatenate.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CD4C26C1E2C0E6E00929470 /* StringConcatenate.cpp */; };
                7CEFA9661AC0B9E200B910FD /* _WKUserContentExtensionStore.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7CEFA9641AC0B9E200B910FD /* _WKUserContentExtensionStore.mm */; };
                7CFBCAE51743238F00B2BFCF /* WillLoad_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */; };
                8349D3C21DB96DDE004A9F65 /* ContextMenuDownload.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8349D3C11DB96DDA004A9F65 /* ContextMenuDownload.mm */; };
                7CCE7E8C1A41144E00447C4C /* libTestWebKitAPI.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libTestWebKitAPI.a; sourceTree = BUILT_PRODUCTS_DIR; };
                7CCE7EA31A4115CB00447C4C /* TestWebKitAPILibrary.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = TestWebKitAPILibrary.xcconfig; sourceTree = "<group>"; };
                7CD0D5AA1D5534DE000CC9E1 /* Variant.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Variant.cpp; sourceTree = "<group>"; };
+               7CD4C26C1E2C0E6E00929470 /* StringConcatenate.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StringConcatenate.cpp; sourceTree = "<group>"; };
                7CEFA9641AC0B9E200B910FD /* _WKUserContentExtensionStore.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = _WKUserContentExtensionStore.mm; sourceTree = "<group>"; };
                7CFBCADD1743234F00B2BFCF /* WillLoad.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad.cpp; sourceTree = "<group>"; };
                7CFBCAE31743238E00B2BFCF /* WillLoad_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WillLoad_Bundle.cpp; sourceTree = "<group>"; };
                BC9096461255618900083756 /* WTF */ = {
                        isa = PBXGroup;
                        children = (
-                               AD7C434C1DD2A5470026888B /* Expected.cpp */,
                                C0991C4F143C7D68007998F2 /* cf */,
                                7CBBA07519BB8A0900BBF025 /* darwin */,
                                BC029B1A1486B23800817DA9 /* ns */,
                                1A3524A91D627BD40031729B /* DeletedAddressOfOperator.h */,
                                E4A757D3178AEA5B00B5D7A4 /* Deque.cpp */,
                                1AF7B21D1D6CD12E008C126C /* EnumTraits.cpp */,
+                               AD7C434C1DD2A5470026888B /* Expected.cpp */,
                                7A38D7E51C752D5F004F157D /* HashCountedSet.cpp */,
                                0BCD833414857CE400EA2003 /* HashMap.cpp */,
                                26B2DFF815BDE599004F691D /* HashSet.cpp */,
                                DC69AA621CF77C6500C6272F /* ScopedLambda.cpp */,
                                CD5393C91757BAC400C07123 /* SHA1.cpp */,
                                81B50192140F232300D9EB58 /* StringBuilder.cpp */,
+                               7CD4C26C1E2C0E6E00929470 /* StringConcatenate.cpp */,
                                93ABA80816DDAB91002DB2FA /* StringHasher.cpp */,
                                26F1B44315CA434F00D1E4BF /* StringImpl.cpp */,
                                C01363C713C3997300EF3964 /* StringOperators.cpp */,
                                1AF7B21F1D6CD14D008C126C /* EnumTraits.cpp in Sources */,
                                7C83DEEF1D0A590C00FEBCF3 /* MD5.cpp in Sources */,
                                7C83DEF11D0A590C00FEBCF3 /* MediaTime.cpp in Sources */,
+                               7CD4C26E1E2C0E6E00929470 /* StringConcatenate.cpp in Sources */,
                                7C83DEF61D0A590C00FEBCF3 /* MetaAllocator.cpp in Sources */,
                                7C83DEFE1D0A590C00FEBCF3 /* NakedPtr.cpp in Sources */,
                                531C1D8E1DF8EF72006E979F /* LEBDecoder.cpp in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WTF/StringConcatenate.cpp b/Tools/TestWebKitAPI/Tests/WTF/StringConcatenate.cpp
new file mode 100644 (file)
index 0000000..fc25b08
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "WTFStringUtilities.h"
+#include <wtf/text/StringConcatenate.h>
+#include <wtf/text/StringConcatenateNumbers.h>
+
+namespace TestWebKitAPI {
+
+TEST(WTF, StringConcatenate)
+{
+    EXPECT_EQ("hello world", makeString("hello", " ", "world"));
+}
+
+TEST(WTF, StringConcatenate_Int)
+{
+    EXPECT_EQ(5u, WTF::lengthOfNumberAsStringSigned(17890));
+    EXPECT_EQ("hello 17890 world", makeString("hello ", 17890 , " world"));
+
+    EXPECT_EQ(6u, WTF::lengthOfNumberAsStringSigned(-17890));
+    EXPECT_EQ("hello -17890 world", makeString("hello ", -17890 , " world"));
+
+    EXPECT_EQ(1u, WTF::lengthOfNumberAsStringSigned(0));
+    EXPECT_EQ("hello 0 world", makeString("hello ", 0 , " world"));
+}
+
+TEST(WTF, StringConcatenate_Unsigned)
+{
+    EXPECT_EQ(5u, WTF::lengthOfNumberAsStringUnsigned(17890u));
+    EXPECT_EQ("hello 17890 world", makeString("hello ", 17890u , " world"));
+
+    EXPECT_EQ(1u, WTF::lengthOfNumberAsStringSigned(0u));
+    EXPECT_EQ("hello 0 world", makeString("hello ", 0u , " world"));
+}
+
+TEST(WTF, StringConcatenate_Float)
+{
+    EXPECT_EQ("hello 17890 world", makeString("hello ", 17890.0f , " world"));
+    EXPECT_EQ("hello 17890.5 world", makeString("hello ", 17890.5f , " world"));
+
+    EXPECT_EQ("hello -17890 world", makeString("hello ", -17890.0f , " world"));
+    EXPECT_EQ("hello -17890.5 world", makeString("hello ", -17890.5f , " world"));
+
+    EXPECT_EQ("hello 0 world", makeString("hello ", 0.0f , " world"));
+}
+
+TEST(WTF, StringConcatenate_Double)
+{
+    EXPECT_EQ("hello 17890 world", makeString("hello ", 17890.0 , " world"));
+    EXPECT_EQ("hello 17890.5 world", makeString("hello ", 17890.5 , " world"));
+
+    EXPECT_EQ("hello -17890 world", makeString("hello ", -17890.0 , " world"));
+    EXPECT_EQ("hello -17890.5 world", makeString("hello ", -17890.5 , " world"));
+
+    EXPECT_EQ("hello 0 world", makeString("hello ", 0.0 , " world"));
+}
+
+TEST(WTF, StringConcatenate_FormattedDoubleFixedPrecision)
+{
+    EXPECT_EQ("hello 17890.0 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.0) , " world"));
+    EXPECT_EQ("hello 1.79e+4 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.0, 3) , " world"));
+    EXPECT_EQ("hello 17890.000 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.0, 8) , " world"));
+    EXPECT_EQ("hello 17890 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.0, 8, true) , " world"));
+
+    EXPECT_EQ("hello 17890.5 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.5) , " world"));
+    EXPECT_EQ("hello 1.79e+4 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.5, 3) , " world"));
+    EXPECT_EQ("hello 17890.500 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.5, 8) , " world"));
+    EXPECT_EQ("hello 17890.5 world", makeString("hello ", FormattedNumber::fixedPrecision(17890.5, 8, true) , " world"));
+
+    EXPECT_EQ("hello -17890.0 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.0) , " world"));
+    EXPECT_EQ("hello -1.79e+4 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.0, 3) , " world"));
+    EXPECT_EQ("hello -17890.000 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.0, 8) , " world"));
+    EXPECT_EQ("hello -17890 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.0, 8, true) , " world"));
+
+    EXPECT_EQ("hello -17890.5 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.5) , " world"));
+    EXPECT_EQ("hello -1.79e+4 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.5, 3) , " world"));
+    EXPECT_EQ("hello -17890.500 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.5, 8) , " world"));
+    EXPECT_EQ("hello -17890.5 world", makeString("hello ", FormattedNumber::fixedPrecision(-17890.5, 8, true) , " world"));
+
+    EXPECT_EQ("hello 0.00000 world", makeString("hello ", FormattedNumber::fixedPrecision(0.0) , " world"));
+    EXPECT_EQ("hello 0.00 world", makeString("hello ", FormattedNumber::fixedPrecision(0.0, 3) , " world"));
+    EXPECT_EQ("hello 0.0000000 world", makeString("hello ", FormattedNumber::fixedPrecision(0.0, 8) , " world"));
+    EXPECT_EQ("hello 0 world", makeString("hello ", FormattedNumber::fixedPrecision(0.0, 8, true) , " world"));
+}
+
+TEST(WTF, StringConcatenate_FormattedDoubleFixedWidth)
+{
+    EXPECT_EQ("hello 17890.000 world", makeString("hello ", FormattedNumber::fixedWidth(17890.0, 3) , " world"));
+    EXPECT_EQ("hello 17890.500 world", makeString("hello ", FormattedNumber::fixedWidth(17890.5, 3) , " world"));
+
+    EXPECT_EQ("hello -17890.000 world", makeString("hello ", FormattedNumber::fixedWidth(-17890.0, 3) , " world"));
+    EXPECT_EQ("hello -17890.500 world", makeString("hello ", FormattedNumber::fixedWidth(-17890.5, 3) , " world"));
+
+    EXPECT_EQ("hello 0.000 world", makeString("hello ", FormattedNumber::fixedWidth(0.0, 3) , " world"));
+}
+
+}