Skip to content

Commit cccf56a

Browse files
committed
Make the replace_all stuff work for c++11
1 parent 33ef237 commit cccf56a

File tree

3 files changed

+24
-22
lines changed

3 files changed

+24
-22
lines changed

src/formatting.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,21 @@ namespace cpptrace {
3333
// this brings it in-line with other compilers and prevents any tokenization/highlighting issues
3434
static const std::regex msvc_anonymous_namespace("`anonymous namespace'");
3535
detail::replace_all(symbol, msvc_anonymous_namespace, "(anonymous namespace)");
36-
// rules to replace std::basic_string -> std::string and std::basic_string_view -> std::string_view
36+
// rules to replace std::basic_string -> std::string and std::basic_string_view -> std::string
3737
// rule to replace ", std::allocator<whatever>"
38-
static const std::pair<std::regex, std::string_view> basic_string = {
38+
static const std::pair<std::regex, std::string> basic_string = {
3939
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string<char)"), "std::string"
4040
};
4141
detail::replace_all_template(symbol, basic_string);
42-
static const std::pair<std::regex, std::string_view> basic_string_view = {
42+
static const std::pair<std::regex, std::string> basic_string_view = {
4343
std::regex(R"(std(::[a-zA-Z0-9_]+)?::basic_string_view<char)"), "std::string_view"
4444
};
4545
detail::replace_all_template(symbol, basic_string_view);
46-
static const std::pair<std::regex, std::string_view> allocator = {
46+
static const std::pair<std::regex, std::string> allocator = {
4747
std::regex(R"(,\s*std(::[a-zA-Z0-9_]+)?::allocator<)"), ""
4848
};
4949
detail::replace_all_template(symbol, allocator);
50-
static const std::pair<std::regex, std::string_view> default_delete = {
50+
static const std::pair<std::regex, std::string> default_delete = {
5151
std::regex(R"(,\s*std(::[a-zA-Z0-9_]+)?::default_delete<)"), ""
5252
};
5353
detail::replace_all_template(symbol, default_delete);

src/utils/replace_all.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@
22

33
namespace cpptrace {
44
namespace detail {
5-
void replace_all(std::string& str, std::string_view substr, std::string_view replacement) {
5+
void replace_all(std::string& str, string_view substr, string_view replacement) {
66
std::string::size_type pos = 0;
7-
while((pos = str.find(substr.data(), pos, substr.length())) != std::string::npos) {
8-
str.replace(pos, substr.length(), replacement.data(), replacement.length());
9-
pos += replacement.length();
7+
while((pos = str.find(substr.data(), pos, substr.size())) != std::string::npos) {
8+
str.replace(pos, substr.size(), replacement.data(), replacement.size());
9+
pos += replacement.size();
1010
}
1111
}
1212

13-
void replace_all(std::string& str, const std::regex& re, std::string_view replacement) {
13+
void replace_all(std::string& str, const std::regex& re, string_view replacement) {
1414
std::smatch match;
1515
std::size_t i = 0;
1616
while(std::regex_search(str.cbegin() + i, str.cend(), match, re)) {
17-
str.replace(i + match.position(), match.length(), replacement);
18-
i += match.position() + replacement.length();
17+
str.replace(i + match.position(), match.length(), replacement.data(), replacement.size());
18+
i += match.position() + replacement.size();
1919
}
2020
}
2121

22-
void replace_all_dynamic(std::string& str, std::string_view substr, std::string_view replacement) {
22+
void replace_all_dynamic(std::string& str, string_view substr, string_view replacement) {
2323
std::string::size_type pos = 0;
24-
while((pos = str.find(substr.data(), pos, substr.length())) != std::string::npos) {
25-
str.replace(pos, substr.length(), replacement.data(), replacement.length());
24+
while((pos = str.find(substr.data(), pos, substr.size())) != std::string::npos) {
25+
str.replace(pos, substr.size(), replacement.data(), replacement.size());
2626
// advancing by one rather than replacement.length() in case replacement leads to
2727
// another replacement opportunity, e.g. folding > > > to >> > then >>>
2828
pos++;
2929
}
3030
}
3131

32-
void replace_all_template(std::string& str, const std::pair<std::regex, std::string_view>& rule) {
32+
void replace_all_template(std::string& str, const std::pair<std::regex, string_view>& rule) {
3333
const auto& [re, replacement] = rule;
3434
std::smatch match;
3535
std::size_t cursor = 0;
@@ -45,8 +45,8 @@ namespace detail {
4545
}
4646
}
4747
// make the replacement
48-
str.replace(match_begin, end - match_begin, replacement);
49-
cursor = match_begin + replacement.length();
48+
str.replace(match_begin, end - match_begin, replacement.data(), replacement.size());
49+
cursor = match_begin + replacement.size();
5050
}
5151
}
5252
}

src/utils/replace_all.hpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
#include <string>
55
#include <regex>
66

7+
#include "utils/string_view.hpp"
8+
79
namespace cpptrace {
810
namespace detail {
911
// replace all instances of substr with the replacement
10-
void replace_all(std::string& str, std::string_view substr, std::string_view replacement);
12+
void replace_all(std::string& str, string_view substr, string_view replacement);
1113

1214
// replace all regex matches with the replacement
13-
void replace_all(std::string& str, const std::regex& re, std::string_view replacement);
15+
void replace_all(std::string& str, const std::regex& re, string_view replacement);
1416

1517
// replace all instances of substr with the replacement, including new instances introduced by the replacement
16-
void replace_all_dynamic(std::string& str, std::string_view substr, std::string_view replacement);
18+
void replace_all_dynamic(std::string& str, string_view substr, string_view replacement);
1719

1820
// replace all matches of a regex including template parameters
19-
void replace_all_template(std::string& str, const std::pair<std::regex, std::string_view>& rule);
21+
void replace_all_template(std::string& str, const std::pair<std::regex, string_view>& rule);
2022
}
2123
}
2224

0 commit comments

Comments
 (0)