Botan 3.14.1
Crypto and TLS for C&
parsing.h
Go to the documentation of this file.
1/*
2* Various string utils and parsing functions
3* (C) 1999-2007,2013 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_PARSING_UTILS_H_
9#define BOTAN_PARSING_UTILS_H_
10
11#include <botan/types.h>
12#include <iosfwd>
13#include <map>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace Botan {
20
21/**
22* Split a string
23* @param str the input string
24* @param delim the delimiter
25* @return string split by delim
26*/
27BOTAN_TEST_API std::vector<std::string> split_on(std::string_view str, char delim);
28
29/**
30* Join a string
31* @param strs strings to join
32* @param delim the delimiter
33* @return string joined by delim
34*/
35std::string string_join(const std::vector<std::string>& strs, char delim);
36
37/**
38* Convert a decimal string to a number, throwing if invalid
39* @param input the string to convert
40* @return number value of the string
41*/
42BOTAN_TEST_API uint32_t to_u32bit(std::string_view input);
43
44/**
45* Attempt to parse a string as a 16-bit decimal integer
46*
47* @param input the string to convert
48* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
49* @return integer value, or nullopt if invalid
50*/
51std::optional<uint16_t> parse_u16(std::string_view input, bool require_canonical = false);
52
53/**
54* Attempt to parse a string as a 32-bit decimal integer
55*
56* @param input the string to convert
57* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
58* @return integer value, or nullopt if invalid
59*/
60BOTAN_TEST_API std::optional<uint32_t> parse_u32(std::string_view input, bool require_canonical = false);
61
62/**
63* Attempt to parse a string as a 64-bit decimal integer
64*
65* @param input the string to convert
66* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
67* @return integer value, or nullopt if invalid
68*/
69BOTAN_TEST_API std::optional<uint64_t> parse_u64(std::string_view input, bool require_canonical = false);
70
71/**
72* Attempt to parse a string as a size_t-sized decimal integer
73*
74* @param input the string to convert
75* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
76* @return integer value, or nullopt if invalid
77*/
78BOTAN_TEST_API std::optional<size_t> parse_sz(std::string_view input, bool require_canonical = false);
79
80std::map<std::string, std::string> read_cfg(std::istream& is);
81
82/**
83* Accepts key value pairs delimited by commas:
84*
85* "" (returns empty map)
86* "K=V" (returns map {'K': 'V'})
87* "K1=V1,K2=V2"
88* "K1=V1,K2=V2,K3=V3"
89* "K1=V1,K2=V2,K3=a_value\,with\,commas_and_\=equals"
90*
91* Values may be empty, keys must be non-empty and unique. Duplicate
92* keys cause an exception.
93*
94* Within both key and value, comma and equals can be escaped with
95* backslash. Backslash can also be escaped.
96*/
98std::map<std::string, std::string> read_kv(std::string_view kv);
99
100std::string tolower_string(std::string_view str);
101
102} // namespace Botan
103
104#endif
#define BOTAN_TEST_API
Definition api.h:41
std::map< std::string, std::string > read_cfg(std::istream &is)
Definition read_cfg.cpp:32
std::optional< uint64_t > parse_u64(std::string_view input, bool require_canonical)
Definition parsing.cpp:67
BOTAN_TEST_API std::map< std::string, std::string > read_kv(std::string_view kv)
Definition read_kv.cpp:13
std::string tolower_string(std::string_view str)
Definition parsing.cpp:125
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:83
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
Definition parsing.cpp:71
std::optional< uint32_t > parse_u32(std::string_view input, bool require_canonical)
Definition parsing.cpp:63
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:112
std::optional< uint16_t > parse_u16(std::string_view input, bool require_canonical)
Definition parsing.cpp:59
uint32_t to_u32bit(std::string_view input)
Definition parsing.cpp:75