ladybird/Meta/Lagom/Tools/CodeGenerators/LibWeb/GeneratorUtil.h
DexesTTP 7ceeb74535 AK: Use an enum instead of a bool for String::replace(all_occurences)
This commit has no behavior changes.

In particular, this does not fix any of the wrong uses of the previous
default parameter (which used to be 'false', meaning "only replace the
first occurence in the string"). It simply replaces the default uses by
String::replace(..., ReplaceMode::FirstOnly), leaving them incorrect.
2022-07-06 11:12:45 +02:00

65 lines
1.7 KiB
C

/*
* Copyright (c) 2019-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, Samuel Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/JsonObject.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/Stream.h>
#include <ctype.h>
String title_casify(String const& dashy_name)
{
auto parts = dashy_name.split('-');
StringBuilder builder;
for (auto& part : parts) {
if (part.is_empty())
continue;
builder.append(toupper(part[0]));
if (part.length() == 1)
continue;
builder.append(part.substring_view(1, part.length() - 1));
}
return builder.to_string();
}
String camel_casify(StringView dashy_name)
{
auto parts = dashy_name.split_view('-');
StringBuilder builder;
bool first = true;
for (auto& part : parts) {
if (part.is_empty())
continue;
char ch = part[0];
if (!first)
ch = toupper(ch);
else
first = false;
builder.append(ch);
if (part.length() == 1)
continue;
builder.append(part.substring_view(1, part.length() - 1));
}
return builder.to_string();
}
String snake_casify(String const& dashy_name)
{
return dashy_name.replace("-", "_", ReplaceMode::All);
}
ErrorOr<JsonValue> read_entire_file_as_json(StringView filename)
{
auto file = TRY(Core::Stream::File::open(filename, Core::Stream::OpenMode::Read));
auto json_size = TRY(file->size());
auto json_data = TRY(ByteBuffer::create_uninitialized(json_size));
if (!file->read_or_error(json_data.bytes()))
return Error::from_string_literal("Failed to read json file.");
return JsonValue::from_string(json_data);
}