forked from steaphangreene/acidmud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.cpp
More file actions
135 lines (122 loc) · 4.46 KB
/
utils.cpp
File metadata and controls
135 lines (122 loc) · 4.46 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// *************************************************************************
// This file is part of AcidMUD by Steaphan Greene
//
// Copyright 1999-2022 Steaphan Greene <steaphan@gmail.com>
//
// AcidMUD is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// AcidMUD is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with AcidMUD (see the file named "COPYING");
// If not, see <http://www.gnu.org/licenses/>.
//
// *************************************************************************
#include <algorithm>
#include <string>
// Replace with C++20 std::format, when widely available
#define FMT_HEADER_ONLY
#include <fmt/format.h>
#include "color.hpp"
#include "utils.hpp"
void replace_all(
std::u8string& str,
const std::u8string& oldt,
const std::u8string& newt,
size_t st) {
size_t loc = str.find(oldt, st);
while (loc != std::u8string::npos) {
str.replace(loc, oldt.length(), newt);
loc = str.find(oldt, loc + newt.length());
}
}
void replace_all(std::u8string& str, const std::u8string& oldt, int newn, size_t st) {
replace_all(str, oldt, fmt::format(u8"{}", newn), st);
}
void trim_string(std::u8string& str) { // Remove extra whitespace from std::u8string
size_t b = str.find_first_not_of(u8" \n\r\t\f\v");
size_t e = str.find_last_not_of(u8" \n\r\t\f\v");
if (b == std::u8string::npos || e == std::u8string::npos || b > e) { // No (valid) string
str.clear();
} else if (b != 0 || e + 1 != str.length()) { // String needs trimming
str = str.substr(b, (e - b) + 1);
}
}
void trim_string(std::u8string_view& str) { // Remove extra whitespace from std::u8string
size_t b = str.find_first_not_of(u8" \n\r\t\f\v");
size_t e = str.find_last_not_of(u8" \n\r\t\f\v");
if (b == std::u8string::npos || e == std::u8string::npos || b > e) { // No (valid) string
str = str.substr(0, 0);
} else if (b != 0 || e + 1 != str.length()) { // String needs trimming
str = str.substr(b, (e - b) + 1);
}
}
size_t skip_line(const std::u8string_view& str, size_t pos) {
pos = str.find_first_of(u8"\n\r", pos + 1);
while (pos != std::u8string::npos && pos < str.length() && isspace(str[pos]))
++pos;
if (pos >= str.length())
pos = std::u8string::npos;
return pos;
}
size_t prev_line(const std::u8string_view& str, size_t pos) {
pos = str.find_last_of(u8"\n\r", pos - 1);
while (pos != std::u8string::npos && pos > 0 && isspace(str[pos]))
--pos;
if (pos != std::u8string::npos)
pos = str.find_last_of(u8"\n\r", pos);
while (pos != std::u8string::npos && pos < str.length() && isspace(str[pos]))
++pos;
if (pos >= str.length())
pos = std::u8string::npos;
return pos;
}
static bool phrase_match_sensitive(
const std::u8string_view& str,
const std::u8string_view& phrase) {
auto desc = str;
while (desc.length() >= phrase.length()) {
if (desc.starts_with(phrase) &&
(desc.length() == phrase.length() || !ascii_isalnum(desc.at(phrase.length())))) {
return true;
}
auto off = std::ranges::find_if_not(desc, ascii_isalnum);
if (off == desc.end()) {
return false;
}
off = std::find_if(off, desc.end(), ascii_isalnum);
if (off == desc.end()) {
return false;
}
desc = desc.substr(off - desc.begin());
}
return false;
}
bool phrase_match(const std::u8string_view& str, const std::u8string_view& phrase) {
if (phrase.length() == 0)
return false;
if (std::ranges::any_of(str, ascii_isupper)) {
std::u8string str2(str);
std::ranges::transform(str2, str2.begin(), ascii_tolower);
return phrase_match_sensitive(str2, phrase);
} else {
return phrase_match_sensitive(str, phrase);
}
}
bool words_match(const std::u8string_view& str, const std::u8string_view& words) {
auto start = std::ranges::find_if(words, ascii_isalpha);
while (start != words.end()) {
auto end = std::find_if_not(start, words.end(), ascii_isalnum);
if (phrase_match(str, words.substr(start - words.begin(), end - start))) {
return true;
}
start = std::find_if(end, words.end(), ascii_isalpha);
}
return false;
}