Skip to content

Commit 2da9760

Browse files
committed
Improve remove special character function
1 parent 9c63dd6 commit 2da9760

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/Util/TextUtil.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <algorithm>
66
#include <cctype>
7+
#include <ranges>
78
#include <string>
89
#include <vector>
910

@@ -44,14 +45,10 @@ namespace BetterSongSearch::Util {
4445
* Removes special characters from a string
4546
*/
4647
std::string removeSpecialCharacter(std::string_view const s) {
47-
std::string stringy(s);
48-
for (int i = 0; i < stringy.size(); i++) {
49-
if (stringy[i] != ' ' && (stringy[i] < 'A' || stringy[i] > 'Z') && (stringy[i] < 'a' || stringy[i] > 'z') &&
50-
(stringy[i] < '0' || stringy[i] > '9')) {
51-
stringy.erase(i, 1);
52-
i--;
53-
}
54-
}
48+
auto filteredView = s | std::views::filter([](char c) {
49+
return std::isalnum(c) || c == ' ';
50+
});
51+
std::string stringy(filteredView.begin(), filteredView.end());
5552
return stringy;
5653
}
5754

0 commit comments

Comments
 (0)