Title: [221203] trunk
Revision
221203
Author
[email protected]
Date
2017-08-25 13:23:57 -0700 (Fri, 25 Aug 2017)

Log Message

Add String::format variant that takes va_args
https://bugs.webkit.org/show_bug.cgi?id=175988

Reviewed by Jer Noble.

Source/WTF:

* wtf/text/WTFString.cpp:
(WTF::createWithFormatAndArguments): Created with the guts of String::format.
(WTF::String::formatWithArguments): New, call createWithFormatAndArguments.
(WTF::String::format): Move logic to createWithFormatAndArguments, use it.
* wtf/text/WTFString.h:

Tools:

* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::testWithFormatAndArguments):
(TestWebKitAPI::TEST):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (221202 => 221203)


--- trunk/Source/WTF/ChangeLog	2017-08-25 20:10:51 UTC (rev 221202)
+++ trunk/Source/WTF/ChangeLog	2017-08-25 20:23:57 UTC (rev 221203)
@@ -1,3 +1,16 @@
+2017-08-25  Eric Carlson  <[email protected]>
+
+        Add String::format variant that takes va_args
+        https://bugs.webkit.org/show_bug.cgi?id=175988
+
+        Reviewed by Jer Noble.
+
+        * wtf/text/WTFString.cpp:
+        (WTF::createWithFormatAndArguments): Created with the guts of String::format.
+        (WTF::String::formatWithArguments): New, call createWithFormatAndArguments.
+        (WTF::String::format): Move logic to createWithFormatAndArguments, use it.
+        * wtf/text/WTFString.h:
+
 2017-08-25  Saam Barati  <[email protected]>
 
         Support compiling catch in the DFG

Modified: trunk/Source/WTF/wtf/text/WTFString.cpp (221202 => 221203)


--- trunk/Source/WTF/wtf/text/WTFString.cpp	2017-08-25 20:10:51 UTC (rev 221202)
+++ trunk/Source/WTF/wtf/text/WTFString.cpp	2017-08-25 20:23:57 UTC (rev 221203)
@@ -465,23 +465,21 @@
     return result;
 }
 
-String String::format(const char *format, ...)
+static String createWithFormatAndArguments(const char *format, va_list args)
 {
-    va_list args;
-    va_start(args, format);
+    va_list argsCopy;
+    va_copy(argsCopy, args);
 
+#if COMPILER(CLANG)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-nonliteral"
+#endif
+
 #if USE(CF) && !OS(WINDOWS)
     if (strstr(format, "%@")) {
         RetainPtr<CFStringRef> cfFormat = adoptCF(CFStringCreateWithCString(kCFAllocatorDefault, format, kCFStringEncodingUTF8));
 
-#if COMPILER(CLANG)
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wformat-nonliteral"
-#endif
         RetainPtr<CFStringRef> result = adoptCF(CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, nullptr, cfFormat.get(), args));
-#if COMPILER(CLANG)
-#pragma clang diagnostic pop
-#endif
 
         va_end(args);
         return result.get();
@@ -505,14 +503,31 @@
     Vector<char, 256> buffer;
     unsigned len = result;
     buffer.grow(len + 1);
-    
-    va_start(args, format);
+
     // Now do the formatting again, guaranteed to fit.
-    vsnprintf(buffer.data(), buffer.size(), format, args);
+    vsnprintf(buffer.data(), buffer.size(), format, argsCopy);
+    va_end(argsCopy);
 
+#if COMPILER(CLANG)
+#pragma clang diagnostic pop
+#endif
+
+    return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), len);
+}
+
+String String::formatWithArguments(const char *format, va_list args)
+{
+    return createWithFormatAndArguments(format, args);
+}
+
+String String::format(const char *format, ...)
+{
+    va_list args;
+    va_start(args, format);
+    String result = createWithFormatAndArguments(format, args);
     va_end(args);
     
-    return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), len);
+    return result;
 }
 
 String String::number(int number)

Modified: trunk/Source/WTF/wtf/text/WTFString.h (221202 => 221203)


