8#include <BufferStream.h>
16 || std::same_as<V, bool>
17 || std::same_as<V, int32_t>
18 || std::same_as<V, int64_t>
19 || std::same_as<V, float>;
21template<
typename S = std::
string_view>
22requires std::convertible_to<S, std::string_view>
26 [[nodiscard]] std::string_view
getKey()
const {
31 [[nodiscard]] std::string_view
getValue()
const {
36 template<KV1ValueType V>
38 if constexpr (std::convertible_to<V, std::string_view>) {
40 }
else if constexpr (std::same_as<V, bool>) {
42 }
else if constexpr (std::same_as<V, int32_t> || std::same_as<V, int64_t>) {
50 }
else if constexpr (std::same_as<V, float>) {
64 [[nodiscard]]
bool hasChild(std::string_view childKey)
const {
76 for (
const auto& element : this->
children) {
85 [[nodiscard]]
const std::vector<KV1ElementReadable>&
getChildren()
const {
89 using iterator = std::vector<KV1ElementReadable>::iterator;
129 for (
const auto& element : this->
children) {
139 unsigned int count = 0;
140 for (
const auto& element : this->
children) {
163 [[nodiscard]]
explicit operator bool()
const {
175 if (stream.peek<
char>() ==
'}') {
186 if (stream.peek<
char>() !=
'{') {
191 if (stream.peek<
char>() ==
'[') {
196 if (stream.peek<
char>() ==
'{') {
199 if (stream.peek<
char>() !=
'}') {
214template<
typename S = std::
string_view>
215requires std::convertible_to<S, std::string_view>
218 explicit KV1(std::string_view kv1Data,
bool useEscapeSequences_ =
false)
221 if (kv1Data.empty()) {
224 BufferStreamReadOnly stream{kv1Data};
227 this->backingData.resize(kv1Data.size() * 2);
228 BufferStream backing{this->backingData, false};
230 KV1ElementReadable<S>::read(stream, backing, this->children, sourcepp::parser::text::getDefaultEscapeSequencesOrNone(this->useEscapeSequences));
231 } catch (
const std::overflow_error&) {}
245inline KV1<> operator""_kv1(
const char* str,
const std::size_t len) {
246 return KV1{std::string_view{str, len}};
KV1ElementReadable()=default
std::string_view getConditional() const
Get the conditional associated with the element.
const KV1ElementReadable & operator[](std::string_view childKey) const
Get the first child element of the element with the given key.
constexpr const_iterator cend() const
V getValue() const
Get the value associated with the element as the given type.
const KV1ElementReadable & operator()(std::string_view childKey, unsigned int n) const
Get the nth child element of the element with the given key.
const KV1ElementReadable & operator()(std::string_view childKey) const
Get the first child element of the element with the given key.
std::vector< KV1ElementReadable > children
std::string_view getKey() const
Get the key associated with the element.
bool isInvalid() const
Check if the given element is invalid.
constexpr const_iterator cbegin() const
static const KV1ElementReadable & getInvalid()
uint64_t getChildCount() const
Get the number of child elements.
bool hasChild(std::string_view childKey) const
Check if the element has one or more children with the given key.
std::vector< KV1ElementReadable >::const_iterator const_iterator
static void read(BufferStreamReadOnly &stream, BufferStream &backing, std::vector< KV1ElementReadable > &elements, const sourcepp::parser::text::EscapeSequenceMap &escapeSequences)
std::vector< KV1ElementReadable >::iterator iterator
constexpr const_iterator end() const
const std::vector< KV1ElementReadable > & getChildren() const
Get the child elements of the element.
uint64_t getChildCount(std::string_view childKey) const
Get the number of child elements with the given key.
constexpr const_iterator begin() const
constexpr iterator begin()
std::string_view getValue() const
Get the value associated with the element.
const KV1ElementReadable & operator[](unsigned int n) const
Get the child element of the element at the given index.
KV1(std::string_view kv1Data, bool useEscapeSequences_=false)
std::unordered_map< char, char > EscapeSequenceMap
void eatWhitespaceAndSingleLineComments(BufferStream &stream, std::string_view singleLineCommentStart=DEFAULT_SINGLE_LINE_COMMENT_START)
Eat all whitespace and single line comments after the current stream position.
constexpr std::string_view DEFAULT_STRING_END
constexpr std::string_view DEFAULT_STRING_START
std::string_view readStringToBuffer(BufferStream &stream, BufferStream &backing, std::string_view start=DEFAULT_STRING_START, std::string_view end=DEFAULT_STRING_END, const EscapeSequenceMap &escapeSequences=getDefaultEscapeSequences())
Read a string starting at the current stream position.
bool isNumber(char c)
If a char is a numerical character (0-9).
std::from_chars_result toFloat(std::string_view number, std::floating_point auto &out)
std::from_chars_result toInt(std::string_view number, std::integral auto &out, int base=10)
bool iequals(std::string_view s1, std::string_view s2)