AoC code coverage
Current view: top level - aoclib - Parsing.h (source / functions) Coverage Total Hit
Test: master Lines: 100.0 % 49 49
Test Date: 2025-12-11 19:43:23 Functions: 100.0 % 20 20

            Line data    Source code
       1              : #pragma once
       2              : 
       3              : #include <libassert/assert.hpp>
       4              : 
       5              : #include <algorithm>
       6              : #include <charconv>
       7              : #include <concepts>
       8              : #include <iterator>
       9              : #include <ranges>
      10              : #include <spanstream>
      11              : #include <string_view>
      12              : #include <vector>
      13              : 
      14              : template <typename... T> void parse(std::string_view const s, T &...values) {
      15              :   std::ispanstream iss(s);
      16              :   (iss >> ... >> values);
      17              : }
      18              : 
      19         1208 : template <typename T> std::vector<T> parseRange(std::string_view const s) {
      20         1208 :   std::ispanstream iss(s);
      21         1208 :   return {std::istream_iterator<T>(iss), std::istream_iterator<T>{}};
      22         1208 : }
      23              : 
      24            3 : template <typename T, typename OutIt> OutIt parseRange(std::string_view const s, OutIt out) {
      25            3 :   std::ispanstream iss(s);
      26            3 :   return std::copy(std::istream_iterator<T>(iss), std::istream_iterator<T>{}, out);
      27            3 : }
      28              : 
      29       100971 : template <std::integral T> T parseInt(std::string_view const s) {
      30       100971 :   T out{};
      31       100971 :   auto [ptr, ec] = std::from_chars(s.begin(), s.end(), out);
      32       100971 :   ASSERT(ec == std::errc(), s);
      33       100971 :   return out;
      34       100971 : }
      35              : 
      36        17544 : template <std::integral T> T parseInt(std::ranges::input_range auto &&r) {
      37        17544 :   std::string_view s(r);
      38        17544 :   return parseInt<T>(s);
      39        17544 : }
      40              : 
      41          624 : template <std::integral T> T parseHexInt(std::string_view const s) {
      42          624 :   T out{};
      43          624 :   auto [ptr, ec] = std::from_chars(s.begin(), s.end(), out, 16);
      44          624 :   ASSERT(ec == std::errc(), s);
      45          624 :   return out;
      46          624 : }
      47              : 
      48              : template <std::integral T>
      49         8601 : std::vector<T> parseIntegerRange(std::string_view const s, char const delim = ',') {
      50         8601 :   std::vector<T> result;
      51        63853 :   for (auto it = s.begin(); it < s.end(); ++it) {
      52        55252 :     auto startIt = it;
      53        55252 :     it = std::find(startIt, s.end(), delim);
      54        55252 :     result.push_back(parseInt<T>({startIt, it}));
      55        55252 :   }
      56         8601 :   return result;
      57         8601 : }
      58              : 
      59              : template <std::integral T, typename OutIt>
      60         2698 : void parseIntegerRange(std::string_view const s, OutIt out, char const delim = ',') {
      61        13883 :   for (auto it = s.begin(); it < s.end(); ++it, ++out) {
      62        11185 :     auto startIt = it;
      63        11185 :     it = std::find(startIt, s.end(), delim);
      64        11185 :     *out = parseInt<T>({startIt, it});
      65        11185 :   }
      66         2698 : }
      67              : 
      68         2270 : inline std::vector<std::string_view> split(std::string_view const s, char const delim = ',') {
      69         2270 :   std::vector<std::string_view> result;
      70        16791 :   for (auto it = s.begin(); it < s.end(); ++it) {
      71        14521 :     auto startIt = it;
      72        14521 :     it = std::find(startIt, s.end(), delim);
      73        14521 :     result.emplace_back(startIt, it);
      74        14521 :   }
      75         2270 :   return result;
      76         2270 : }
        

Generated by: LCOV version 2.0-1