Skip to content

Commit

Permalink
Merge r242308 - Finish removing String::format
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
darinadler authored and carlosgcampos committed Mar 5, 2019
1 parent 6d24db4 commit 5c57e25
Show file tree
Hide file tree
Showing 29 changed files with 337 additions and 257 deletions.
11 changes: 11 additions & 0 deletions Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,14 @@
2019-03-01 Darin Adler <darin@apple.com>

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

Reviewed by Daniel Bates.

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

2019-03-01 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] Fix FTL build on ARM32_64 by adding stubs for JSRopeString::offsetOfXXX
Expand Down
5 changes: 3 additions & 2 deletions Source/JavaScriptCore/bytecode/CodeBlock.cpp
Expand Up @@ -92,6 +92,7 @@
#include <wtf/Forward.h>
#include <wtf/SimpleStats.h>
#include <wtf/StringPrintStream.h>
#include <wtf/text/StringConcatenateNumbers.h>
#include <wtf/text/UniquedStringImpl.h>

#if ENABLE(ASSEMBLER)
Expand Down Expand Up @@ -2888,9 +2889,9 @@ String CodeBlock::nameForRegister(VirtualRegister virtualRegister)
if (virtualRegister == thisRegister())
return "this"_s;
if (virtualRegister.isArgument())
return String::format("arguments[%3d]", virtualRegister.toArgument());
return makeString("arguments[", pad(' ', 3, virtualRegister.toArgument()), ']');

return "";
return emptyString();
}

ValueProfile* CodeBlock::tryGetValueProfileForBytecodeOffset(int bytecodeOffset)
Expand Down
34 changes: 34 additions & 0 deletions Source/WTF/ChangeLog
@@ -1,3 +1,37 @@
2019-03-01 Darin Adler <darin@apple.com>

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

Reviewed by Daniel Bates.

* 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.

2019-02-28 Yusuke Suzuki <ysuzuki@apple.com>

[JSC] sizeof(JSString) should be 16
Expand Down
52 changes: 51 additions & 1 deletion Source/WTF/wtf/Assertions.cpp
Expand Up @@ -74,6 +74,56 @@
#include <unistd.h>
#endif

namespace WTF {

WTF_ATTRIBUTE_PRINTF(1, 0) static String createWithFormatAndArguments(const char* format, va_list args)
{
va_list argsCopy;
va_copy(argsCopy, args);

ALLOW_NONLITERAL_FORMAT_BEGIN

#if USE(CF) && !OS(WINDOWS)
if (strstr(format, "%@")) {
auto cfFormat = adoptCF(CFStringCreateWithCString(kCFAllocatorDefault, format, kCFStringEncodingUTF8));
auto result = adoptCF(CFStringCreateWithFormatAndArguments(kCFAllocatorDefault, nullptr, cfFormat.get(), args));
va_end(argsCopy);
return result.get();
}
#endif

// Do the format once to get the length.
#if COMPILER(MSVC)
int result = _vscprintf(format, args);
#else
char ch;
int result = vsnprintf(&ch, 1, format, args);
#endif

if (!result) {
va_end(argsCopy);
return emptyString();
}
if (result < 0) {
va_end(argsCopy);
return { };
}

Vector<char, 256> buffer;
unsigned length = result;
buffer.grow(length + 1);

// Now do the formatting again, guaranteed to fit.
vsnprintf(buffer.data(), buffer.size(), format, argsCopy);
va_end(argsCopy);

ALLOW_NONLITERAL_FORMAT_END

return StringImpl::create(reinterpret_cast<const LChar*>(buffer.data()), length);
}

}

extern "C" {

static void logToStderr(const char* buffer)
Expand Down Expand Up @@ -399,7 +449,7 @@ void WTFLog(WTFLogChannel* channel, const char* format, ...)
va_start(args, format);

ALLOW_NONLITERAL_FORMAT_BEGIN
String loggingString = String::format(format, args);
String loggingString = WTF::createWithFormatAndArguments(format, args);
ALLOW_NONLITERAL_FORMAT_END

va_end(args);
Expand Down
1 change: 0 additions & 1 deletion Source/WTF/wtf/HexNumber.h
Expand Up @@ -102,7 +102,6 @@ template<> class StringTypeAdapter<HexNumberBuffer> {
unsigned length() const { return m_buffer.length; }
bool is8Bit() const { return true; }
template<typename CharacterType> void writeTo(CharacterType* destination) const { StringImpl::copyCharacters(destination, characters(), length()); }
String toString() const { return { characters(), length() }; }

private:
const LChar* characters() const { return &*(m_buffer.characters.end() - length()); }
Expand Down

0 comments on commit 5c57e25

Please sign in to comment.