From 0a88a10d99e51a8d087cd7ca65df5b75961c627a Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 11 Oct 2016 21:15:23 +0200 Subject: [PATCH] add a line-counting parse_string to util --- include/cubescript/cubescript.hh | 9 ++++++++- src/cs_util.cc | 23 ++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index 7b62a85..802c29c 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -742,9 +742,16 @@ namespace util { } OSTD_EXPORT ostd::ConstCharRange parse_string( - CsState &cs, ostd::ConstCharRange str + CsState &cs, ostd::ConstCharRange str, ostd::Size &nlines ); + inline ostd::ConstCharRange parse_string( + CsState &cs, ostd::ConstCharRange str + ) { + ostd::Size nlines; + return parse_string(cs, str, nlines); + } + OSTD_EXPORT ostd::ConstCharRange parse_word( CsState &cs, ostd::ConstCharRange str ); diff --git a/src/cs_util.cc b/src/cs_util.cc index 7fb6ff7..5c1612f 100644 --- a/src/cs_util.cc +++ b/src/cs_util.cc @@ -187,13 +187,16 @@ done: namespace util { OSTD_EXPORT ostd::ConstCharRange parse_string( - CsState &cs, ostd::ConstCharRange str + CsState &cs, ostd::ConstCharRange str, ostd::Size &nlines ) { + ostd::Size nl = 0; + nlines = nl; if (str.empty() || (*str != '\"')) { return str; } ostd::ConstCharRange orig = str; ++str; + ++nl; while (!str.empty()) { switch (*str) { case '\r': @@ -201,25 +204,31 @@ namespace util { case '\"': goto end; case '^': + case '\\': { + bool needn = (*str == '\\'); ++str; - if (!str.empty()) { - break; + if (str.empty()) { + goto end; } - goto end; - case '\\': - ++str; - if (!str.empty() && ((*str == '\r') || (*str == '\n'))) { + if ((*str == '\r') || (*str == '\n')) { char c = *str; ++str; + ++nl; if (!str.empty() && (c == '\r') && (*str == '\n')) { ++str; } + } else if (needn) { + goto end; + } else { + ++str; } continue; + } } ++str; } end: + nlines = nl; if (str.empty() || (*str != '\"')) { throw CsErrorException( cs, "unfinished string '%s'", ostd::slice_until(orig, str)