aboutsummaryrefslogtreecommitdiff
path: root/libbutl/string-parser.cxx
blob: 5d5ec47e83d04063d56858663fe71feade0dfef9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// file      : libbutl/string-parser.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/string-parser.mxx>
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <string>
#include <vector>
#include <cstddef>
#include <utility>   // move()
#include <stdexcept>
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.string_parser;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
#endif
#endif

#endif

using namespace std;

namespace butl
{
  namespace string_parser
  {
    // Utility functions.
    //
    inline static bool
    space (char c) noexcept
    {
      return c == ' ' || c == '\t';
    }

    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.

        if (i == e) // No more strings.
          break;

        string s;
        char quoting ('\0'); // Current quoting mode, can be used as bool.
        size_t pos (i - b);  // String position.

        for (; i != e; ++i)
        {
          char c (*i);

          if (!quoting)
          {
            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;
          }

          s += c;
        }

        if (quoting)
          throw invalid_string (i - b, "unterminated quoted string");

        r.emplace_back (move (s), pos);
      }

      return r;
    }

    vector<string>
    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));

      return r;
    }

    string
    unquote (const string& s)
    {
      string r;
      char quoting ('\0'); // Current quoting mode, can be used as bool.

      for (auto i (s.begin ()), e (s.end ()); i != e; ++i)
      {
        char c (*i);

        if (!quoting)
        {
          if (c == '"' || c == '\'') // Begin of quoted substring.
          {
            quoting = c;
            continue;
          }
        }
        else if (c == quoting)       // End of quoted substring.
        {
          quoting = '\0';
          continue;
        }

        r += c;
      }

      return r;
    }

    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;
    }
  }
}