### Install Example Development Exported Targets Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the exported targets for the Ada library, namespaced as 'ada::', to the CMake configuration directory for the 'example_development' component. ```cmake install( EXPORT ada_targets NAMESPACE ada:: DESTINATION "${ADA_INSTALL_CMAKEDIR}" COMPONENT example_development ) ``` -------------------------------- ### Initial Project Setup Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Clone the Ada repository, navigate to the directory, and set up the build environment with testing enabled. ```bash cd /path/to/ada cmake -B build -DADA_TESTING=ON cmake --build build ``` -------------------------------- ### Build and Run Ada URL Parser Example Source: https://github.com/ada-url/ada/blob/main/tests/installation/CMakeLists.txt This CMakeLists.txt file configures a C++ project to use the Ada URL parser library. It sets up the C++ standard, finds the Ada package, writes a main.cpp file with example usage, compiles an executable, and links it against the Ada library. ```cmake cmake_minimum_required(VERSION 3.15) project(test_ada_install VERSION 0.1.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(ada REQUIRED) # You can provide your own code, this is just an example: file(WRITE main.cpp " #include \"ada.h\" #include int main(int , char *[]) { ada::result url = ada::parse(\"https://www.google.com\"); url->set_protocol(\"http\"); std::cout << url->get_protocol() << std::endl; std::cout << url->get_host() << std::endl; return EXIT_SUCCESS; }") add_executable(main main.cpp) target_link_libraries(main PUBLIC ada::ada) ``` -------------------------------- ### Windows Build Configuration Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Specify build configuration for Windows, including testing and Release mode. This example shows how to build and run tests in Release configuration. ```bash cmake -B build -DADA_TESTING=ON cmake --build build --config Release ctest --output-on-failure --test-dir build --config Release ``` -------------------------------- ### Install Targets Source: https://github.com/ada-url/ada/blob/main/tools/cli/CMakeLists.txt Installs the 'adaparse' target to the appropriate library, archive, and runtime directories based on installation prefixes. ```cmake install( TARGETS adaparse ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) ``` -------------------------------- ### Install Ada CMake Configuration Files Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the generated 'ada-config.cmake' and 'ada-config-version.cmake' files to the specified CMake installation directory, for the 'ada_development' component. ```cmake install( FILES "${PROJECT_BINARY_DIR}/ada-config.cmake" "${PROJECT_BINARY_DIR}/ada-config-version.cmake" DESTINATION "${ADA_INSTALL_CMAKEDIR}" COMPONENT ada_development ) ``` -------------------------------- ### Clean Rebuild Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Remove the existing build directory and perform a fresh setup and build of the project with testing enabled. ```bash # Remove build directory and start fresh rm -rf build cmake -B build -DADA_TESTING=ON cmake --build build ``` -------------------------------- ### Install Ada Header Files Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the main Ada header files to the CMAKE_INSTALL_INCLUDEDIR, marked for the 'ada_development' component. ```cmake install( FILES include/ada.h include/ada_c.h DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT ada_development ) ``` -------------------------------- ### Configure pkg-config Files Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Sets up pkg-config variables for include and library directories using CMake's join_paths function. It then configures the 'ada.pc' file and installs it to the pkgconfig directory. ```cmake include(cmake/JoinPaths.cmake) join_paths(PKGCONFIG_INCLUDEDIR "${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") join_paths(PKGCONFIG_LIBDIR "${prefix}" "${CMAKE_INSTALL_LIBDIR}") configure_file("ada.pc.in" "ada.pc" @ONLY) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/ada.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" ) ``` -------------------------------- ### Troubleshooting Missing Benchmark Executable Source: https://github.com/ada-url/ada/blob/main/AGENTS.md If the benchmark executable cannot be found, ensure benchmarks are built by enabling `ADA_BENCHMARKS`. This example shows how to build and then check the build directory. ```bash cmake -B build -DADA_BENCHMARKS=ON cmake --build build ls build/benchmarks/ ``` -------------------------------- ### Ada URL Parsing Demo Source: https://github.com/ada-url/ada/blob/main/README.md A C++ example demonstrating how to parse a URL, modify its protocol, and retrieve protocol and host information using the Ada library. ```cpp #include "ada.cpp" #include "ada.h" #include int main(int, char *[]) { auto url = ada::parse("https://www.google.com"); if (!url) { std::cout << "failure" << std::endl; return EXIT_FAILURE; } url->set_protocol("http"); std::cout << url->get_protocol() << std::endl; std::cout << url->get_host() << std::endl; return EXIT_SUCCESS; } ``` -------------------------------- ### Install Ada Header Directory Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the 'include/ada' directory to the CMAKE_INSTALL_INCLUDEDIR, marked for the 'ada_development' component. ```cmake install( DIRECTORY include/ada DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" COMPONENT ada_development ) ``` -------------------------------- ### Install Ada Library Target Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the Ada library target, defining components for runtime and development, and setting include paths. ```cmake install( TARGETS ada EXPORT ada_targets RUNTIME COMPONENT ada_runtime LIBRARY COMPONENT ada_runtime NAMELINK_COMPONENT ada_development ARCHIVE COMPONENT ada_development INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) ``` -------------------------------- ### Include CMake Utility Modules Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Includes standard CMake modules for package configuration and installation directory definitions. ```cmake include(CMakePackageConfigHelpers) include(GNUInstallDirs) ``` -------------------------------- ### Install Ada Exported Targets Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Installs the exported targets for the Ada library, namespaced as 'ada::', to the CMake configuration directory for the 'ada_development' component. ```cmake install( EXPORT ada_targets NAMESPACE ada:: DESTINATION "${ADA_INSTALL_CMAKEDIR}" COMPONENT ada_development ) ``` -------------------------------- ### Generate C Header with cbindgen Source: https://github.com/ada-url/ada/blob/main/benchmarks/competitors/servo-url/README.md Use cbindgen to generate C/C++ header files from Rust code. Ensure cbindgen is installed via Homebrew. ```bash brew install cbindgen cbindgen --config cbindgen.toml --crate servo-url --output servo_url.h ``` -------------------------------- ### Handle Rust Unavailability Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt This block executes if Rust is not found. It informs the user that benchmarking will not occur and provides platform-specific instructions for installing Rust. ```cmake else() message(STATUS "Rust/Cargo is unavailable." ) message(STATUS "We will not benchmark servo-url." ) if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") message(STATUS "Under macOS, you may be able to install rust with") message(STATUS "curl https://sh.rustup.rs -sSf | sh") elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") message(STATUS "Under Linux, you may be able to install rust with a command such as") message(STATUS "apt-get install cargo" ) message(STATUS "or" ) message(STATUS "curl https://sh.rustup.rs -sSf | sh") endif() endif() ``` -------------------------------- ### Build Ada with Local Packages Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Configure the Ada build with CMake to use locally installed packages for dependencies like GoogleTest, when `ADA_TESTING` is enabled. ```bash cmake -B build -DADA_TESTING=ON -DCPM_USE_LOCAL_PACKAGES=ON cmake --build build ``` -------------------------------- ### Incorrect Benchmark Build Configuration Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Example of an incorrect build configuration for benchmarks, omitting `CMAKE_BUILD_TYPE=Release`, which leads to development checks being enabled and skewed results. ```bash # WRONG: Don't benchmark with development checks enabled cmake -B build -DADA_BENCHMARKS=ON # Missing Release mode! ``` -------------------------------- ### Build Ada URL Parser Without Tests Source: https://github.com/ada-url/ada/blob/main/README.md Use this command to build the Ada URL parser locally without including tests. Ensure you have cmake installed. ```bash cmake -B build && cmake --build build ``` -------------------------------- ### Compile C Program with Ada Library Source: https://github.com/ada-url/ada/blob/main/README.md Example compilation command for linking a C program with the Ada library, using a C++ compiler for C++ source files. This is typically used on Linux/macOS systems. ```Shell c++ -c ada.cpp -std=c++20 cc -c demo.c c++ demo.o ada.o -o cdemo ./cdemo ``` -------------------------------- ### Build LoongArch64 Code with QEMU Source: https://github.com/ada-url/ada/blob/main/cmake/toolchains-dev/README.md Use these commands to download QEMU, set up the environment, and build LoongArch64 code. Ensure GCC and Binutils versions meet the requirements. ```bash $ sudo curl -L https://github.com/loongson/build-tools/releases/download/2025.06.06/qemu-loongarch64 --output /opt/qemu-loongarch64 $ sudo chmod +x /opt/qemu-loongarch64 $ export PATH=/opt:$PATH $ export QEMU_LD_PREFIX="/usr/loongarch64-linux-gnu" # ubuntu 24.04 $ export QEMU_CPU="la464" $ mkdir build && cd build $ cmake -DCMAKE_TOOLCHAIN_FILE=cmake/toolchains-dev/loongarch64.cmake -DADA_TESTING=ON ../ $ make ``` -------------------------------- ### Compile Demo with C++ Source: https://github.com/ada-url/ada/blob/main/singleheader/README.md Compile the demo C++ file, linking the amalgamated ada.cpp. ```bash c++ -std=c++20 -c demo.cpp ``` ```bash c++ -std=c++20 -o demo demo.cpp ``` ```bash ./demo ``` -------------------------------- ### Compile C Demo Source: https://github.com/ada-url/ada/blob/main/singleheader/README.md Compile the C demo file and link it with the amalgamated ada.cpp. ```bash c++ -c ada.cpp -std=c++20 ``` ```bash cc -c demo.c ``` ```bash c++ demo.o ada.o -o cdemo ``` ```bash ./cdemo ``` -------------------------------- ### Performance Validation with Benchmarks Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Set up a separate build configuration for benchmarks in Release mode and execute them to validate performance. ```bash # Create separate benchmark build cmake -B build-release -DADA_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release cmake --build build-release # Run benchmarks ./build-release/benchmarks/benchdata # Compare before/after optimizations # (stash changes, rebuild, run benchmark, restore, rebuild, run again) ``` -------------------------------- ### Get URL Href Size Without Allocation Source: https://github.com/ada-url/ada/blob/main/README.md Demonstrates how to get the byte length of a URL's serialized form (href) without allocating a string using `get_href_size()`. ```c++ auto url = ada::parse("https://example.com/path"); assert(url); size_t len = url->get_href_size(); // no allocation assert(len == url->get_href().size()); ``` -------------------------------- ### Demo Executable Configuration Source: https://github.com/ada-url/ada/blob/main/singleheader/CMakeLists.txt Configures the 'demo' executable for testing purposes. It links against the single-header include interface and conditionally against SIMDUTF. ```cmake if (ADA_TESTING) add_executable(demo $) target_link_libraries(demo ada-singleheader-include-source) if (ADA_USE_SIMDUTF) target_link_libraries(demo simdutf) endif() endif() ``` -------------------------------- ### Run LoongArch64 Tests with QEMU Source: https://github.com/ada-url/ada/blob/main/cmake/toolchains-dev/README.md Execute tests for LoongArch64 code using 'make test', 'ctest', or directly with QEMU. This assumes the build process has been completed. ```bash $ make test or $ ctest --output-on-failure --test-dir build or $ qemu-loongarch64 build/singleheader/cdemo ``` -------------------------------- ### Set ADA_COMPETITION Option Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Defines a CMake option 'ADA_COMPETITION' to control the installation of various competitors, defaulting to OFF. ```cmake option(ADA_COMPETITION "Whether to install various competitors." OFF) ``` -------------------------------- ### C Demo Executable Configuration Source: https://github.com/ada-url/ada/blob/main/singleheader/CMakeLists.txt Configures the 'cdemo' executable, which is built from the C source file. It links against the single-header static library. ```cmake add_executable(cdemo $) target_link_libraries(cdemo ada-singleheader-lib) if (CMAKE_CROSSCOMPILING_EMULATOR) add_test(cdemo ${CMAKE_CROSSCOMPILING_EMULATOR} cdemo) else() add_test(cdemo cdemo) endif() ``` -------------------------------- ### Define and Configure All Benchmarks Target Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Sets a list of benchmark targets and creates a custom target 'run_all_benchmarks' to execute them sequentially with a minimum benchmark time. ```cmake set(BENCHMARKS wpt_bench bench benchdata bbc_bench bench_ipv4 percent_encode bench_search_params urlpattern) add_custom_target(run_all_benchmarks COMMAND ${CMAKE_COMMAND} -E echo "Running all benchmarks..." ) foreach(benchmark IN LISTS BENCHMARKS) add_custom_command( TARGET run_all_benchmarks POST_BUILD COMMAND ${CMAKE_COMMAND} -E echo "Running ${benchmark}..." COMMAND $ --benchmark_min_time=1s WORKING_DIRECTORY ${CMAKE_BINARY_DIR} ) endforeach() ``` -------------------------------- ### Create wpt_bench Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Builds the 'wpt_bench' executable, linking it with 'ada', 'counters', and 'simdjson'. It also sets include directories. ```cmake add_executable(wpt_bench wpt_bench.cpp) target_link_libraries(wpt_bench PRIVATE ada counters::counters) target_link_libraries(wpt_bench PRIVATE simdjson) target_include_directories(wpt_bench PUBLIC "$") target_include_directories(wpt_bench PUBLIC "$") ``` -------------------------------- ### Find ICU Package Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Finds the ICU library with the 'uc' and 'i18n' components. If not found, it provides installation instructions for macOS and Linux. ```cmake find_package(ICU COMPONENTS uc i18n) ### If the user does not have ICU, let us help them with instructions: if(NOT ICU_FOUND) if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") if(EXISTS /opt/homebrew) message(STATUS "Under macOS, you may install ICU with brew, using 'brew install icu4c'.") else() message(STATUS "Under macOS, you should install brew (see https://brew.sh) and then icu4c ('brew install icu4c').") endif() elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") message(STATUS "Under Linux, you may be able to install ICU with a command such as 'apt-get install libicu-dev'." ) endif() endif(NOT ICU_FOUND) ``` -------------------------------- ### Basic URL Parsing with Adaparse Source: https://github.com/ada-url/ada/blob/main/docs/cli.md Demonstrates the basic usage of the adaparse command-line tool to parse a well-formatted URL. ```bash adaparse "http://www.google.com" ``` -------------------------------- ### Set Ada CMake Install Directory Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Defines and marks the ADA_INSTALL_CMAKEDIR variable as advanced, specifying the location for CMake package configuration files. ```cmake set( ADA_INSTALL_CMAKEDIR "${CMAKE_INSTALL_LIBDIR}/cmake/ada" CACHE STRING "CMake package config location relative to the install prefix" ) mark_as_advanced(ADA_INSTALL_CMAKEDIR) ``` -------------------------------- ### Create bbc_bench Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Builds the 'bbc_bench' executable, linking it with 'ada' and 'counters', and setting include directories. ```cmake add_executable(bbc_bench bbc_bench.cpp) target_link_libraries(bbc_bench PRIVATE ada counters::counters) target_include_directories(bbc_bench PUBLIC "$") target_include_directories(bbc_bench PUBLIC "$") ``` -------------------------------- ### Run Ada Demo Application Source: https://github.com/ada-url/ada/blob/main/README.md Execute the compiled Ada demo application. This will output the modified protocol and the host of the parsed URL. ```bash ./demo ``` -------------------------------- ### Create urlpattern Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Defines the 'urlpattern' executable and sets its include directories for build-time interface. ```cmake add_executable(urlpattern urlpattern.cpp) target_link_libraries(urlpattern PRIVATE ada counters::counters) target_include_directories(urlpattern PUBLIC "$") target_include_directories(urlpattern PUBLIC "$") ``` -------------------------------- ### URL Search Parameters Source: https://github.com/ada-url/ada/blob/main/README.md Shows how to work with URL search parameters, including initializing from a string, appending new parameters, and retrieving keys. ```APIDOC ### URL Search Params ```cpp ada::url_search_params search_params("a=b&c=d&e=f"); search_params.append("g=h"); search_params.get("g"); // will return "h" auto keys = search_params.get_keys(); while (keys.has_next()) { auto key = keys.next(); // "a", "c", "e", "g" } ``` ``` -------------------------------- ### Create bench Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Defines the 'bench' executable and configures its include paths. ```cmake add_executable(bench bench.cpp) target_link_libraries(bench PRIVATE ada counters::counters) target_include_directories(bench PUBLIC "$") target_include_directories(bench PUBLIC "$") ``` -------------------------------- ### Build Servo URL Crate Source: https://github.com/ada-url/ada/blob/main/benchmarks/competitors/servo-url/README.md Build the servo-url Rust crate in release mode to prepare it for FFI usage. ```bash cargo build --release ``` -------------------------------- ### Find ICU Library on macOS Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Configures CMake to find the ICU library on macOS, prioritizing Homebrew installations. It appends include and library paths to CMake's search lists. ```cmake if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin") message(STATUS "Apple system detected.") # People who run macOS often use brew. if(EXISTS /opt/homebrew/opt/icu4c) message(STATUS "icu is provided by homebrew at /opt/homehomebrew/opt/icu4c.") ## This is a bit awkward, but it is a lot better than asking the ## user to figure that out. list(APPEND CMAKE_PREFIX_PATH "/opt/homebrew/opt/icu4c/include") list(APPEND CMAKE_LIBRARY_PATH "/opt/homebrew/opt/icu4c/lib") elseif(EXISTS /usr/local/opt/icu4c) message(STATUS "icu is provided by homebrew at /usr/local/opt/icu4c.") list(APPEND CMAKE_PREFIX_PATH "/usr/local/opt/icu4c/include") list(APPEND CMAKE_LIBRARY_PATH "/usr/local/opt/icu4c/lib") endif() endif() ``` -------------------------------- ### Set URL Size Limit Source: https://github.com/ada-url/ada/blob/main/README.md Shows how to set a maximum input length for URLs, reject URLs exceeding the limit during parsing or via setters, and query the current limit. ```c++ // Set a 2 KB limit (any uint32_t value works). ada::set_max_input_length(2048); // Parsing a URL whose normalized form exceeds 2 KB fails. auto url = ada::parse("http://example.com/" + std::string(2048, 'a')); assert(!url); // too long // Setters that would push the URL over the limit are rejected. auto u = ada::parse("http://example.com/"); assert(u); bool ok = u->set_pathname(std::string(2048, 'x')); assert(!ok); // pathname too long; URL unchanged // Read the current limit. uint32_t limit = ada::get_max_input_length(); // Reset to the default (no practical limit). ada::set_max_input_length(UINT32_MAX); ``` -------------------------------- ### Conditional Boost URL Configuration Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt This snippet configures the option for installing Boost URL and checks the compiler version. If the compiler is GNU and older than version 9, it disables the Boost URL option. ```cmake option(ADA_BOOST_URL "Whether to install boost URL." OFF) message(STATUS "Compiler is " ${CMAKE_CXX_COMPILER_ID}) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") message(STATUS "Compiler version " ${CMAKE_CXX_COMPILER_VERSION}) if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) message(STATUS "Compiler is too old, disabling boost url.") SET(ADA_BOOST_URL OFF CACHE BOOL "Whether to install boost URL." FORCE) endif() endif() ``` -------------------------------- ### Define and Use URLPattern with Custom Regex Source: https://github.com/ada-url/ada/blob/main/README.md Shows how to define a URLPattern using a custom regex provider (e.g., V8 regex engine) and then match or test URLs against this pattern. ```c++ // Define a regex engine that conforms to the following interface // For example we will use v8 regex engine class v8_regex_provider { public: v8_regex_provider() = default; using regex_type = v8::Global; static std::optional create_instance(std::string_view pattern, bool ignore_case); static std::optional>> regex_search( std::string_view input, const regex_type& pattern); static bool regex_match(std::string_view input, const regex_type& pattern); }; // Define a URLPattern auto pattern = ada::parse_url_pattern("/books/:id(\d+)", "https://example.com"); // Check validity if (!pattern) { return EXIT_FAILURE; } // Match a URL auto match = pattern->match("https://example.com/books/123"); // Test a URL auto matched = pattern->test("https://example.com/books/123"); ``` -------------------------------- ### Faster Ada Builds with Ninja Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Configure and build the Ada library using the Ninja build system for faster compilation times. Supports enabling tests or benchmarks. ```bash cmake -B build -G Ninja -DADA_TESTING=ON && cmake --build build ``` ```bash cmake -B build -G Ninja -DADA_BENCHMARKS=ON -DADA_USE_UNSAFE_STD_REGEX_PROVIDER=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build ``` -------------------------------- ### Build Ada with Benchmarks (Detailed) Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Build the Ada library with benchmarks enabled. It is critical to use `CMAKE_BUILD_TYPE=Release` to disable development checks for accurate performance measurements. ```bash cmake -B build -DADA_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release cmake --build build ``` -------------------------------- ### Add url-dataset Package and Configure benchdata Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Adds the 'url-dataset' package and configures the 'benchdata' executable, including specific compile definitions for data paths and benchmark prefixes. ```cmake CPMAddPackage("gh:ada-url/url-dataset#9749b92c13e970e70409948fa862461191504ccc") add_executable(benchdata bench.cpp) target_link_libraries(benchdata PRIVATE ada counters::counters) target_include_directories(benchdata PUBLIC "$") target_include_directories(benchdata PUBLIC "$") target_compile_definitions(benchdata PRIVATE ADA_URL_FILE="${url-dataset_SOURCE_DIR}/out.txt") target_compile_definitions(benchdata PRIVATE BENCHMARK_PREFIX=BenchData_) target_compile_definitions(benchdata PRIVATE BENCHMARK_PREFIX_STR="BenchData_") ``` -------------------------------- ### Correct Benchmark Build Configuration Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Build Ada with benchmarks enabled in Release mode to ensure development checks are disabled for accurate performance measurements. ```bash # CORRECT: Benchmarks with development checks disabled cmake -B build -DADA_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release cmake --build build ./build/benchmarks/benchdata ``` -------------------------------- ### Add CPM Packages Source: https://github.com/ada-url/ada/blob/main/tools/cli/CMakeLists.txt Adds 'fmt' and 'cxxopts' libraries as dependencies using CPM.cmake. ```cmake CPMAddPackage("gh:fmtlib/fmt#11.0.2") CPMAddPackage( GITHUB_REPOSITORY jarro2783/cxxopts VERSION 3.2.0 OPTIONS "CXXOPTS_BUILD_EXAMPLES NO" "CXXOPTS_BUILD_TESTS NO" "CXXOPTS_ENABLE_INSTALL YES" ) ``` -------------------------------- ### Create bench_protocol Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Builds the 'bench_protocol' executable by linking it against the 'ada' and 'counters' libraries. ```cmake add_executable(bench_protocol bench_protocol.cpp) target_link_libraries(bench_protocol PRIVATE ada counters::counters) ``` -------------------------------- ### Create bench_search_params Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Builds the 'bench_search_params' executable, linking it with 'ada' and 'counters'. ```cmake add_executable(bench_search_params bench_search_params.cpp) target_link_libraries(bench_search_params PRIVATE ada counters::counters) ``` -------------------------------- ### Run Ada Main Benchmark Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Execute the main benchmark suite for the Ada URL parser, typically named `benchdata`, located in the build's benchmarks directory. ```bash ./build/benchmarks/benchdata ``` -------------------------------- ### Add url_whatwg Package and Library Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Adds the url_whatwg package from GitHub and creates a static library from its source files. It links against ICU and sets include directories and compile definitions. ```cmake CPMAddPackage( NAME url_whatwg GITHUB_REPOSITORY rmisev/url_whatwg GIT_TAG 72bcabf OPTIONS "URL_BUILD_TESTS OFF" "URL_USE_LIBS OFF" ) add_library(url_whatwg_lib STATIC "${url_whatwg_SOURCE_DIR}/src/url.cpp" "${url_whatwg_SOURCE_DIR}/src/url_idna.cpp" "${url_whatwg_SOURCE_DIR}/src/url_ip.cpp" "${url_whatwg_SOURCE_DIR}/src/url_percent_encode.cpp" "${url_whatwg_SOURCE_DIR}/src/url_search_params.cpp" "${url_whatwg_SOURCE_DIR}/src/url_utf.cpp" "${url_whatwg_SOURCE_DIR}/src/url.cpp") target_include_directories(url_whatwg_lib PUBLIC "${url_whatwg_SOURCE_DIR}/include") target_link_libraries(url_whatwg_lib PRIVATE ICU::uc ICU::i18n) target_link_libraries(bench PRIVATE url_whatwg_lib) target_link_libraries(benchdata PRIVATE url_whatwg_lib) target_link_libraries(bbc_bench PRIVATE url_whatwg_lib) target_link_libraries(wpt_bench PRIVATE url_whatwg_lib) target_include_directories(bench PUBLIC "${url_whatwg_SOURCE_DIR}") target_include_directories(benchdata PUBLIC "${url_whatwg_SOURCE_DIR}") target_include_directories(bbc_bench PUBLIC "${url_whatwg_SOURCE_DIR}") target_include_directories(wpt_bench PUBLIC "${url_whatwg_SOURCE_DIR}") target_compile_definitions(bench PRIVATE ADA_url_whatwg_ENABLED=1) target_compile_definitions(benchdata PRIVATE ADA_url_whatwg_ENABLED=1) target_compile_definitions(bbc_bench PRIVATE ADA_url_whatwg_ENABLED=1) target_compile_definitions(wpt_bench PRIVATE ADA_url_whatwg_ENABLED=1) ``` -------------------------------- ### URLPattern with Custom Regex Engine Source: https://github.com/ada-url/ada/blob/main/README.md Illustrates how to define and use URLPattern with a custom regular expression engine, emphasizing security and performance considerations. It shows how to create a pattern, match a URL against it, and test for a match. ```APIDOC ### URLPattern Our implementation doesn't provide a regex engine and leaves the decision of choosing the right engine to the user. This is done as a security measure since the default std::regex engine is not safe and open to DDOS attacks. Runtimes like Node.js and Cloudflare Workers use the V8 regex engine, which is safe and performant. ```cpp // Define a regex engine that conforms to the following interface // For example we will use v8 regex engine class v8_regex_provider { public: v8_regex_provider() = default; using regex_type = v8::Global; static std::optional create_instance(std::string_view pattern, bool ignore_case); static std::optional>> regex_search( std::string_view input, const regex_type& pattern); static bool regex_match(std::string_view input, const regex_type& pattern); }; // Define a URLPattern auto pattern = ada::parse_url_pattern("/books/:id(\d+)", "https://example.com"); // Check validity if (!pattern) { return EXIT_FAILURE; } // Match a URL auto match = pattern->match("https://example.com/books/123"); // Test a URL auto matched = pattern->test("https://example.com/books/123"); ``` ``` -------------------------------- ### Run Clang Format and Tidy Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Execute the clang-format and clang-tidy script before committing. This script formats source files and runs static analysis on the main translation unit. ```bash bash tools/run-clangcldocker.sh ``` -------------------------------- ### Apple Platform Optimizations Source: https://github.com/ada-url/ada/blob/main/tools/cli/CMakeLists.txt Applies dead_strip and LTO (Link-Time Optimization) for size reduction on Apple platforms. ```cmake if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") # We are on an Apple platform, so we can use dead_strip and LTO # to reduce the size of the final binary. message(STATUS "Apple platform detected, using dead_strip and LTO") target_link_options(adaparse PRIVATE "-Wl,-dead_strip") target_compile_options(adaparse PRIVATE "-flto") target_link_options(adaparse PRIVATE "-flto") endif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") ``` -------------------------------- ### Compile Ada Demo Application Source: https://github.com/ada-url/ada/blob/main/README.md Command to compile the C++ demo file using a C++20 compliant compiler. Ensure you have GCC 12+, LLVM 14+, or Visual Studio 2022. ```bash c++ -std=c++20 -o demo demo.cpp ``` -------------------------------- ### Manipulate URL Search Parameters Source: https://github.com/ada-url/ada/blob/main/README.md Illustrates how to create URL search parameters from a string, append new parameters, retrieve values by key, and iterate over keys. ```c++ ada::url_search_params search_params("a=b&c=d&e=f"); search_params.append("g=h"); search_params.get("g"); // will return "h" auto keys = search_params.get_keys(); while (keys.has_next()) { auto key = keys.next(); // "a", "c", "e", "g" } ``` -------------------------------- ### Run Amalgamation Script Source: https://github.com/ada-url/ada/blob/main/docs/RELEASE.md Execute the amalgamation script to prepare the single-header files for release. Ensure you are in the project's root directory. ```bash ./singleheader/amalgamate.py ``` -------------------------------- ### Interface Library for Include Directories Source: https://github.com/ada-url/ada/blob/main/singleheader/CMakeLists.txt Creates an interface library to manage include directories for the single-header build. It includes the build directory and conditionally includes SIMDUTF if enabled. ```cmake add_library(ada-singleheader-include-source INTERFACE) target_include_directories(ada-singleheader-include-source INTERFACE $) if (ADA_USE_SIMDUTF) target_include_directories(ada-singleheader-include-source INTERFACE ${simdutf_SOURCE_DIR}/include) endif() add_dependencies(ada-singleheader-include-source ada-singleheader-files) ``` -------------------------------- ### Create bench_ipv4 Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Defines the 'bench_ipv4' executable, linking it with 'ada' and 'counters', and setting compile definitions and include paths. ```cmake add_executable(bench_ipv4 bench_ipv4.cpp) target_link_libraries(bench_ipv4 PRIVATE ada counters::counters) target_include_directories(bench_ipv4 PUBLIC "$") target_include_directories(bench_ipv4 PUBLIC "$") target_compile_definitions(bench_ipv4 PRIVATE ADA_URL_FILE="${url-dataset_SOURCE_DIR}/out.txt") ``` -------------------------------- ### CMake Build Configurations for Ada Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Summary of common CMake build commands for library, testing, benchmarking, and development builds. Note the status of testing and benchmarking for each configuration. ```cmake cmake -B build && cmake --build build ``` ```cmake cmake -B build -DADA_TESTING=ON && cmake --build build ``` ```cmake cmake -B build -DADA_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build ``` ```cmake cmake -B build -DADA_TESTING=ON -DCMAKE_BUILD_TYPE=Debug && cmake --build build ``` -------------------------------- ### Build Ada with Tests Enabled Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Build the Ada library with development checks and tests enabled. Use `ctest` to run the tests after building. ```bash cmake -B build -DADA_TESTING=ON && cmake --build build ctest --output-on-failure --test-dir build ``` -------------------------------- ### Download Ada Library Files Source: https://github.com/ada-url/ada/blob/main/README.md Instructions to download the Ada C++ library source files using wget. These files are required for local compilation. ```bash wget https://github.com/ada-url/ada/releases/download/v3.0.0/ada.cpp wget https://github.com/ada-url/ada/releases/download/v3.0.0/ada.h ``` -------------------------------- ### Development Cycle with Tests Source: https://github.com/ada-url/ada/blob/main/AGENTS.md After making code changes, rebuild the project and run tests to ensure correctness. This process is optimized to only rebuild changed files. ```bash # Make code changes... # Rebuild (only rebuilds changed files) cmake --build build # Run tests to verify correctness ctest --output-on-failure --test-dir build # Or run specific test ./build/tests/basic_tests ``` -------------------------------- ### Advanced Usage: Extract Host and Benchmark Source: https://github.com/ada-url/ada/blob/main/docs/cli.md Combine flags to extract specific data (e.g., host using -g host), output to a file (-o), and run in benchmark mode (-b). ```bash adaparse" -p wikipedia_top100.txt -o test_write.txt -g host -b ``` -------------------------------- ### Configure Test JavaScript File Source: https://github.com/ada-url/ada/blob/main/tests/wasm/CMakeLists.txt Copies a template JavaScript file ('test.js.in') to its final destination ('test.js') for use in testing. ```cmake configure_file(test.js.in test.js) ``` -------------------------------- ### Create percent_encode Executable with Post-Build Copy Command Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Builds the 'percent_encode' executable and includes a custom post-build command to copy the 'ada' DLL if using MSVC and shared libraries. ```cmake add_executable(percent_encode percent_encode.cpp) target_link_libraries(percent_encode PRIVATE ada counters::counters) target_include_directories(percent_encode PUBLIC "$") target_include_directories(percent_encode PUBLIC "$") if(MSVC AND BUILD_SHARED_LIBS) # Copy the ada dll into the directory add_custom_command(TARGET percent_encode POST_BUILD # Adds a post-build event COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake -E copy_if_different..." "$" # <--this is in-file "$") # <--this is out-file path endif() ``` -------------------------------- ### Benchmark Comparison: Adaparse vs Trurl (Top100 Dataset) Source: https://github.com/ada-url/ada/blob/main/docs/cli.md Compares the performance of adaparse and trurl when processing the top100 dataset. Shows time taken for each command. ```bash time cat url-various-datasets/top100/top100.txt| trurl --url-file - &> /dev/null 1 cat url-various-datasets/top100/top100.txt 0,00s user 0,00s system 4% cpu 0,115 total trurl --url-file - &> /dev/null 0,09s user 0,02s system 97% cpu 0,113 total time cat url-various-datasets/top100/top100.txt| ./build/tools/cli/adaparse -g href &> /dev/null cat url-various-datasets/top100/top100.txt 0,00s user 0,01s system 11% cpu 0,062 total ./build/tools/cli/adaparse -g href &> /dev/null 0,05s user 0,00s system 94% cpu 0,061 total ``` -------------------------------- ### Ada URL Parser Benchmark Results Source: https://github.com/ada-url/ada/blob/main/README.md Performance comparison of Ada against Servo URL and CURL for URL parsing. Results are measured in nanoseconds per URL. ```text ada ▏ 188 ns/URL ███▏ servo url ▏ 664 ns/URL ███████████▎ CURL ▏ 1471 ns/URL █████████████████████████ ``` -------------------------------- ### Add Corrosion Package Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Adds the Corrosion package from GitHub, downloading it but not building it initially. This is a prerequisite for using Corrosion's CMake functions. ```cmake CPMAddPackage( NAME corrosion GITHUB_REPOSITORY corrosion-rs/corrosion VERSION 0.5.0 DOWNLOAD_ONLY ON OPTIONS "Rust_FIND_QUIETLY OFF" ) ``` -------------------------------- ### URL Size Limit Configuration Source: https://github.com/ada-url/ada/blob/main/README.md Explains how to set and manage the maximum input length for URLs to prevent excessive memory usage or potential denial-of-service attacks. It covers setting a limit, checking if a URL exceeds it during parsing or updates, and resetting to the default. ```APIDOC ## URL Size Limit By default, ada allows URLs up to about 4 GB. You can set a lower limit to reject any URL whose serialized form (the href) would exceed a given number of bytes. The limit is enforced during parsing and across all setters. Setters that return `bool` return `false` when the limit would be exceeded; `void` setters (`set_search`, `set_hash`) silently leave the URL unchanged. In all cases the URL is never modified when a limit violation is detected. Percent-encoding expansion is accounted for: a short input that encodes into a long result is still rejected. ```cpp // Set a 2 KB limit (any uint32_t value works). ada::set_max_input_length(2048); // Parsing a URL whose normalized form exceeds 2 KB fails. auto url = ada::parse("http://example.com/" + std::string(2048, 'a')); assert(!url); // too long // Setters that would push the URL over the limit are rejected. auto u = ada::parse("http://example.com/"); assert(u); bool ok = u->set_pathname(std::string(2048, 'x')); assert(!ok); // pathname too long; URL unchanged // Read the current limit. uint32_t limit = ada::get_max_input_length(); // Reset to the default (no practical limit). ada::set_max_input_length(UINT32_MAX); ``` You can query the byte length of a URL without allocating a string via `get_href_size()`: ```c++ auto url = ada::parse("https://example.com/path"); assert(url); size_t len = url->get_href_size(); // no allocation assert(len == url->get_href().size()); ``` ``` -------------------------------- ### Build Ada URL Parser with Docker Source: https://github.com/ada-url/ada/blob/main/README.md Build the Ada URL parser using the provided Dockerfile. This command builds the Docker image and then runs a container to perform the build within the repository's root directory. ```bash docker build -t ada-builder . docker run --rm -it -v ${PWD}:/repo ada-builder ``` -------------------------------- ### Build and Test Ada URL Parser with Git Source: https://github.com/ada-url/ada/blob/main/README.md Build the Ada URL parser with testing enabled, which requires git. After building, run the tests using ctest. ```bash cmake -B build -DADA_TESTING=ON && cmake --build build ctest --output-on-failure --test-dir build ``` -------------------------------- ### Basic Ada Library Build Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Command to perform a basic build of the Ada library using CMake, without enabling tests or benchmarks. ```bash cmake -B build cmake --build build ``` -------------------------------- ### Build and Test Ada URL Parser with Local Packages Source: https://github.com/ada-url/ada/blob/main/README.md Build the Ada URL parser with testing enabled and configured to use local packages. This requires available local packages and git. After building, run the tests using ctest. ```bash cmake -B build -DADA_TESTING=ON -D CPM_USE_LOCAL_PACKAGES=ON && cmake --build build ctest --output-on-failure --test-dir build ``` -------------------------------- ### Link Benchmarking Library to Executables Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Links the 'benchmark::benchmark' library to various executables, enabling them to use the benchmarking framework. ```cmake target_link_libraries(wpt_bench PRIVATE benchmark::benchmark) target_link_libraries(bench PRIVATE benchmark::benchmark) target_link_libraries(benchdata PRIVATE benchmark::benchmark) target_link_libraries(bbc_bench PRIVATE benchmark::benchmark) target_link_libraries(bench_ipv4 PRIVATE benchmark::benchmark) target_link_libraries(percent_encode PRIVATE benchmark::benchmark) target_link_libraries(bench_search_params PRIVATE benchmark::benchmark) target_link_libraries(urlpattern PRIVATE benchmark::benchmark) ``` -------------------------------- ### Add Performance Counters Package Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Manages the 'counters' performance counter library using CPM.AddPackage, specifying its GitHub repository and Git tag. ```cmake CPMAddPackage( NAME counters GITHUB_REPOSITORY lemire/counters GIT_TAG v3.0.0 ) ``` -------------------------------- ### Create model_bench Executable Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Defines the 'model_bench' executable and sets a compile definition for the ADA URL file. ```cmake add_executable(model_bench model_bench.cpp) target_link_libraries(model_bench PRIVATE ada counters::counters) target_compile_definitions(model_bench PRIVATE ADA_URL_FILE="${url-dataset_SOURCE_DIR}/out.txt") ``` -------------------------------- ### Run Specific Ada Benchmarks Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Execute specific benchmark executables for Ada URL parsing, including basic parsing, BBC URLs, Web Platform Tests, and percent encoding. ```bash ./build/benchmarks/bench ``` ```bash ./build/benchmarks/bbc_bench ``` ```bash ./build/benchmarks/wpt_bench ``` ```bash ./build/benchmarks/percent_encode ``` -------------------------------- ### Benchmark Comparison: Adaparse vs Trurl (Wikipedia Dataset) Source: https://github.com/ada-url/ada/blob/main/docs/cli.md Compares the performance of adaparse and trurl when processing the wikipedia_100k dataset. Shows time taken for each command. ```bash time cat url-various-datasets/wikipedia/wikipedia_100k.txt| trurl --url-file - &> /dev/null 1 cat url-various-datasets/wikipedia/wikipedia_100k.txt 0,00s user 0,01s system 3% cpu 0,179 total trurl --url-file - &> /dev/null 0,14s user 0,03s system 98% cpu 0,180 total time cat url-various-datasets/wikipedia/wikipedia_100k.txt| ./build/tools/cli/adaparse -g href &> /dev/null cat url-various-datasets/wikipedia/wikipedia_100k.txt 0,00s user 0,00s system 10% cpu 0,056 total ./build/tools/cli/adaparse -g href &> /dev/null 0,05s user 0,00s system 93% cpu 0,055 total ``` -------------------------------- ### Run Benchmark with Piped Input Source: https://github.com/ada-url/ada/blob/main/docs/cli.md Use the benchmark flag (-b) to measure the processing time of piped input. This is useful for performance analysis. ```bash cat wikipedia_100k.txt | adaparse -b ``` -------------------------------- ### Build RISC-V Vector Extension Code Source: https://github.com/ada-url/ada/blob/main/cmake/toolchains-dev/README.md Commands for setting up the build environment and compiling RISC-V Vector Extension code on Debian/Ubuntu systems. Requires GCC-13/CLANG-16 or newer. ```bash # For Debian/Ubuntu $ sudo apt install g++-riscv64-linux-gnu qemu-system-riscv qemu-user $ mkdir build; cd build $ export QEMU_LD_PREFIX="/usr/riscv64-linux-gnu" $ export QEMU_CPU="rv64,v=on" $ mkdir build && cd build $ cmake -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains-dev/riscv64-rvv.cmake -DADA_TESTING=ON .. $ cmake --build -j $(nproc) ``` -------------------------------- ### Add httpparser Package Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Adds the http-parser package from GitHub, creates a static library from its C source file, and links it against the 'bench' and 'bbc_bench' targets. This is conditionally compiled if ADA_COMPETITION is enabled. ```cmake # HTTP Parser CPMAddPackage( NAME httpparser GITHUB_REPOSITORY nodejs/http-parser VERSION 2.9.4 ) add_library(httpparser STATIC "${httpparser_SOURCE_DIR}/http_parser.c") set_source_files_properties("${httpparser_SOURCE_DIR}/http_parser.c" PROPERTIES LANGUAGE C) target_include_directories(httpparser PUBLIC "${httpparser_SOURCE_DIR}") target_link_libraries(bench PRIVATE httpparser) target_link_libraries(bbc_bench PRIVATE httpparser) ``` -------------------------------- ### Build Ada with Tests Enabled (Detailed) Source: https://github.com/ada-url/ada/blob/main/AGENTS.md Build the Ada library with tests enabled using CMake. Development checks are active by default unless Release mode is specified. ```bash cmake -B build -DADA_TESTING=ON cmake --build build ``` -------------------------------- ### Run Amalgamation Script Source: https://github.com/ada-url/ada/blob/main/singleheader/README.md Execute the amalgamation script to generate ada.h and ada.cpp from the command line. ```bash python singleheader/amalgamate.py ``` -------------------------------- ### Enable Competition Libraries Source: https://github.com/ada-url/ada/blob/main/benchmarks/CMakeLists.txt Sets compile definitions to enable various competition-related libraries if ADA_COMPETITION is defined. This applies to 'bench' and 'bbc_bench' targets. ```cmake target_compile_definitions(bench PRIVATE ADA_VARIOUS_COMPETITION_ENABLED=1) target_compile_definitions(bbc_bench PRIVATE ADA_VARIOUS_COMPETITION_ENABLED=1) ``` -------------------------------- ### Ada Demo Output Source: https://github.com/ada-url/ada/blob/main/README.md Expected output from the Ada URL parsing demo application after successful execution. ```text http: www.google.com ``` -------------------------------- ### Write Ada Package Version File Source: https://github.com/ada-url/ada/blob/main/CMakeLists.txt Creates the 'ada-config-version.cmake' file to specify the version compatibility of the Ada package. ```cmake write_basic_package_version_file( ada-config-version.cmake COMPATIBILITY SameMinorVersion ) ```