From c09cd7512491cee1e82c1ad8128ce9fd4bc3f79b Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 22 Sep 2017 23:32:28 +0200 Subject: Initial modularization with both Clang and VC hacks Note: gave up on VC about half way though. --- libbutl/string-parser.cxx | 193 ++++++++++++++++++++++++++-------------------- 1 file changed, 109 insertions(+), 84 deletions(-) (limited to 'libbutl/string-parser.cxx') diff --git a/libbutl/string-parser.cxx b/libbutl/string-parser.cxx index c579db0..53c1d1a 100644 --- a/libbutl/string-parser.cxx +++ b/libbutl/string-parser.cxx @@ -2,131 +2,156 @@ // copyright : Copyright (c) 2014-2017 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#include +#ifndef __cpp_modules +#include +#endif -#include // move() +// C includes. + +#ifndef __cpp_lib_modules +#include +#include +#include +#include // move() +#include +#endif + +// Other includes. + +#ifdef __cpp_modules +module butl.string_parser; + +// Only imports additional to interface. +#ifdef __clang__ +#ifdef __cpp_lib_modules +import std.core; +#endif +#endif + +#endif using namespace std; namespace butl { - // Utility functions - // - inline static bool - space (char c) noexcept - { - return c == ' ' || c == '\t'; - } - - // string_parser - // - vector> string_parser:: - parse_quoted_position (const string& s, bool unquote) + namespace string_parser { - vector> r; - for (auto b (s.begin ()), i (b), e (s.end ()); i != e; ) + // Utility functions. + // + inline static bool + space (char c) noexcept { - for (; i != e && space (*i); ++i) ; // Skip spaces. + return c == ' ' || c == '\t'; + } - if (i == e) // No more strings. - break; + vector> + parse_quoted_position (const string& s, bool unquote) + { + vector> r; + for (auto b (s.begin ()), i (b), e (s.end ()); i != e; ) + { + for (; i != e && space (*i); ++i) ; // Skip spaces. - string s; - char quoting ('\0'); // Current quoting mode, can be used as bool. - size_t pos (i - b); // String position. + if (i == e) // No more strings. + break; - for (; i != e; ++i) - { - char c (*i); + string s; + char quoting ('\0'); // Current quoting mode, can be used as bool. + size_t pos (i - b); // String position. - if (!quoting) + for (; i != e; ++i) { - if (space (c)) // End of string. - break; + char c (*i); - if (c == '"' || c == '\'') // Begin of quoted substring. + if (!quoting) { - quoting = c; + if (space (c)) // End of string. + break; + + if (c == '"' || c == '\'') // Begin of quoted substring. + { + quoting = c; + + if (!unquote) + s += c; + + continue; + } + } + else if (c == quoting) // End of quoted substring. + { + quoting = '\0'; if (!unquote) s += c; continue; } - } - else if (c == quoting) // End of quoted substring. - { - quoting = '\0'; - - if (!unquote) - s += c; - continue; + s += c; } - s += c; - } + if (quoting) + throw invalid_string (i - b, "unterminated quoted string"); - if (quoting) - throw invalid_string (i - b, "unterminated quoted string"); + r.emplace_back (move (s), pos); + } - r.emplace_back (move (s), pos); + return r; } - return r; - } - - vector string_parser:: - parse_quoted (const string& s, bool unquote) - { - vector> sp (parse_quoted_position (s, unquote)); - - vector r; - r.reserve (sp.size ()); - for (auto& s: sp) - r.emplace_back (move (s.first)); + vector + parse_quoted (const string& s, bool unquote) + { + vector> sp (parse_quoted_position (s, unquote)); - return r; - } + vector r; + r.reserve (sp.size ()); + for (auto& s: sp) + r.emplace_back (move (s.first)); - string string_parser:: - unquote (const string& s) - { - string r; - char quoting ('\0'); // Current quoting mode, can be used as bool. + return r; + } - for (auto i (s.begin ()), e (s.end ()); i != e; ++i) + string + unquote (const string& s) { - char c (*i); + string r; + char quoting ('\0'); // Current quoting mode, can be used as bool. - if (!quoting) + for (auto i (s.begin ()), e (s.end ()); i != e; ++i) { - if (c == '"' || c == '\'') // Begin of quoted substring. + char c (*i); + + if (!quoting) { - quoting = c; + if (c == '"' || c == '\'') // Begin of quoted substring. + { + quoting = c; + continue; + } + } + else if (c == quoting) // End of quoted substring. + { + quoting = '\0'; continue; } - } - else if (c == quoting) // End of quoted substring. - { - quoting = '\0'; - continue; + + r += c; } - r += c; + return r; } - return r; - } - - vector string_parser:: - unquote (const vector& v) - { - vector r; - r.reserve (v.size ()); - for (auto& s: v) - r.emplace_back (unquote (s)); + vector + unquote (const vector& v) + { + vector r; + r.reserve (v.size ()); + for (auto& s: v) + r.emplace_back (unquote (s)); - return r; + return r; + } } } -- cgit v1.1