srctree

Robin Linden parent be1c0667 8cc87c21
all: Normalize const and constexpr variable names

Now all constexpr variables are kCamelCase and all runtime constvariables are snake_case.

inlinesplit
archive/zlib_test.cpp added: 69, removed: 70, total 0
@@ -14,32 +14,32 @@ using namespace std::literals;
 
namespace {
 
constexpr auto expected = "p { font-size: 123em; }\n"sv;
constexpr auto kExpected = "p { font-size: 123em; }\n"sv;
 
// p { font-size: 123em; }, gzipped.
constexpr auto gzipped_css =
constexpr auto kGzippedCss =
"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x2b\x50\xa8\x56\x48\xcb\xcf\x2b\xd1\x2d\xce\xac\x4a\xb5\x52\x30\x34\x32\x4e\xcd\xb5\x56\xa8\xe5\x02\x00\x0c\x97\x72\x35\x18\x00\x00\x00"sv;
 
// p { font-size: 123em; }, zlibbed.
constexpr auto zlibbed_css =
constexpr auto kZlibbedCss =
"\x78\x5e\x2b\x50\xa8\x56\x48\xcb\xcf\x2b\xd1\x2d\xce\xac\x4a\xb5\x52\x30\x34\x32\x4e\xcd\xb5\x56\xa8\xe5\x02\x00\x63\xc3\x07\x6f"sv;
 
} // namespace
 
