aboutsummaryrefslogtreecommitdiff
path: root/libbutl/string-parser.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2017-09-22 23:32:28 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2017-09-22 23:32:28 +0200
commitc09cd7512491cee1e82c1ad8128ce9fd4bc3f79b (patch)
treea659ed768d849130ab5780a11b7f791a463a1a91 /libbutl/string-parser.cxx
parent2a00871f07067f8f9e2de08bb9c8f50e1bf6a650 (diff)
Initial modularization with both Clang and VC hacks
Note: gave up on VC about half way though.
Diffstat (limited to 'libbutl/string-parser.cxx')
-rw-r--r--libbutl/string-parser.cxx193
1 files changed, 109 insertions, 84 deletions
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 <libbutl/string-parser.hxx>
+#ifndef __cpp_modules
+#include <libbutl/string-parser.mxx>
+#endif
-#include <utility> // move()
+// C includes.
+
+#ifndef __cpp_lib_modules
+#include <string>
+#include <vector>
+#include <cstddef>
+#include <utility> // move()
+#include <stdexcept>
+#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<pair<string, size_t>> string_parser::
- parse_quoted_position (const string& s, bool unquote)
+ namespace string_parser
{
- vector<pair<string, size_t>> 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<pair<string, size_t>>
+ parse_quoted_position (const string& s, bool unquote)
+ {
+ vector<pair<string, size_t>> 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> string_parser::
- parse_quoted (const string& s, bool unquote)
- {
- vector<pair<string, size_t>> sp (parse_quoted_position (s, unquote));
-
- vector<string> r;
- r.reserve (sp.size ());
- for (auto& s: sp)
- r.emplace_back (move (s.first));
+ vector<string>
+ parse_quoted (const string& s, bool unquote)
+ {
+ vector<pair<string, size_t>> sp (parse_quoted_position (s, unquote));
- return r;
- }
+ vector<string> 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> string_parser::
- unquote (const vector<string>& v)
- {
- vector<string> r;
- r.reserve (v.size ());
- for (auto& s: v)
- r.emplace_back (unquote (s));
+ vector<string>
+ unquote (const vector<string>& v)
+ {
+ vector<string> r;
+ r.reserve (v.size ());
+ for (auto& s: v)
+ r.emplace_back (unquote (s));
- return r;
+ return r;
+ }
}
}