--- trunk/Source/WTF/wtf/text/WTFString.h	2017-08-25 20:10:51 UTC (rev 221202)
+++ trunk/Source/WTF/wtf/text/WTFString.h	2017-08-25 20:23:57 UTC (rev 221203)
@@ -25,6 +25,7 @@
 // This file would be called String.h, but that conflicts with <string.h>
 // on systems without case-sensitive file systems.
 
+#include <stdarg.h>
 #include <wtf/Function.h>
 #include <wtf/text/ASCIIFastPath.h>
 #include <wtf/text/IntegerToStringConversion.h>
@@ -353,6 +354,7 @@
     WTF_EXPORT_STRING_API String foldCase() const;
 
     WTF_EXPORT_STRING_API static String format(const char *, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
+    WTF_EXPORT_STRING_API static String formatWithArguments(const char *, va_list) WTF_ATTRIBUTE_PRINTF(1, 0);
 
     // Returns an uninitialized string. The characters needs to be written
     // into the buffer returned in data before the returned string is used.

Modified: trunk/Tools/ChangeLog (221202 => 221203)


--- trunk/Tools/ChangeLog	2017-08-25 20:10:51 UTC (rev 221202)
+++ trunk/Tools/ChangeLog	2017-08-25 20:23:57 UTC (rev 221203)
@@ -1,3 +1,14 @@
+2017-08-25  Eric Carlson  <[email protected]>
+
+        Add String::format variant that takes va_args
+        https://bugs.webkit.org/show_bug.cgi?id=175988
+
+        Reviewed by Jer Noble.
+
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::testWithFormatAndArguments):
+        (TestWebKitAPI::TEST):
+
 2017-08-25  Jonathan Bedard  <[email protected]>
 
         Follow-up Internal build fix for r221187

Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp (221202 => 221203)


--- trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2017-08-25 20:10:51 UTC (rev 221202)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp	2017-08-25 20:23:57 UTC (rev 221203)
@@ -362,4 +362,43 @@
     EXPECT_EQ(reference.reverseFind('c', 4), notFound);
 }
 
+WTF_ATTRIBUTE_PRINTF(2, 3)
+static void testWithFormatAndArguments(const char* expected, const char* format, ...)
+{
+    va_list arguments;
+    va_start(arguments, format);
+
+#if COMPILER(GCC_OR_CLANG)
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#endif
+    String result = String::formatWithArguments(format, arguments);
+#if COMPILER(GCC_OR_CLANG)
+#pragma GCC diagnostic pop
+#endif
+
+    va_end(arguments);
+
+    EXPECT_STREQ(expected, result.utf8().data());
+}
+
+TEST(WTF, StringFormatWithArguments)
+{
+    testWithFormatAndArguments("hello cruel world", "%s %s %s", "hello", "cruel" , "world");
+
+    testWithFormatAndArguments("hello 17890 world", "%s%u%s", "hello ", 17890u, " world");
+
+    testWithFormatAndArguments("hello 17890.000 world", "%s %.3f %s", "hello", 17890.0f, "world");
+    testWithFormatAndArguments("hello 17890.50 world", "%s %.2f %s", "hello", 17890.5f, "world");
+
+    testWithFormatAndArguments("hello -17890 world", "%s %.0f %s", "hello", -17890.0f, "world");
+    testWithFormatAndArguments("hello -17890.5 world", "%s %.1f %s", "hello", -17890.5f, "world");
+
+    testWithFormatAndArguments("hello 17890 world", "%s %.0f %s", "hello", 17890.0, "world");
+    testWithFormatAndArguments("hello 17890.5 world", "%s %.1f %s", "hello", 17890.5, "world");
+
+    testWithFormatAndArguments("hello -17890 world", "%s %.0f %s", "hello", -17890.0, "world");
+    testWithFormatAndArguments("hello -17890.5 world", "%s %.1f %s", "hello", -17890.5, "world");
+}
+
 } // namespace TestWebKitAPI
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to