aboutsummaryrefslogtreecommitdiff
path: root/libbutl/regex.cxx
blob: 83e296cfec076e9e8f4c48fbdbfa5a8d1d8a361f (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
// file      : libbutl/regex.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/regex.mxx>
#endif

// C includes.

#ifndef __cpp_lib_modules_ts
#include <regex>
#include <string>

#include <ostream>
#include <sstream>
#include <stdexcept> // runtime_error
#if defined(_MSC_VER) && _MSC_VER < 2000
#  include <cstring> // strstr()
#endif
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.regex;

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

import butl.utility; // operator<<(ostream, exception)
#else
#include <libbutl/utility.mxx>
#endif

namespace std
{
  // Currently libstdc++ just returns the name of the exception (bug #67361).
  // So we check that the description contains at least one space character.
  //
  // While VC's description is meaningful, it has an undesired prefix that
  // resembles the following: 'regex_error(error_badrepeat): '. So we skip it.
  //
  ostream&
  operator<< (ostream& o, const regex_error& e)
  {
    const char* d (e.what ());

#if defined(_MSC_VER) && _MSC_VER < 2000
    // Note: run the regex test like this to check new VC version:
    //
    // ./driver.exe a '{' b
    //
    const char* rd (strstr (d, "): "));
    if (rd != nullptr)
      d = rd + 3;
#endif

    ostringstream os;
    os << runtime_error (d); // Sanitize the description.

    string s (os.str ());
    if (s.find (' ') != string::npos)
      o << ": " << s;

    return o;
  }
}