Use string_view where appropriate for parser, add some additional constructors for ease of use

This commit is contained in:
Jonathan 2022-01-16 04:17:06 -05:00
parent 4d1901534f
commit 10fa97e6b8
6 changed files with 15 additions and 9 deletions

View File

@ -1033,7 +1033,7 @@ static void DecompileEntity(
continue; continue;
} else if (modelNum > 0 && keyValue.first == "origin") { } else if (modelNum > 0 && keyValue.first == "origin") {
auto &value = keyValue.second; auto &value = keyValue.second;
parser_t parser(value.data(), value.data() + value.size()); parser_t parser(value);
qvec3d vec; qvec3d vec;
parser.parse_token(); parser.parse_token();
vec[0] = stof(parser.token); vec[0] = stof(parser.token);

View File

@ -117,7 +117,7 @@ keyvalues_t::iterator entdict_t::end()
std::vector<entdict_t> EntData_Parse(const std::string &entdata) std::vector<entdict_t> EntData_Parse(const std::string &entdata)
{ {
std::vector<entdict_t> result; std::vector<entdict_t> result;
parser_t parser(entdata.data(), entdata.data() + entdata.size()); parser_t parser(entdata);
/* go through all the entities */ /* go through all the entities */
while (1) { while (1) {

View File

@ -47,7 +47,15 @@ struct parser_t
uint32_t linenum = 1; uint32_t linenum = 1;
std::string token; std::string token;
parser_t(const char *data, const char *in_end) : pos(data), end(in_end) { } // base constructor; accept raw start & length
parser_t(const void *start, size_t length) : pos(reinterpret_cast<const char *>(start)), end(reinterpret_cast<const char *>(start) + length) { }
// pull from string_view; note that the string view must live for the entire
// duration of the parser's life time
parser_t(const std::string_view &view) : parser_t(&view.front(), view.size()) { }
// pull from C string; made explicit because this is error-prone
explicit parser_t(const char *str) : parser_t(str, strlen(str)) { }
bool parse_token(parseflags flags = PARSE_NORMAL); bool parse_token(parseflags flags = PARSE_NORMAL);

View File

@ -1404,7 +1404,7 @@ bool ParseLightsFile(const std::filesystem::path &fname)
while (!f.eof()) { while (!f.eof()) {
std::getline(f, buf); std::getline(f, buf);
parser_t parser(buf.data(), buf.data() + buf.size()); parser_t parser(buf);
if (!parser.parse_token()) if (!parser.parse_token())
continue; continue;

View File

@ -1847,8 +1847,7 @@ mapentity_t LoadExternalMap(const char *filename)
FError("Couldn't load external map file \"{}.\"\n", filename); FError("Couldn't load external map file \"{}.\"\n", filename);
} }
const char *begin = reinterpret_cast<const char *>(file->data()); parser_t parser(file->data(), file->size());
parser_t parser(begin, begin + file->size());
// parse the worldspawn // parse the worldspawn
if (!ParseEntity(parser, &dest)) { if (!ParseEntity(parser, &dest)) {
@ -1896,8 +1895,7 @@ void LoadMapFile(void)
return; return;
} }
const char *begin = reinterpret_cast<const char *>(file->data()); parser_t parser(file->data(), file->size());
parser_t parser(begin, begin + file->size());
for (int i = 0;; i++) { for (int i = 0;; i++) {
mapentity_t &entity = map.entities.emplace_back(); mapentity_t &entity = map.entities.emplace_back();

View File

@ -32,7 +32,7 @@ static mapentity_t LoadMap(const char *map)
options.target_version = &bspver_q1; options.target_version = &bspver_q1;
options.target_game = options.target_version->game; options.target_game = options.target_version->game;
parser_t parser(map, map + strlen(map)); parser_t parser(map);
mapentity_t worldspawn; mapentity_t worldspawn;
// FIXME: adds the brush to the global map... // FIXME: adds the brush to the global map...