/*
* Various string utils and parsing functions
* (C) 1999-2007,2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_PARSING_UTILS_H_
#define BOTAN_PARSING_UTILS_H_

#include <botan/types.h>
#include <iosfwd>
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>

namespace Botan {

/**
* Parse a SCAN-style algorithm name
* @param scan_name the name
* @return the name components
*/
std::vector<std::string> parse_algorithm_name(std::string_view scan_name);

/**
* Split a string
* @param str the input string
* @param delim the delimiter
* @return string split by delim
*/
BOTAN_TEST_API std::vector<std::string> split_on(std::string_view str, char delim);

/**
* Join a string
* @param strs strings to join
* @param delim the delimiter
* @return string joined by delim
*/
std::string string_join(const std::vector<std::string>& strs, char delim);

/**
* Convert a decimal string to a number, throwing if invalid
* @param input the string to convert
* @return number value of the string
*/
BOTAN_TEST_API uint32_t to_u32bit(std::string_view input);

/**
* Attempt to parse a string as a 16-bit decimal integer
*
* @param input the string to convert
* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
* @return integer value, or nullopt if invalid
*/
std::optional<uint16_t> parse_u16(std::string_view input, bool require_canonical = false);

/**
* Attempt to parse a string as a 32-bit decimal integer
*
* @param input the string to convert
* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
* @return integer value, or nullopt if invalid
*/
BOTAN_TEST_API std::optional<uint32_t> parse_u32(std::string_view input, bool require_canonical = false);

/**
* Attempt to parse a string as a 64-bit decimal integer
*
* @param input the string to convert
* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
* @return integer value, or nullopt if invalid
*/
BOTAN_TEST_API std::optional<uint64_t> parse_u64(std::string_view input, bool require_canonical = false);

/**
* Attempt to parse a string as a size_t-sized decimal integer
*
* @param input the string to convert
* @param require_canonical if set, reject leading zeros ("007"); "0" is still accepted
* @return integer value, or nullopt if invalid
*/
BOTAN_TEST_API std::optional<size_t> parse_sz(std::string_view input, bool require_canonical = false);

std::map<std::string, std::string> read_cfg(std::istream& is);

/**
* Accepts key value pairs delimited by commas:
*
* "" (returns empty map)
* "K=V" (returns map {'K': 'V'})
* "K1=V1,K2=V2"
* "K1=V1,K2=V2,K3=V3"
* "K1=V1,K2=V2,K3=a_value\,with\,commas_and_\=equals"
*
* Values may be empty, keys must be non-empty and unique. Duplicate
* keys cause an exception.
*
* Within both key and value, comma and equals can be escaped with
* backslash. Backslash can also be escaped.
*/
BOTAN_TEST_API
std::map<std::string, std::string> read_kv(std::string_view kv);

std::string tolower_string(std::string_view str);

}  // namespace Botan

#endif
