Botan 3.13.0
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* Parse a SCAN-style algorithm name
23* @param scan_name the name
24* @return the name components
25*/
26std::vector<std::string> parse_algorithm_name(std::string_view scan_name);
27
28/**
29* Split a string
30* @param str the input string
31* @param delim the delimiter
32* @return string split by delim
33*/
34BOTAN_TEST_API std::vector<std::string> split_on(std::string_view str, char delim);
35
36/**
37* Join a string
38* @param strs strings to join
39* @param delim the delimiter
40* @return string joined by delim
41*/
42std::string string_join(const std::vector<std::string>& strs, char delim);
43
44/**
45* Convert a decimal string to a number, throwing if invalid
46* @param input the string to convert
47* @return number value of the string
48*/
49BOTAN_TEST_API uint32_t to_u32bit(std::string_view input);
50
51/**
52* Attempt to parse a string as a 16-bit decimal integer
53*
54* @param input the string to convert
55* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
56* @return integer value, or nullopt if invalid
57*/
58std::optional<uint16_t> parse_u16(std::string_view input, bool require_canonical = false);
59
60/**
61* Attempt to parse a string as a 32-bit decimal integer
62*
63* @param input the string to convert
64* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
65* @return integer value, or nullopt if invalid
66*/
67BOTAN_TEST_API std::optional<uint32_t> parse_u32(std::string_view input, bool require_canonical = false);
68
69/**
70* Attempt to parse a string as a 64-bit decimal integer
71*
72* @param input the string to convert
73* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
74* @return integer value, or nullopt if invalid
75*/
76BOTAN_TEST_API std::optional<uint64_t> parse_u64(std::string_view input, bool require_canonical = false);
77
78/**
79* Attempt to parse a string as a size_t-sized decimal integer
80*
81* @param input the string to convert
82* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
83* @return integer value, or nullopt if invalid
84*/
85BOTAN_TEST_API std::optional<size_t> parse_sz(std::string_view input, bool require_canonical = false);
86
87std::map<std::string, std::string> read_cfg(std::istream& is);
88
89/**
90* Accepts key value pairs delimited by commas:
91*
92* "" (returns empty map)
93* "K=V" (returns map {'K': 'V'})
94* "K1=V1,K2=V2"
95* "K1=V1,K2=V2,K3=V3"
96* "K1=V1,K2=V2,K3=a_value\,with\,commas_and_\=equals"
97*
98* Values may be empty, keys must be non-empty and unique. Duplicate
99* keys cause an exception.
100*
101* Within both key and value, comma and equals can be escaped with
102* backslash. Backslash can also be escaped.
103*/
105std::map<std::string, std::string> read_kv(std::string_view kv);
106
107std::string tolower_string(std::string_view str);
108
109} // namespace Botan
110
111#endif
#define BOTAN_TEST_API
Definition api.h:41
std::map< std::string, std::string > read_cfg(std::istream &is)
Definition read_cfg.cpp:35
std::optional< uint64_t > parse_u64(std::string_view input, bool require_canonical)
Definition parsing.cpp:68
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:183
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:141
std::optional< size_t > parse_sz(std::string_view input, bool require_canonical)
Definition parsing.cpp:72
std::optional< uint32_t > parse_u32(std::string_view input, bool require_canonical)
Definition parsing.cpp:64
std::vector< std::string > parse_algorithm_name(std::string_view scan_name)
Definition parsing.cpp:87
std::string string_join(const std::vector< std::string > &strs, char delim)
Definition parsing.cpp:170
std::optional< uint16_t > parse_u16(std::string_view input, bool require_canonical)
Definition parsing.cpp:60
uint32_t to_u32bit(std::string_view input)
Definition parsing.cpp:76