From d81b1665441a5209d910f30fbde8f8ca14ad2fc5 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 12 Aug 2015 02:11:22 +0100 Subject: [PATCH] add range-based str parse --- cubescript.cc | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/cubescript.cc b/cubescript.cc index edfb77f..9ca394c 100644 --- a/cubescript.cc +++ b/cubescript.cc @@ -1016,6 +1016,21 @@ const char *parsestring(const char *p) { return p; } +ostd::ConstCharRange cs_parse_str(ostd::ConstCharRange str) { + for (; !str.empty(); str.pop_front()) + switch (str.front()) { + case '\r': + case '\n': + case '\"': + return str; + case '^': + str.pop_front(); + if (!str.empty()) break; + return str; + } + return str; +} + static char *conc(ostd::Vector &buf, TaggedValue *v, int n, bool space, const char *prefix = nullptr, int prefixlen = 0) { if (prefix) { buf.push_n(prefix, prefixlen); @@ -1665,12 +1680,15 @@ static bool compileblockstr(GenState &gs, ostd::ConstCharRange str, bool macro) str.pop_front(); break; case '\"': { - const char *start = str.data(); - const char *end = parsestring(start + 1); - if (*end == '\"') end++; - memcpy(&buf[len], start, end - start); - len += end - start; - str.pop_front_n(end - start); + ostd::ConstCharRange start = str; + start.pop_front(); + ostd::ConstCharRange end = cs_parse_str(start); + if (!end.empty() && (end.front() == '\"')) + end.pop_front(); + ostd::Size slen = str.distance_front(end); + memcpy(&buf[len], str.data(), slen); + len += slen; + str = end; break; } case '/':