瀏覽代碼

LibJS: Use string::formatted() in to_string() functions

Linus Groh 4 年之前
父節點
當前提交
2e2571743b

+ 1 - 1
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"; }

+ 1 - 1
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) \

+ 4 - 5
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);
 }
 

+ 1 - 1
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<MarkedValueList> arguments)

+ 1 - 1
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)

+ 1 - 1
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()));
 }
 
 }

+ 1 - 1
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"; }

+ 1 - 1
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 "<accessor>";
     case Type::NativeProperty: