From 2e2571743b4612cbec6fd7037375abe3f43af380 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 4 Oct 2020 15:18:12 +0100 Subject: [PATCH] LibJS: Use string::formatted() in to_string() functions --- Libraries/LibJS/Runtime/BigInt.h | 2 +- Libraries/LibJS/Runtime/ErrorPrototype.cpp | 2 +- Libraries/LibJS/Runtime/FunctionPrototype.cpp | 9 ++++----- Libraries/LibJS/Runtime/Object.cpp | 2 +- Libraries/LibJS/Runtime/ObjectPrototype.cpp | 2 +- Libraries/LibJS/Runtime/RegExpObject.cpp | 2 +- Libraries/LibJS/Runtime/Symbol.h | 2 +- Libraries/LibJS/Runtime/Value.cpp | 2 +- 8 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Libraries/LibJS/Runtime/BigInt.h b/Libraries/LibJS/Runtime/BigInt.h index 0fcc16982d..34994d2019 100644 --- a/Libraries/LibJS/Runtime/BigInt.h +++ b/Libraries/LibJS/Runtime/BigInt.h @@ -38,7 +38,7 @@ public: virtual ~BigInt(); const Crypto::SignedBigInteger& big_integer() const { return m_big_integer; } - const String to_string() const { return String::format("%sn", m_big_integer.to_base10().characters()); } + const String to_string() const { return String::formatted("{}n", m_big_integer.to_base10()); } private: virtual const char* class_name() const override { return "BigInt"; } diff --git a/Libraries/LibJS/Runtime/ErrorPrototype.cpp b/Libraries/LibJS/Runtime/ErrorPrototype.cpp index 09b25b0d40..c448d3deb6 100644 --- a/Libraries/LibJS/Runtime/ErrorPrototype.cpp +++ b/Libraries/LibJS/Runtime/ErrorPrototype.cpp @@ -123,7 +123,7 @@ JS_DEFINE_NATIVE_FUNCTION(ErrorPrototype::to_string) return js_string(vm, message); if (message.length() == 0) return js_string(vm, name); - return js_string(vm, String::format("%s: %s", name.characters(), message.characters())); + return js_string(vm, String::formatted("{}: {}", name, message)); } #define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \ diff --git a/Libraries/LibJS/Runtime/FunctionPrototype.cpp b/Libraries/LibJS/Runtime/FunctionPrototype.cpp index 28011570a4..a166c3de41 100644 --- a/Libraries/LibJS/Runtime/FunctionPrototype.cpp +++ b/Libraries/LibJS/Runtime/FunctionPrototype.cpp @@ -148,7 +148,7 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::to_string) String function_body; if (this_object->is_native_function() || this_object->is_bound_function()) { - function_body = String::format(" [%s]", this_object->class_name()); + function_body = String::formatted(" [{}]", this_object->class_name()); } else { StringBuilder parameters_builder; auto first = true; @@ -169,10 +169,9 @@ JS_DEFINE_NATIVE_FUNCTION(FunctionPrototype::to_string) function_body = " ???"; } - auto function_source = String::format("function %s(%s) {\n%s\n}", - function_name.is_null() ? "" : function_name.characters(), - function_parameters.characters(), - function_body.characters()); + auto function_source = String::formatted( + "function {}({}) {{\n{}\n}}", + function_name.is_null() ? "" : function_name, function_parameters, function_body); return js_string(vm, function_source); } diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 7fd765baa6..5a1a17b69d 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -850,7 +850,7 @@ Value Object::to_string() const return {}; return string; } - return js_string(heap(), String::format("[object %s]", class_name())); + return js_string(heap(), String::formatted("[object {}]", class_name())); } Value Object::invoke(const StringOrSymbol& property_name, Optional arguments) diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index a7c6339f33..d01054d5c3 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -103,7 +103,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string) tag = "Object"; } - return js_string(vm, String::format("[object %s]", tag.characters())); + return js_string(vm, String::formatted("[object {}]", tag)); } JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_locale_string) diff --git a/Libraries/LibJS/Runtime/RegExpObject.cpp b/Libraries/LibJS/Runtime/RegExpObject.cpp index a2404b36c3..88dbcab7ea 100644 --- a/Libraries/LibJS/Runtime/RegExpObject.cpp +++ b/Libraries/LibJS/Runtime/RegExpObject.cpp @@ -51,7 +51,7 @@ RegExpObject::~RegExpObject() Value RegExpObject::to_string() const { - return js_string(heap(), String::format("/%s/%s", content().characters(), flags().characters())); + return js_string(heap(), String::formatted("/{}/{}", content(), flags())); } } diff --git a/Libraries/LibJS/Runtime/Symbol.h b/Libraries/LibJS/Runtime/Symbol.h index b0f5bb49fa..d01179add7 100644 --- a/Libraries/LibJS/Runtime/Symbol.h +++ b/Libraries/LibJS/Runtime/Symbol.h @@ -41,7 +41,7 @@ public: const String& description() const { return m_description; } bool is_global() const { return m_is_global; } - String to_string() const { return String::format("Symbol(%s)", description().characters()); } + String to_string() const { return String::formatted("Symbol({})", description()); } private: virtual const char* class_name() const override { return "Symbol"; } diff --git a/Libraries/LibJS/Runtime/Value.cpp b/Libraries/LibJS/Runtime/Value.cpp index 448bb68ab3..0ca094b4b3 100644 --- a/Libraries/LibJS/Runtime/Value.cpp +++ b/Libraries/LibJS/Runtime/Value.cpp @@ -123,7 +123,7 @@ String Value::to_string_without_side_effects() const case Type::BigInt: return m_value.as_bigint->to_string(); case Type::Object: - return String::format("[object %s]", as_object().class_name()); + return String::formatted("[object {}]", as_object().class_name()); case Type::Accessor: return ""; case Type::NativeProperty: