• 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/sed_transform.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
//  Copyright (c) 2011 Bryce Adelstein-Lelbach
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7
////////////////////////////////////////////////////////////////////////////////
8

9
#include <hpx/util/sed_transform.hpp>
10

11
#include <memory>
12
#include <regex>
13
#include <string>
14

15
namespace hpx::util {
16

17
    bool parse_sed_expression(
×
18
        std::string const& input, std::string& search, std::string& replace)
19
    {
20
        if (input.size() <= 2)
×
21
            return false;
22

23
        // s/search/replace/
24
        // ^^
25
        if ('s' != input.at(0) || '/' != input.at(1))
×
26
            return false;
27

28
        constexpr std::string::size_type search_begin = 2;
29
        std::string::size_type search_end = search_begin;
30

31
        // s/search/replace/
32
        //   ^^^^^^
33
        while (true)
34
        {
35
            // See if we're at the end of the string.
36
            if ((input.size() - 1) < search_end)
×
37
                return false;
38

39
            // If this is an escape, then don't check the next character.
40
            if ('\\' == input.at(search_end++))
×
41
            {
42
                // See if we're at the end of the string.
43
                if ((input.size() - 1) < ++search_end)
×
44
                    return false;
45
            }
46

47
            if ('/' == input.at(search_end))
×
48
            {
49
                --search_end;
50
                break;
51
            }
52
        }
53

54
        // s/search/replace/
55
        //         ^
56
        if ('/' != input.at(search_end + 1))
57
            return false;
58

59
        std::string::size_type const replace_begin = search_end + 2;
×
60
        std::string::size_type replace_end = input.size() - 1;
61

62
        // s/search/replace/
63
        //                 ^ (optional)
64
        if ('/' == input.at(replace_end))
×
65
            --replace_end;
×
66

67
        search = input.substr(search_begin, (search_end - search_begin) + 1);
×
68
        replace =
69
            input.substr(replace_begin, (replace_end - replace_begin) + 1);
×
70

71
        return true;
×
72
    }
73

74
    struct sed_transform::command
×
75
    {
76
        command(std::string const& search, std::string replace)
77
          : search_(search)
×
78
          , replace_(HPX_MOVE(replace))
×
79
        {
80
        }
81

82
        std::regex search_;
83
        std::string replace_;
84
    };
85

86
    sed_transform::sed_transform(std::string const& search, std::string replace)
×
87
      : command_(std::make_shared<command>(search, HPX_MOVE(replace)))
88
    {
89
    }
×
90

91
    sed_transform::sed_transform(std::string const& expression)
×
92
    {
93
        std::string search, replace;
94

95
        if (parse_sed_expression(expression, search, replace))
×
96
        {
97
            command_ = std::make_shared<command>(search, HPX_MOVE(replace));
×
98
        }
99
    }
×
100

101
    std::string sed_transform::operator()(std::string const& input) const
×
102
    {
103
        if (!command_)
×
104
            return input;
105

106
        return std::regex_replace(input, command_->search_, command_->replace_,
×
107
            std::regex_constants::match_default |
108
                std::regex_constants::format_sed);
×
109
    }
110
}    // namespace hpx::util
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