aboutsummaryrefslogtreecommitdiff
path: root/butl/string-parser
blob: e3fa507d1ef9a9244a3efeb0bed07b920380cfe4 (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
// file      : butl/string-parser -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUTL_STRING_PARSER
#define BUTL_STRING_PARSER

#include <string>
#include <vector>
#include <cstddef>   // size_t
#include <stdexcept> // invalid_argument

#include <butl/export>

namespace butl
{
  class LIBBUTL_EXPORT invalid_string: public std::invalid_argument
  {
  public:
    invalid_string (std::size_t p, const std::string& d)
        : invalid_argument (d), pos (p) {}

    std::size_t pos; // Zero-based.
  };

  class LIBBUTL_EXPORT string_parser
  {
  public:
    // Parse a whitespace-separated list of strings. Can contain single or
    // double quoted substrings. No escaping is supported. If unquote is true,
    // return one-level unquoted values. Throw invalid_string in case of
    // invalid quoting.
    //
    std::vector<string>
    parse_quoted (const string&, bool unquote);

    // Remove a single level of quotes. Note that the format or the
    // correctness of the quotation is not validated.
    //
    std::string
    unquote (const string&);
  };
}

#endif // BUTL_STRING_PARSER