mitteLib
Loading...
Searching...
No Matches
strutils.hpp
Go to the documentation of this file.
1//
2// Created by spak on 2/1/23.
3//
4
5#ifndef MLAB_STRUTILS_HPP
6#define MLAB_STRUTILS_HPP
7
8#include <chrono>
9#include <mlab/bin_data.hpp>
10#include <optional>
11
12namespace mlab {
13 using datetime = std::chrono::time_point<std::chrono::system_clock>;
14
15 [[nodiscard]] range<std::uint8_t const *> data_view_from_string(std::string_view s);
16 [[nodiscard]] bin_data data_from_string(std::string_view s);
17
18 template <std::size_t N>
19 [[nodiscard]] std::string data_to_string(std::array<std::uint8_t, N> const &v);
20 [[nodiscard]] std::string data_to_string(std::vector<std::uint8_t> const &v);
21 [[nodiscard]] std::string data_to_string(range<bin_data::const_iterator> rg);
22 [[nodiscard]] std::string data_to_string(range<std::uint8_t const *> rg);
23
24 template <std::size_t N>
25 [[nodiscard]] std::string data_to_hex_string(std::array<std::uint8_t, N> const &v);
26 [[nodiscard]] std::string data_to_hex_string(std::vector<std::uint8_t> const &v);
27 [[nodiscard]] std::string data_to_hex_string(range<bin_data::const_iterator> rg);
28 [[nodiscard]] std::string data_to_hex_string(range<std::uint8_t const *> rg);
29
30 [[nodiscard]] bin_data data_from_hex_string(std::string_view s);
31
32 template <class It>
33 [[nodiscard]] std::string data_to_hex_string(It begin, It end);
34
35 [[nodiscard]] std::string replace_all(std::string_view text, std::string_view search, std::string_view replace);
36
37 [[nodiscard]] std::string concatenate(std::vector<std::string_view> const &strs, std::string_view separator = "");
38 [[nodiscard]] std::string concatenate_s(std::vector<std::string> const &strs, std::string_view separator = "");
39
43 [[nodiscard]] std::optional<datetime> strptime(std::string_view s, std::string_view fmt);
44
48 [[nodiscard]] std::string strftime(datetime const &dt, std::string_view fmt);
49
50 bin_data &operator<<(encode_length<bin_data> w, std::string_view sv);
52
53}// namespace mlab
54
55
56namespace mlab {
57 template <std::size_t N>
58 std::string data_to_string(std::array<std::uint8_t, N> const &v) {
59 return data_to_string(make_range(v));
60 }
61
62 template <std::size_t N>
63 std::string data_to_hex_string(std::array<std::uint8_t, N> const &v) {
65 }
66
67 template <class It>
68 std::string data_to_hex_string(It begin, It end) {
69 std::string retval;
70 // Include one NUL ending char needed for snprintf
71 retval.resize(2 * std::distance(begin, end) + 1);
72 auto it = std::begin(retval);
73 for (; begin != end; ++begin, it += 2) {
74 std::snprintf(&*it, 3, "%02x", *begin);
75 }
76 // Remove the final null separator
77 retval.pop_back();
78 return retval;
79 }
80}// namespace mlab
81
82#endif//MLAB_STRUTILS_HPP
Definition bin_data.hpp:127
Definition bin_data.hpp:177
Definition log.cpp:8
std::chrono::time_point< std::chrono::system_clock > datetime
Definition strutils.hpp:13
bin_data data_from_hex_string(std::string_view s)
Definition strutils.cpp:64
bin_data data_from_string(std::string_view s)
Definition strutils.cpp:40
std::string strftime(datetime const &dt, std::string_view fmt)
Definition strutils.cpp:145
std::string data_to_hex_string(std::vector< std::uint8_t > const &v)
Definition strutils.cpp:98
bin_data & operator<<(encode_length< bin_data > w, std::string_view sv)
Definition strutils.cpp:155
std::string concatenate_s(std::vector< std::string > const &strs, std::string_view separator)
Definition strutils.cpp:8
std::string concatenate(std::vector< std::string_view > const &strs, std::string_view separator)
Definition strutils.cpp:15
constexpr range< Iterator > make_range(Iterator begin, Iterator end)
Definition bin_data.hpp:70
bin_stream & operator>>(encode_length< bin_stream > w, std::string &c)
Definition strutils.cpp:159
std::string replace_all(std::string_view text, std::string_view search, std::string_view replace)
Definition strutils.cpp:110
std::optional< datetime > strptime(std::string_view s, std::string_view fmt)
Definition strutils.cpp:137
range< std::uint8_t const * > data_view_from_string(std::string_view s)
Definition strutils.cpp:34
std::string data_to_string(std::vector< std::uint8_t > const &v)
Definition strutils.cpp:73
Definition bin_data.hpp:308
Definition bin_data.hpp:31