1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:17:35 +00:00

Userland: Change static const variables to static constexpr

`static const` variables can be computed and initialized at run-time
during initialization or the first time a function is called. Change
them to `static constexpr` to ensure they are computed at
compile-time.

This allows some removal of `strlen` because the length of the
`StringView` can be used which is pre-computed at compile-time.
This commit is contained in:
Lenny Maiorani 2022-03-13 16:09:41 -06:00 committed by Andreas Kling
parent 31515a9147
commit f912a48315
23 changed files with 111 additions and 82 deletions

View file

@ -20,8 +20,7 @@
namespace Audio {
static const String empty_string = "";
static String no_plugin_error = "No loader plugin available";
static constexpr StringView no_plugin_error = "No loader plugin available";
using LoaderSamples = Result<NonnullRefPtr<Buffer>, LoaderError>;
using MaybeLoaderError = Result<void, LoaderError>;

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <LibCore/DateTime.h>
#include <LibGUI/Calendar.h>
#include <LibGUI/Painter.h>
@ -16,21 +17,21 @@ REGISTER_WIDGET(GUI, Calendar);
namespace GUI {
static const char* long_day_names[] = {
static constexpr Array<StringView, 7> long_day_names = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
static const char* short_day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char* mini_day_names[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
static const char* micro_day_names[] = { "S", "M", "T", "W", "T", "F", "S" };
static constexpr Array<StringView, 7> short_day_names = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static constexpr Array<StringView, 7> mini_day_names = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
static constexpr Array<StringView, 7> micro_day_names = { "S", "M", "T", "W", "T", "F", "S" };
static const char* long_month_names[] = {
static constexpr Array<StringView, 12> long_month_names = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
static const char* short_month_names[] = {
static constexpr Array<StringView, 12> short_month_names = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

View file

@ -16,9 +16,9 @@ REGISTER_WIDGET(GUI, CheckBox)
namespace GUI {
static const int s_box_width = 13;
static const int s_box_height = 13;
static const int s_horizontal_padding = 6;
static constexpr int s_box_width = 13;
static constexpr int s_box_height = 13;
static constexpr int s_horizontal_padding = 6;
CheckBox::CheckBox(String text)
: AbstractButton(move(text))

View file

@ -1,9 +1,11 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <LibCore/ConfigFile.h>
@ -178,7 +180,10 @@ Icon FileIconProvider::icon_for_executable(const String& path)
int image_size;
};
static const IconSection icon_sections[] = { { .section_name = "serenity_icon_s", .image_size = 16 }, { .section_name = "serenity_icon_m", .image_size = 32 } };
static constexpr Array<IconSection, 2> icon_sections = {
IconSection { .section_name = "serenity_icon_s", .image_size = 16 },
IconSection { .section_name = "serenity_icon_m", .image_size = 32 }
};
bool had_error = false;
for (const auto& icon_section : icon_sections) {

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Alex McGrath <amk@amk.ie>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -48,7 +49,7 @@ void LinkLabel::set_hovered(bool hover)
void LinkLabel::mousemove_event(MouseEvent& event)
{
static const int extra_target_width = 3;
constexpr int extra_target_width = 3;
set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
}

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -16,8 +17,8 @@
namespace Gfx {
// Row strides and offsets for each interlace pass.
static const int INTERLACE_ROW_STRIDES[] = { 8, 8, 4, 2 };
static const int INTERLACE_ROW_OFFSETS[] = { 0, 4, 2, 1 };
static constexpr Array<int, 4> INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 };
static constexpr Array<int, 4> INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 };
struct GIFImageDescriptor {
u16 x { 0 };

View file

@ -1,9 +1,11 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/Endian.h>
#include <AK/Vector.h>
@ -17,7 +19,7 @@
namespace Gfx {
static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
static constexpr Array<u8, 8> png_header = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
struct PNG_IHDR {
NetworkOrdered<u32> width;
@ -516,7 +518,7 @@ static bool decode_png_header(PNGLoadingContext& context)
return false;
}
if (memcmp(context.data, png_header, sizeof(png_header)) != 0) {
if (memcmp(context.data, png_header.span().data(), sizeof(png_header)) != 0) {
dbgln_if(PNG_DEBUG, "Invalid PNG header");
context.state = PNGLoadingContext::State::Error;
return false;

View file

@ -1,10 +1,12 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Array.h>
#include <AK/Function.h>
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/AbstractOperations.h>
@ -19,7 +21,7 @@
namespace JS {
static const u8 max_precision_for_radix[37] = {
static constexpr AK::Array<u8, 37> max_precision_for_radix = {
// clang-format off
0, 0, 52, 32, 26, 22, 20, 18, 17, 16,
15, 15, 14, 14, 13, 13, 13, 12, 12, 12,
@ -28,7 +30,11 @@ static const u8 max_precision_for_radix[37] = {
// clang-format on
};
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static constexpr AK::Array<char, 36> digits = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
static String decimal_digits_to_string(double number)
{

View file

@ -1,6 +1,7 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Mițca Dumitru <dumitru0mitca@gmail.com>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -63,11 +64,11 @@ union FloatExtractor;
// This assumes long double is 80 bits, which is true with GCC on Intel platforms
template<>
union FloatExtractor<long double> {
static const int mantissa_bits = 64;
static const unsigned long long mantissa_max = ~0u;
static const int exponent_bias = 16383;
static const int exponent_bits = 15;
static const unsigned exponent_max = 32767;
static constexpr int mantissa_bits = 64;
static constexpr unsigned long long mantissa_max = ~0u;
static constexpr int exponent_bias = 16383;
static constexpr int exponent_bits = 15;
static constexpr unsigned exponent_max = 32767;
struct {
unsigned long long mantissa;
unsigned exponent : 15;
@ -79,11 +80,11 @@ union FloatExtractor<long double> {
template<>
union FloatExtractor<double> {
static const int mantissa_bits = 52;
static const unsigned long long mantissa_max = (1ull << 52) - 1;
static const int exponent_bias = 1023;
static const int exponent_bits = 11;
static const unsigned exponent_max = 2047;
static constexpr int mantissa_bits = 52;
static constexpr unsigned long long mantissa_max = (1ull << 52) - 1;
static constexpr int exponent_bias = 1023;
static constexpr int exponent_bits = 11;
static constexpr unsigned exponent_max = 2047;
struct {
unsigned long long mantissa : 52;
unsigned exponent : 11;
@ -94,11 +95,11 @@ union FloatExtractor<double> {
template<>
union FloatExtractor<float> {
static const int mantissa_bits = 23;
static const unsigned mantissa_max = (1 << 23) - 1;
static const int exponent_bias = 127;
static const int exponent_bits = 8;
static const unsigned exponent_max = 255;
static constexpr int mantissa_bits = 23;
static constexpr unsigned mantissa_max = (1 << 23) - 1;
static constexpr int exponent_bias = 127;
static constexpr int exponent_bits = 8;
static constexpr unsigned exponent_max = 255;
struct {
unsigned long long mantissa : 23;
unsigned exponent : 8;

View file

@ -1,12 +1,13 @@
/*
* Copyright (c) 2021, Gunnar Beutner <gunnar@beutner.name>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibC/bits/pthread_forward.h>
static const PthreadFunctions s_functions = {
static constexpr PthreadFunctions s_functions = {
.pthread_mutex_trylock = pthread_mutex_trylock,
.pthread_mutex_destroy = pthread_mutex_destroy,

View file

@ -1,16 +1,18 @@
/*
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/StringView.h>
#include <LibRegex/Regex.h>
#include <LibSQL/AST/AST.h>
#include <LibSQL/Database.h>
namespace SQL::AST {
static const String s_posix_basic_metacharacters = ".^$*[]+\\";
static constexpr StringView s_posix_basic_metacharacters = ".^$*[]+\\";
ResultOr<Value> NumericLiteral::evaluate(ExecutionContext&) const
{

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2020-2022, the SerenityOS developers.
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
@ -15,7 +15,7 @@
// U+FFFD REPLACEMENT CHARACTER (<28>)
#define REPLACEMENT_CHARACTER 0xFFFD
static const u32 TOKENIZER_EOF = 0xFFFFFFFF;
static constexpr u32 TOKENIZER_EOF = 0xFFFFFFFF;
static inline void log_parse_error(const SourceLocation& location = SourceLocation::current())
{

View file

@ -29,9 +29,9 @@ protected:
template<typename T>
struct TypeTrivia {
static const size_t bits = sizeof(T) * 8;
static const T sign_bit = 1 << (bits - 1);
static const T mask = MakeUnsigned<T>(-1);
static constexpr size_t bits = sizeof(T) * 8;
static constexpr T sign_bit = 1 << (bits - 1);
static constexpr T mask = MakeUnsigned<T>(-1);
};
template<typename T, typename U>
@ -189,7 +189,7 @@ enum InstructionFormat {
OP_NEAR_imm,
};
static const unsigned CurrentAddressSize = 0xB33FBABE;
static constexpr unsigned CurrentAddressSize = 0xB33FBABE;
struct InstructionDescriptor {
InstructionHandler handler { nullptr };