Skip to content

Commit

Permalink
migrate to c++20
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 2, 2024
1 parent 59cc693 commit 7abc164
Show file tree
Hide file tree
Showing 15 changed files with 26 additions and 42 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ project(ada
LANGUAGES C CXX
VERSION 2.9.1
)
set(CMAKE_CXX_STANDARD 17)
set(ADA_LIB_VERSION "2.9.1" CACHE STRING "ada library version")
set(ADA_LIB_SOVERSION "2" CACHE STRING "ada library soversion")

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ standard libraries, follow the RFC 3986. The following table illustrates possibl
### Requirements

The project is otherwise self-contained and it has no dependency.
A recent C++ compiler supporting C++17. We test GCC 9 or better, LLVM 10 or better and Microsoft Visual Studio 2022.
A recent C++ compiler supporting C++20. We test GCC 9 or better, LLVM 10 or better and Microsoft Visual Studio 2022.

## Ada is fast.

Expand Down
2 changes: 1 addition & 1 deletion cmake/ada-flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ endif()
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

Expand Down
10 changes: 5 additions & 5 deletions fuzz/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,39 @@ mkdir build
AMALGAMATE_OUTPUT_PATH=./build/singleheader python3 singleheader/amalgamate.py

$CXX $CFLAGS $CXXFLAGS \
-std=c++17 \
-std=c++20 \
-I build/singleheader \
-c fuzz/parse.cc -o parse.o

$CXX $CFLAGS $CXXFLAGS $LIB_FUZZING_ENGINE parse.o \
-o $OUT/parse

$CXX $CFLAGS $CXXFLAGS \
-std=c++17 \
-std=c++20 \
-I build/singleheader \
-c fuzz/can_parse.cc -o can_parse.o

$CXX $CFLAGS $CXXFLAGS $LIB_FUZZING_ENGINE can_parse.o \
-o $OUT/can_parse

$CXX $CFLAGS $CXXFLAGS \
-std=c++17 \
-std=c++20 \
-I build/singleheader \
-c fuzz/idna.cc -o idna.o

$CXX $CFLAGS $CXXFLAGS $LIB_FUZZING_ENGINE idna.o \
-o $OUT/idna

$CXX $CFLAGS $CXXFLAGS \
-std=c++17 \
-std=c++20 \
-I build/singleheader \
-c fuzz/url_search_params.cc -o url_search_params.o

$CXX $CFLAGS $CXXFLAGS $LIB_FUZZING_ENGINE url_search_params.o \
-o $OUT/url_search_params

$CXX $CFLAGS $CXXFLAGS \
-std=c++17 \
-std=c++20 \
-I build/singleheader \
-c build/singleheader/ada.cpp -o ada.o

Expand Down
8 changes: 0 additions & 8 deletions include/ada/checkers-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ inline constexpr bool is_normalized_windows_drive_letter(
return input.size() >= 2 && (is_alpha(input[0]) && (input[1] == ':'));
}

ada_really_inline bool begins_with(std::string_view view,
std::string_view prefix) {
// in C++20, you have view.begins_with(prefix)
// std::equal is constexpr in C++20
return view.size() >= prefix.size() &&
std::equal(prefix.begin(), prefix.end(), view.begin());
}

} // namespace ada::checkers

#endif // ADA_CHECKERS_INL_H
7 changes: 0 additions & 7 deletions include/ada/checkers.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ inline constexpr bool is_windows_drive_letter(std::string_view input) noexcept;
inline constexpr bool is_normalized_windows_drive_letter(
std::string_view input) noexcept;

/**
* @private
* @warning Will be removed when Ada requires C++20.
*/
ada_really_inline bool begins_with(std::string_view view,
std::string_view prefix);