int main() {
etest::test("zlib", [] {
expect_eq(zlib_decode(zlibbed_css, ZlibMode::Zlib), expected);
expect(!zlib_decode(gzipped_css, ZlibMode::Zlib).has_value());
expect_eq(zlib_decode(kZlibbedCss, ZlibMode::Zlib), kExpected);
expect(!zlib_decode(kGzippedCss, ZlibMode::Zlib).has_value());
});
 
etest::test("gzip", [] {
expect(!zlib_decode(zlibbed_css, ZlibMode::Gzip), std::nullopt);
expect_eq(zlib_decode(gzipped_css, ZlibMode::Gzip), expected);
expect(!zlib_decode(kZlibbedCss, ZlibMode::Gzip), std::nullopt);
expect_eq(zlib_decode(kGzippedCss, ZlibMode::Gzip), kExpected);
});
 
etest::test("zlib and gzip", [] {
expect_eq(zlib_decode(zlibbed_css, ZlibMode::GzipAndZlib), expected);
expect_eq(zlib_decode(gzipped_css, ZlibMode::GzipAndZlib), expected);
expect_eq(zlib_decode(kZlibbedCss, ZlibMode::GzipAndZlib), kExpected);
expect_eq(zlib_decode(kGzippedCss, ZlibMode::GzipAndZlib), kExpected);
});
 
return etest::run_all_tests();
 
css/parser.cpp added: 69, removed: 70, total 0
@@ -28,26 +28,25 @@
namespace css {
namespace {
 
constexpr std::array border_shorthand_properties{
"border", "border-left", "border-right", "border-top", "border-bottom"};
constexpr std::array kBorderShorthandProperties{"border", "border-left", "border-right", "border-top", "border-bottom"};
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/border-style
constexpr std::array border_style_keywords{
constexpr std::array kBorderStyleKeywords{
"none", "hidden", "dotted", "dashed", "solid", "double", "groove", "ridge", "inset", "outset"};
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/border-width
constexpr std::array border_width_keywords{"thin", "medium", "thick"};
constexpr std::array kBorderWidthKeywords{"thin", "medium", "thick"};
 
constexpr auto shorthand_edge_property = std::array{"padding", "margin", "border-style"};
constexpr auto kShorthandEdgeProperties = std::array{"padding", "margin", "border-style"};
 
constexpr auto absolute_size_keywords =
constexpr auto kAbsoluteSizeKeywords =
std::array{"xx-small", "x-small", "small", "medium", "large", "x-large", "xx-large", "xxx-large"};
 
constexpr auto relative_size_keywords = std::array{"larger", "smaller"};
constexpr auto kRelativeSizeKeywords = std::array{"larger", "smaller"};
 
constexpr auto weight_keywords = std::array{"bold", "bolder", "lighter"};
constexpr auto kWeightKeywords = std::array{"bold", "bolder", "lighter"};
 
constexpr auto stretch_keywords = std::array{"ultra-condensed",
constexpr auto kStretchKeywords = std::array{"ultra-condensed",
"extra-condensed",
"condensed",
"semi-condensed",
@@ -56,7 +55,7 @@ constexpr auto stretch_keywords = std::array{"ultra-condensed",
"extra-expanded",
"ultra-expanded"};
 
constexpr std::string_view dot_and_digits = ".0123456789";
constexpr std::string_view kDotAndDigits = ".0123456789";
 
template<auto const &array>
constexpr bool is_in_array(std::string_view str) {
@@ -64,28 +63,28 @@ constexpr bool is_in_array(std::string_view str) {
}
 
constexpr bool is_shorthand_edge_property(std::string_view str) {
return is_in_array<shorthand_edge_property>(str);
return is_in_array<kShorthandEdgeProperties>(str);
}
 
constexpr bool is_absolute_size(std::string_view str) {
return is_in_array<absolute_size_keywords>(str);
return is_in_array<kAbsoluteSizeKeywords>(str);
}
 
constexpr bool is_relative_size(std::string_view str) {
return is_in_array<relative_size_keywords>(str);
return is_in_array<kRelativeSizeKeywords>(str);
}
 
constexpr bool is_weight(std::string_view str) {
return is_in_array<weight_keywords>(str);
return is_in_array<kWeightKeywords>(str);
}
 
constexpr bool is_stretch(std::string_view str) {
return is_in_array<stretch_keywords>(str);
return is_in_array<kStretchKeywords>(str);
}
 
constexpr bool is_length_or_percentage(std::string_view str) {
// TODO(mkiael): Make this check more reliable.
std::size_t pos = str.find_first_not_of(dot_and_digits);
std::size_t pos = str.find_first_not_of(kDotAndDigits);
return pos > 0 && pos != std::string_view::npos;
}
 
@@ -351,7 +350,7 @@ void Parser::add_declaration(
expand_border_radius_values(declarations, value);
} else if (name == "text-decoration") {
expand_text_decoration_values(declarations, value);
} else if (is_in_array<border_shorthand_properties>(name)) {
} else if (is_in_array<kBorderShorthandProperties>(name)) {
expand_border(name, declarations, value);
} else {
declarations.insert_or_assign(property_id_from_string(name), std::string{value});
@@ -406,11 +405,11 @@ void Parser::expand_border_impl(
 
enum class BorderPropertyType { Color, Style, Width };
auto guess_type = [](std::string_view v) -> BorderPropertyType {
if (is_in_array<border_style_keywords>(v)) {
if (is_in_array<kBorderStyleKeywords>(v)) {
return BorderPropertyType::Style;
}
 
if (v.find_first_of(dot_and_digits) == 0 || is_in_array<border_width_keywords>(v)) {
if (v.find_first_of(kDotAndDigits) == 0 || is_in_array<kBorderWidthKeywords>(v)) {
return BorderPropertyType::Width;
}
 
 
css/property_id.cpp added: 69, removed: 70, total 0
@@ -13,7 +13,7 @@ using namespace std::literals;
namespace css {
namespace {
 
std::map<std::string_view, PropertyId> const kKnownProperties{
std::map<std::string_view, PropertyId> const known_properties{
{"azimuth"sv, PropertyId::Azimuth},
{"background-attachment"sv, PropertyId::BackgroundAttachment},
{"background-clip"sv, PropertyId::BackgroundClip},
@@ -114,16 +114,16 @@ std::map<std::string_view, PropertyId> const kKnownProperties{
} // namespace
 
PropertyId property_id_from_string(std::string_view id) {
if (!kKnownProperties.contains(id)) {
if (!known_properties.contains(id)) {
return PropertyId::Unknown;
}
 
return kKnownProperties.at(id);
return known_properties.at(id);
}
 
std::string_view to_string(PropertyId id) {
auto it = std::ranges::find_if(kKnownProperties, [id](auto const &entry) { return entry.second == id; });
if (it != end(kKnownProperties)) {
auto it = std::ranges::find_if(known_properties, [id](auto const &entry) { return entry.second == id; });
if (it != end(known_properties)) {
return it->first;
}
 
 
gfx/color.cpp added: 69, removed: 70, total 0
@@ -22,7 +22,7 @@ struct CaseInsensitiveLess {
};
 
// https://developer.mozilla.org/en-US/docs/Web/CSS/named-color#list_of_all_color_keywords
std::map<std::string_view, gfx::Color, CaseInsensitiveLess> const kNamedColors{
std::map<std::string_view, gfx::Color, CaseInsensitiveLess> const named_colors{
// System colors.
// https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#system_colors
// TODO(robinlinden): Move these elsewhere and actually grab them from the system.
@@ -192,11 +192,11 @@ std::map<std::string_view, gfx::Color, CaseInsensitiveLess> const kNamedColors{
} // namespace
 
std::optional<Color> Color::from_css_name(std::string_view name) {
if (!kNamedColors.contains(name)) {
if (!named_colors.contains(name)) {
return std::nullopt;
}
 
return kNamedColors.at(name);
return named_colors.at(name);
}
 
} // namespace gfx
 
html2/character_reference.cpp added: 69, removed: 70, total 0
@@ -29,7 +29,7 @@ namespace {
// print(f'"{key}"sv', *codepoints, sep=', ', end='')
// print('},')
// ```
static constexpr std::array references = std::to_array<CharacterReference>({{"&AElig"sv, 198},
static constexpr std::array kReferences = std::to_array<CharacterReference>({{"&AElig"sv, 198},
{"&AElig;"sv, 198},
{"&AMP"sv, 38},
{"&AMP;"sv, 38},
@@ -2266,7 +2266,7 @@ static constexpr std::array references = std::to_array<CharacterReference>({{"&A
std::optional<CharacterReference> find_named_character_reference_for(std::string_view buffer) {
std::optional<CharacterReference> maybe_reference{std::nullopt};
 
for (auto const &reference : references) {
for (auto const &reference : kReferences) {
if (buffer.starts_with(reference.name)
&& (!maybe_reference || reference.name.size() > maybe_reference->name.size())) {
maybe_reference = reference;
 
layout/layout.cpp added: 69, removed: 70, total 0
@@ -248,7 +248,7 @@ void calculate_padding(LayoutBox &box, int const font_size, int const root_font_
}
 
// https://drafts.csswg.org/css-backgrounds/#the-border-width
std::map<std::string_view, int> const kBorderWidthKeywords{
std::map<std::string_view, int> const border_width_keywords{
{"thin", 3},
{"medium", 5},
{"thick", 7},
@@ -256,8 +256,8 @@ std::map<std::string_view, int> const kBorderWidthKeywords{
 
void calculate_border(LayoutBox &box, int const font_size, int const root_font_size) {
auto as_px = [&](std::string_view border_width_property) {
if (kBorderWidthKeywords.contains(border_width_property)) {
return kBorderWidthKeywords.at(border_width_property);
if (border_width_keywords.contains(border_width_property)) {
return border_width_keywords.at(border_width_property);
}
 
return to_px(border_width_property, font_size, root_font_size);
 
style/styled_node.cpp added: 69, removed: 70, total 0
@@ -28,7 +28,7 @@ namespace style {
namespace {
 
// https://www.w3.org/TR/css-cascade/#initial-values
std::map<css::PropertyId, std::string_view> const kInitialValues{
std::map<css::PropertyId, std::string_view> const initial_values{
// https://developer.mozilla.org/en-US/docs/Web/CSS/background-color#formal_definition
{css::PropertyId::BackgroundColor, "transparent"sv},
 
@@ -234,7 +234,7 @@ std::string_view get_parent_raw_property(style::StyledNode const &node, css::Pro
return node.parent->get_raw_property(property);
}
 
return kInitialValues.at(property);
return initial_values.at(property);
}
 
std::optional<std::pair<float, std::string_view>> split_into_value_and_unit(std::string_view property) {
@@ -271,10 +271,10 @@ std::string_view StyledNode::get_raw_property(css::PropertyId property) const {
return parent->get_raw_property(property);
}
 
return kInitialValues.at(property);
return initial_values.at(property);
} else if (it->second == "initial") {
// https://developer.mozilla.org/en-US/docs/Web/CSS/initial
return kInitialValues.at(property);
return initial_values.at(property);
} else if (it->second == "inherit") {
// https://developer.mozilla.org/en-US/docs/Web/CSS/inherit
return get_parent_raw_property(*this, property);
@@ -398,7 +398,7 @@ std::vector<TextDecorationLine> StyledNode::get_text_decoration_line_property()
static constexpr int kDefaultFontSize{10};
// https://drafts.csswg.org/css-fonts-4/#absolute-size-mapping
constexpr int kMediumFontSize = kDefaultFontSize;
std::map<std::string_view, float> const kFontSizeAbsoluteSizeKeywords{
std::map<std::string_view, float> const font_size_absolute_size_keywords{
{"xx-small", 3 / 5.f},
{"x-small", 3 / 4.f},
{"small", 8 / 9.f},
@@ -430,8 +430,8 @@ int StyledNode::get_font_size_property() const {
}
auto raw_value = closest->first;
 
if (kFontSizeAbsoluteSizeKeywords.contains(raw_value)) {
return std::lround(kFontSizeAbsoluteSizeKeywords.at(raw_value) * kMediumFontSize);
if (font_size_absolute_size_keywords.contains(raw_value)) {
return std::lround(font_size_absolute_size_keywords.at(raw_value) * kMediumFontSize);
}
 
auto value_and_unit = split_into_value_and_unit(raw_value);
 
util/base_parser_test.cpp added: 69, removed: 70, total 0
@@ -12,24 +12,24 @@ using util::BaseParser;
 
int main() {
etest::test("peek", [] {
constexpr auto abcd = BaseParser("abcd");
expect(abcd.peek() == 'a');
expect(abcd.peek(2) == "ab");
expect(abcd.peek(3) == "abc");
expect(abcd.peek(4) == "abcd");
constexpr auto kAbcd = BaseParser("abcd");
expect(kAbcd.peek() == 'a');
expect(kAbcd.peek(2) == "ab");
expect(kAbcd.peek(3) == "abc");
expect(kAbcd.peek(4) == "abcd");
expect(BaseParser(" ").peek() == ' ');
});
 
etest::test("starts_with", [] {
constexpr auto abcd = BaseParser("abcd");
expect(!abcd.starts_with("hello"));
expect(abcd.starts_with("ab"));
expect(abcd.starts_with("abcd"));
constexpr auto kAbcd = BaseParser("abcd");
expect(!kAbcd.starts_with("hello"));
expect(kAbcd.starts_with("ab"));
expect(kAbcd.starts_with("abcd"));
});
 
etest::test("is_eof, advance", [] {
constexpr auto abcd = BaseParser("abcd");
expect(!abcd.is_eof());
constexpr auto kAbcd = BaseParser("abcd");
expect(!kAbcd.is_eof());
expect(BaseParser("").is_eof());
 
auto p = BaseParser("abcd");
 
util/string.h added: 69, removed: 70, total 0
@@ -109,8 +109,8 @@ constexpr std::pair<std::string_view, std::string_view> split_once(std::string_v
}
 
constexpr bool is_whitespace(char ch) {
constexpr std::array ws_chars = {' ', '\n', '\r', '\f', '\v', '\t'};
return std::ranges::any_of(ws_chars, [ch](char ws_ch) { return ch == ws_ch; });
constexpr std::array kWsChars = {' ', '\n', '\r', '\f', '\v', '\t'};
return std::ranges::any_of(kWsChars, [ch](char ws_ch) { return ch == ws_ch; });
}
 
constexpr std::string_view trim_start(std::string_view s) {