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 1205 : template <typename T> std::vector<T> parseRange(std::string_view const s) {
20 1205 : std::ispanstream iss(s);
21 1205 : return {std::istream_iterator<T>(iss), std::istream_iterator<T>{}};
22 1205 : }
23 :
24 : template <typename T, typename OutIt> OutIt parseRange(std::string_view const s, OutIt out) {
25 : std::ispanstream iss(s);
26 : return std::copy(std::istream_iterator<T>(iss), std::istream_iterator<T>{}, out);
27 : }
28 :
29 78500 : template <std::integral T> T parseInt(std::string_view const s) {
30 78500 : T out{};
31 78500 : auto [ptr, ec] = std::from_chars(s.begin(), s.end(), out);
32 78500 : ASSUME(ec == std::errc(), s);
33 78500 : return out;
34 78500 : }
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 : ASSUME(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 2268 : inline std::vector<std::string_view> split(std::string_view const s, char const delim = ',') {
60 2268 : std::vector<std::string_view> result;
61 16729 : for (auto it = s.begin(); it < s.end(); ++it) {
62 14461 : auto startIt = it;
63 14461 : it = std::find(startIt, s.end(), delim);
64 14461 : result.emplace_back(startIt, it);
65 14461 : }
66 2268 : return result;
67 2268 : }
|