void test_strview_basics(const string_view& sv, const char *p, size_t n) { ASSERT_EQ(p, sv.data()); ASSERT_EQ(n, sv.size()); ASSERT_EQ(n, sv.length()); ASSERT_EQ((n == 0), sv.empty()); ASSERT_EQ(p, sv.cbegin()); ASSERT_EQ(p, sv.begin()); ASSERT_EQ(p + n, sv.cend()); ASSERT_EQ(p + n, sv.end()); using reviter_t = std::reverse_iterator<string_view::const_iterator>; ASSERT_EQ(reviter_t(sv.end()), sv.rbegin()); ASSERT_EQ(reviter_t(sv.begin()), sv.rend()); ASSERT_EQ(reviter_t(sv.cend()), sv.crbegin()); ASSERT_EQ(reviter_t(sv.cbegin()), sv.crend()); for (size_t i = 0; i < n; ++i) { ASSERT_EQ(p[i], sv[i]); ASSERT_EQ(p[i], sv.at(i)); } ASSERT_THROW(sv.at(n), std::out_of_range); ASSERT_THROW(sv.at(string_view::npos), std::out_of_range); if (n > 0) { ASSERT_EQ(p, &(sv.front())); ASSERT_EQ(p + (n-1), &(sv.back())); ASSERT_EQ(p[0], sv.front()); ASSERT_EQ(p[n-1], sv.back()); } }
string_view parse_token(string_view line, string_view::size_type& index) { auto initial_index = index; while ((!eol(line, index)) && (!is_whitespace(line.at(index)))) { ++index; } return string_view(&line[initial_index], index - initial_index); }
string_view parse_string(string_view line, string_view::size_type& index, char start_delimiter, char end_delimiter) { string_view result; string_view::size_type initial_index; if (line.at(index) != start_delimiter) throw runtime_error("Wrong starting delimiter"); ++index; initial_index = index; while (true) { if (eol(line, index)) break; if ((index > 0) && (line[index] == end_delimiter) && (line[index - 1] != '\\')) break; ++index; } if (eol(line, index)) throw runtime_error("Wrong ending delimiter"); assert(line.at(index) == end_delimiter); ++index; return string_view(&line[initial_index], index - initial_index - 1); }
path_match_result path_match(string_view input, string_view& match_contents) { if (input.length() < 2) return path_match_result::invalid; match_result result; token_kind kind; std::size_t length; switch (input.at(0)) { case '.': result = match_simple_string(input.data() + 1, input.data() + input.size(), kind, length); if (result == match_result::complete) { match_contents = input.substr(0, length + 1); return path_match_result::simple_object; } else { return path_match_result::invalid; } case '[': result = attempt_match(input.data() + 1, input.data() + input.length(), kind, length); if (result == match_result::complete) { if (input.length() == length + 1 || input.at(1 + length) != ']') return path_match_result::invalid; if (kind != token_kind::string && kind != token_kind::number) return path_match_result::invalid; match_contents = input.substr(0, length + 2); return path_match_result::brace; } else { return path_match_result::invalid; } default: return path_match_result::invalid; } }
void skip_whitespace(string_view line, string_view::size_type& index) { while ((!eol(line, index)) && (is_whitespace(line.at(index)))) { ++index; } }