mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 18:10:56 +09:00
LibJS: Use String::formatted() in various other places
This commit is contained in:
parent
2e2571743b
commit
123f98201e
Notes:
sideshowbarker
2024-07-19 02:03:54 +09:00
Author: https://github.com/linusg
Commit: 123f98201e
Pull-request: https://github.com/SerenityOS/serenity/pull/3681
7 changed files with 23 additions and 22 deletions
|
@ -78,7 +78,7 @@ static void update_function_name(Value& value, const FlyString& name)
|
||||||
static String get_function_name(GlobalObject& global_object, Value value)
|
static String get_function_name(GlobalObject& global_object, Value value)
|
||||||
{
|
{
|
||||||
if (value.is_symbol())
|
if (value.is_symbol())
|
||||||
return String::format("[%s]", value.as_symbol().description().characters());
|
return String::formatted("[{}]", value.as_symbol().description());
|
||||||
if (value.is_string())
|
if (value.is_string())
|
||||||
return value.as_string().string();
|
return value.as_string().string();
|
||||||
return value.to_string(global_object);
|
return value.to_string(global_object);
|
||||||
|
@ -743,9 +743,9 @@ Value ClassExpression::execute(Interpreter& interpreter, GlobalObject& global_ob
|
||||||
String accessor_name = [&] {
|
String accessor_name = [&] {
|
||||||
switch (method.kind()) {
|
switch (method.kind()) {
|
||||||
case ClassMethod::Kind::Getter:
|
case ClassMethod::Kind::Getter:
|
||||||
return String::format("get %s", get_function_name(global_object, key).characters());
|
return String::formatted("get {}", get_function_name(global_object, key));
|
||||||
case ClassMethod::Kind::Setter:
|
case ClassMethod::Kind::Setter:
|
||||||
return String::format("set %s", get_function_name(global_object, key).characters());
|
return String::formatted("set {}", get_function_name(global_object, key));
|
||||||
default:
|
default:
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
|
@ -1538,9 +1538,9 @@ Value ObjectExpression::execute(Interpreter& interpreter, GlobalObject& global_o
|
||||||
|
|
||||||
String name = get_function_name(global_object, key);
|
String name = get_function_name(global_object, key);
|
||||||
if (property.type() == ObjectProperty::Type::Getter) {
|
if (property.type() == ObjectProperty::Type::Getter) {
|
||||||
name = String::format("get %s", name.characters());
|
name = String::formatted("get {}", name);
|
||||||
} else if (property.type() == ObjectProperty::Type::Setter) {
|
} else if (property.type() == ObjectProperty::Type::Setter) {
|
||||||
name = String::format("set %s", name.characters());
|
name = String::formatted("set {}", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
update_function_name(value, name);
|
update_function_name(value, name);
|
||||||
|
@ -1597,9 +1597,9 @@ String MemberExpression::to_string_approximation() const
|
||||||
if (m_object->is_identifier())
|
if (m_object->is_identifier())
|
||||||
object_string = static_cast<const Identifier&>(*m_object).string();
|
object_string = static_cast<const Identifier&>(*m_object).string();
|
||||||
if (is_computed())
|
if (is_computed())
|
||||||
return String::format("%s[<computed>]", object_string.characters());
|
return String::formatted("{}[<computed>]", object_string);
|
||||||
ASSERT(m_property->is_identifier());
|
ASSERT(m_property->is_identifier());
|
||||||
return String::format("%s.%s", object_string.characters(), static_cast<const Identifier&>(*m_property).string().characters());
|
return String::formatted("{}.{}", object_string, static_cast<const Identifier&>(*m_property).string());
|
||||||
}
|
}
|
||||||
|
|
||||||
Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
Value MemberExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
|
||||||
|
|
|
@ -33,7 +33,7 @@ BoundFunction::BoundFunction(GlobalObject& global_object, Function& target_funct
|
||||||
: Function::Function(*global_object.function_prototype(), bound_this, move(arguments))
|
: Function::Function(*global_object.function_prototype(), bound_this, move(arguments))
|
||||||
, m_target_function(&target_function)
|
, m_target_function(&target_function)
|
||||||
, m_constructor_prototype(constructor_prototype)
|
, m_constructor_prototype(constructor_prototype)
|
||||||
, m_name(String::format("bound %s", target_function.name().characters()))
|
, m_name(String::formatted("bound {}", target_function.name()))
|
||||||
, m_length(length)
|
, m_length(length)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,7 +68,7 @@ public:
|
||||||
String time_string() const { return m_datetime.to_string("%T GMT+0000 (UTC)"); }
|
String time_string() const { return m_datetime.to_string("%T GMT+0000 (UTC)"); }
|
||||||
String string() const
|
String string() const
|
||||||
{
|
{
|
||||||
return String::format("%s %s", date_string().characters(), time_string().characters());
|
return String::formatted("{} {}", date_string(), time_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
String iso_date_string() const;
|
String iso_date_string() const;
|
||||||
|
|
|
@ -80,7 +80,7 @@ Value FunctionConstructor::construct(Function&)
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
auto source = String::format("function anonymous(%s) { %s }", parameters_source.characters(), body_source.characters());
|
auto source = String::formatted("function anonymous({}) {{ {} }}", parameters_source, body_source);
|
||||||
auto parser = Parser(Lexer(source));
|
auto parser = Parser(Lexer(source));
|
||||||
auto function_expression = parser.parse_function_node<FunctionExpression>();
|
auto function_expression = parser.parse_function_node<FunctionExpression>();
|
||||||
if (parser.has_errors()) {
|
if (parser.has_errors()) {
|
||||||
|
|
|
@ -207,7 +207,7 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
|
||||||
|
|
||||||
state.seen_objects.set(&object);
|
state.seen_objects.set(&object);
|
||||||
String previous_indent = state.indent;
|
String previous_indent = state.indent;
|
||||||
state.indent = String::format("%s%s", state.indent.characters(), state.gap.characters());
|
state.indent = String::formatted("{}{}", state.indent, state.gap);
|
||||||
Vector<String> property_strings;
|
Vector<String> property_strings;
|
||||||
|
|
||||||
auto process_property = [&](const PropertyName& key) {
|
auto process_property = [&](const PropertyName& key) {
|
||||||
|
@ -215,11 +215,11 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
|
||||||
if (vm.exception())
|
if (vm.exception())
|
||||||
return;
|
return;
|
||||||
if (!serialized_property_string.is_null()) {
|
if (!serialized_property_string.is_null()) {
|
||||||
property_strings.append(String::format(
|
property_strings.append(String::formatted(
|
||||||
"%s:%s%s",
|
"{}:{}{}",
|
||||||
quote_json_string(key.to_string()).characters(),
|
quote_json_string(key.to_string()),
|
||||||
state.gap.is_empty() ? "" : " ",
|
state.gap.is_empty() ? "" : " ",
|
||||||
serialized_property_string.characters()));
|
serialized_property_string));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -263,7 +263,7 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
|
||||||
} else {
|
} else {
|
||||||
builder.append('\n');
|
builder.append('\n');
|
||||||
builder.append(state.indent);
|
builder.append(state.indent);
|
||||||
auto separator = String::format(",\n%s", state.indent.characters());
|
auto separator = String::formatted(",\n{}", state.indent);
|
||||||
for (auto& property_string : property_strings) {
|
for (auto& property_string : property_strings) {
|
||||||
if (!first)
|
if (!first)
|
||||||
builder.append(separator);
|
builder.append(separator);
|
||||||
|
@ -291,7 +291,7 @@ String JSONObject::serialize_json_array(GlobalObject& global_object, StringifySt
|
||||||
|
|
||||||
state.seen_objects.set(&object);
|
state.seen_objects.set(&object);
|
||||||
String previous_indent = state.indent;
|
String previous_indent = state.indent;
|
||||||
state.indent = String::format("%s%s", state.indent.characters(), state.gap.characters());
|
state.indent = String::formatted("{}{}", state.indent, state.gap);
|
||||||
Vector<String> property_strings;
|
Vector<String> property_strings;
|
||||||
|
|
||||||
auto length = length_of_array_like(global_object, Value(&object));
|
auto length = length_of_array_like(global_object, Value(&object));
|
||||||
|
@ -327,7 +327,7 @@ String JSONObject::serialize_json_array(GlobalObject& global_object, StringifySt
|
||||||
} else {
|
} else {
|
||||||
builder.append("[\n");
|
builder.append("[\n");
|
||||||
builder.append(state.indent);
|
builder.append(state.indent);
|
||||||
auto separator = String::format(",\n%s", state.indent.characters());
|
auto separator = String::formatted(",\n{}", state.indent);
|
||||||
bool first = true;
|
bool first = true;
|
||||||
for (auto& property_string : property_strings) {
|
for (auto& property_string : property_strings) {
|
||||||
if (!first)
|
if (!first)
|
||||||
|
|
|
@ -747,7 +747,7 @@ bool Object::define_native_function(const StringOrSymbol& property_name, AK::Fun
|
||||||
if (property_name.is_string()) {
|
if (property_name.is_string()) {
|
||||||
function_name = property_name.as_string();
|
function_name = property_name.as_string();
|
||||||
} else {
|
} else {
|
||||||
function_name = String::format("[%s]", property_name.as_symbol()->description().characters());
|
function_name = String::formatted("[{}]", property_name.as_symbol()->description());
|
||||||
}
|
}
|
||||||
auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
|
auto* function = NativeFunction::create(global_object(), function_name, move(native_function));
|
||||||
function->define_property("length", Value(length), Attribute::Configurable);
|
function->define_property("length", Value(length), Attribute::Configurable);
|
||||||
|
|
|
@ -256,9 +256,10 @@ static Value pad_string(GlobalObject& global_object, const String& string, PadPl
|
||||||
filler_builder.append(fill_string);
|
filler_builder.append(fill_string);
|
||||||
auto filler = filler_builder.build().substring(0, fill_length);
|
auto filler = filler_builder.build().substring(0, fill_length);
|
||||||
|
|
||||||
if (placement == PadPlacement::Start)
|
auto formatted = placement == PadPlacement::Start
|
||||||
return js_string(vm, String::format("%s%s", filler.characters(), string.characters()));
|
? String::formatted("{}{}", filler, string)
|
||||||
return js_string(vm, String::format("%s%s", string.characters(), filler.characters()));
|
: String::formatted("{}{}", string, filler);
|
||||||
|
return js_string(vm, formatted);
|
||||||
}
|
}
|
||||||
|
|
||||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_start)
|
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::pad_start)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue