• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libs/core/util/src/regex_from_pattern.cpp
1
//  Copyright (c) 2007-2025 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/config.hpp>
8
#include <hpx/modules/errors.hpp>
9
#include <hpx/util/regex_from_pattern.hpp>
10

11
#include <string>
12

13
#include <hpx/config/warnings_prefix.hpp>
14

15
namespace hpx::util {
16

17
    namespace detail {
18

×
19
        ///////////////////////////////////////////////////////////////////////
20
        [[nodiscard]] inline std::string regex_from_character_set(
21
            std::string::const_iterator& it,
22
            std::string::const_iterator const& end, error_code& ec)
×
23
        {
×
24
            std::string::const_iterator const start = it;
×
25
            std::string result(1, *it);    // copy '['
26
            if (++it != end)
×
27
            {
28
                if (*it == '!')
29
                {
30
                    result.append(1, '^');    // negated character set
×
31
                }
32
                else if (*it == ']')
×
33
                {
34
                    HPX_THROWS_IF(ec, hpx::error::bad_parameter,
35
                        "regex_from_character_set",
36
                        "Invalid pattern (empty character set) at: {}",
×
37
                        std::string(start, end));
38
                    return "";
39
                }
40
                else
×
41
                {
42
                    result.append(1, *it);    // append this character
43
                }
44
            }
45

×
46
            // copy while in character set
47
            while (++it != end)
×
48
            {
×
49
                result.append(1, *it);
50
                if (*it == ']')
51
                    break;
52
            }
×
53

54
            if (it == end || *it != ']')
×
55
            {
56
                HPX_THROWS_IF(ec, hpx::error::bad_parameter,
57
                    "regex_from_character_set",
58
                    "Invalid pattern (missing closing ']') at: {}",
×
59
                    std::string(start, end));
60
                return "";
61
            }
×
62

63
            return result;
64
        }
65
    }    // namespace detail
×
66

67
    std::string regex_from_pattern(std::string const& pattern, error_code& ec)
68
    {
×
69
        std::string result;
×
70
        auto const end = pattern.end();
71
        for (auto it = pattern.begin(); it != end; ++it)
×
72
        {
73
            switch (char const c = *it)
×
74
            {
×
75
            case '*':
76
                result.append(".*");
77
                break;
78

79
            case '?':
80
                result.append(1, '.');
81
                break;
×
82

83
            case '[':
×
84
            {
×
85
                std::string r = detail::regex_from_character_set(it, end, ec);
×
86
                if (ec)
87
                    return "";
88
                result.append(r);
89
            }
90
            break;
91

×
92
            case '\\':
93
                if (++it == end)
×
94
                {
95
                    HPX_THROWS_IF(ec, hpx::error::bad_parameter,
96
                        "regex_from_pattern", "Invalid escape sequence at: {}",
×
97
                        pattern);
98
                    return "";
×
99
                }
100
                result.append(1, *it);
101
                break;
102

103
            // escape regex special characters
×
104
            // NOLINTNEXTLINE(bugprone-branch-clone)
105
            case '+':
106
                [[fallthrough]];
107
            case '.':
108
                [[fallthrough]];
109
            case '(':
110
                [[fallthrough]];
111
            case ')':
112
                [[fallthrough]];
113
            case '{':
114
                [[fallthrough]];
115
            case '}':
116
                [[fallthrough]];
117
            case '^':
118
                [[fallthrough]];
×
119
            case '$':
120
                result.append("\\");
121
                [[fallthrough]];
×
122

123
            default:
124
                result.append(1, c);
125
                break;
126
            }
×
127
        }
128
        return result;
129
    }
130
}    // namespace hpx::util
131

132
#include <hpx/config/warnings_suffix.hpp>
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc