-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstring_utility.cpp
More file actions
63 lines (56 loc) · 1.88 KB
/
string_utility.cpp
File metadata and controls
63 lines (56 loc) · 1.88 KB
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
// *******************************************************************
// DCue (github.com/xavery/dcue)
// Copyright (c) 2019-2022 Daniel Kamil Kozar
// Original version by :
// DCue (sourceforge.net/projects/dcue)
// Copyright (c) 2013 Fluxtion, DCue project
// *******************************************************************
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// *******************************************************************
#include "string_utility.h"
#include <cctype>
namespace {
std::string_view ltrim(std::string_view text) {
std::string::size_type i = 0;
const std::string::size_type text_size = text.size();
while (i < text_size && std::isspace(text[i])) {
++i;
}
return text.substr(i);
}
std::string_view rtrim(std::string_view text) {
std::string::size_type i = text.size();
while (i > 0 && std::isspace(text[i - 1])) {
--i;
}
return text.substr(0, i);
}
}
std::string_view trim(std::string_view text) {
return ltrim(rtrim(text));
}
bool replace_char(std::string& text, const char candidate,
const std::string& new_text) {
const std::string::size_type text_size = text.size();
bool did_change = false;
for (std::string::size_type i = 0; i < text_size; ++i) {
if (text[i] == candidate) {
text.replace(i, 1, new_text);
did_change = true;
}
}
return did_change;
}
bool replace_string(std::string& text, const std::string& candidate,
const std::string& new_text) {
std::string::size_type found = text.find(candidate);
bool did_change = false;
while (found != std::string::npos) {
text.replace(found, candidate.size(), new_text);
did_change = true;
found = text.find(candidate, found + 1);
}
return did_change;
}