/**
* @private
* Returns true if an input is an ipv4 address. It is assumed that the string
Expand Down
4 changes: 2 additions & 2 deletions include/ada/url-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ inline std::ostream &operator<<(std::ostream &out, const ada::url &u) {
out.host_start = out.protocol_end;
out.host_end = out.host_start;

if (!has_opaque_path && checkers::begins_with(path, "//")) {
if (!has_opaque_path && path.starts_with("//")) {
// If url's host is null, url does not have an opaque path, url's path's
// size is greater than 1, and url's path[0] is the empty string, then
// append U+002F (/) followed by U+002E (.) to output.
Expand Down Expand Up @@ -196,7 +196,7 @@ inline void url::copy_scheme(const ada::url &u) {
if (port.has_value()) {
output += ":" + get_port();
}
} else if (!has_opaque_path && checkers::begins_with(path, "//")) {
} else if (!has_opaque_path && path.starts_with("//")) {
// If url's host is null, url does not have an opaque path, url's path's
// size is greater than 1, and url's path[0] is the empty string, then
// append U+002F (/) followed by U+002E (.) to output.
Expand Down
4 changes: 2 additions & 2 deletions include/ada/url_aggregator-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inline void url_aggregator::update_base_authority(
base.protocol_end, base.host_start - base.protocol_end);
ada_log("url_aggregator::update_base_authority ", input);

bool input_starts_with_dash = checkers::begins_with(input, "//");
bool input_starts_with_dash = input.starts_with("//");
uint32_t diff = components.host_start - components.protocol_end;

buffer.erase(components.protocol_end,
Expand Down Expand Up @@ -267,7 +267,7 @@ inline void url_aggregator::update_base_pathname(const std::string_view input) {
ADA_ASSERT_TRUE(!helpers::overlaps(input, buffer));
ADA_ASSERT_TRUE(validate());

const bool begins_with_dashdash = checkers::begins_with(input, "//");
const bool begins_with_dashdash = input.starts_with("//");
if (!begins_with_dashdash && has_dash_dot()) {
ada_log("url_aggregator::update_base_pathname has /.: \n", to_diagram());
// We must delete the ./
Expand Down
4 changes: 2 additions & 2 deletions src/checkers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
return false;
}
// It must start with 0x.
if (!std::equal(view.begin(), view.begin() + 2, "0x")) {
if (!view.starts_with("0x")) {
return false;
}
// We must allow "0x".
Expand All @@ -59,7 +59,7 @@ ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
// for use with path_signature, we include all characters that need percent
// encoding.
static constexpr std::array<uint8_t, 256> path_signature_table =
[]() constexpr {
[]() consteval {
std::array<uint8_t, 256> result{};
for (size_t i = 0; i < 256; i++) {
if (i <= 0x20 || i == 0x22 || i == 0x23 || i == 0x3c || i == 0x3e ||
Expand Down
4 changes: 2 additions & 2 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ result_type parse_url_impl(std::string_view user_input,
// then set state to special authority ignore slashes state and increase
// pointer by 1.
std::string_view view = helpers::substring(url_data, input_position);
if (ada::checkers::begins_with(view, "//")) {
if (view.starts_with("//")) {
state = ada::state::SPECIAL_AUTHORITY_IGNORE_SLASHES;
input_position += 2;
} else {
Expand Down Expand Up @@ -520,7 +520,7 @@ result_type parse_url_impl(std::string_view user_input,
// then set state to special authority ignore slashes state and increase
// pointer by 1.
std::string_view view = helpers::substring(url_data, input_position);
if (ada::checkers::begins_with(view, "//")) {
if (view.starts_with("//")) {
input_position += 2;
}

Expand Down
6 changes: 3 additions & 3 deletions src/unicode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ ada_really_inline bool has_tabs_or_newline(
// U+003F (?), U+0040 (@), U+005B ([), U+005C (\), U+005D (]), U+005E (^), or
// U+007C (|).
constexpr static std::array<uint8_t, 256> is_forbidden_host_code_point_table =
[]() constexpr {
[]() consteval {
std::array<uint8_t, 256> result{};
for (uint8_t c : {'\0', '\x09', '\x0a', '\x0d', ' ', '#', '/', ':', '<',
'>', '?', '@', '[', '\\', ']', '^', '|'}) {
Expand All @@ -171,7 +171,7 @@ ada_really_inline constexpr bool is_forbidden_host_code_point(
}

constexpr static std::array<uint8_t, 256> is_forbidden_domain_code_point_table =
[]() constexpr {
[]() consteval {
std::array<uint8_t, 256> result{};
for (uint8_t c : {'\0', '\x09', '\x0a', '\x0d', ' ', '#', '/', ':', '<',
'>', '?', '@', '[', '\\', ']', '^', '|', '%'}) {
Expand Down Expand Up @@ -251,7 +251,7 @@ contains_forbidden_domain_code_point_or_upper(const char* input,
}

// std::isalnum(c) || c == '+' || c == '-' || c == '.') is true for
constexpr static std::array<bool, 256> is_alnum_plus_table = []() constexpr {
constexpr static std::array<bool, 256> is_alnum_plus_table = []() consteval {
std::array<bool, 256> result{};
for (size_t c = 0; c < 256; c++) {
result[c] = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') ||
Expand Down
3 changes: 1 addition & 2 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ bool url_aggregator::set_pathname(const std::string_view input) {
}
clear_pathname();
parse_path(input);
if (checkers::begins_with(get_pathname(), "//") && !has_authority() &&
!has_dash_dot()) {
if (get_pathname().starts_with("//") && !has_authority() && !has_dash_dot()) {
buffer.insert(components.pathname_start, "/.");
components.pathname_start += 2;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/installation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)

project(test_ada_install VERSION 0.1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ada REQUIRED)
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if(MSVC AND BUILD_SHARED_LIBS)
"$<TARGET_FILE:ada>" # <--this is in-file
"$<TARGET_FILE_DIR:adaparse>") # <--this is out-file path
endif()
CPMAddPackage("gh:fmtlib/fmt#10.2.1")
CPMAddPackage("gh:fmtlib/fmt#11.0.2")
CPMAddPackage(
GITHUB_REPOSITORY jarro2783/cxxopts
VERSION 3.2.0
Expand Down
9 changes: 5 additions & 4 deletions tools/cli/adaparse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ int main(int argc, char** argv) {

auto adaparse_print = [has_result, &out](const std::string& format_str,
auto&&... args) {
std::string formatted_str =
fmt::format(format_str, std::forward<decltype(args)>(args)...);
std::string formatted_str = fmt::format(
fmt::runtime(format_str), std::forward<decltype(args)>(args)...);
if (has_result) {
out.print(formatted_str);
out.print(fmt::runtime(formatted_str));
} else {
fmt::print("{}", formatted_str);
}
Expand Down Expand Up @@ -275,7 +275,8 @@ int main(int argc, char** argv) {
std::string get_part = result["get"].as<std::string>();

auto print_lambda = [](const std::string& format_str, auto&&... args) {
fmt::print(format_str, std::forward<decltype(args)>(args)...);
fmt::print(fmt::runtime(format_str),
std::forward<decltype(args)>(args)...);
};

return print_part(print_lambda, get_part, url.value()) ? EXIT_SUCCESS
Expand Down

0 comments on commit 7abc164

Please sign in to comment.