-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathslugify.cpp
More file actions
38 lines (35 loc) · 1.14 KB
/
slugify.cpp
File metadata and controls
38 lines (35 loc) · 1.14 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
#include "slugify.hpp"
#include <memory>
#include <string>
#include <userver/formats/json/value.hpp>
#include "errors.hpp"
#include "fmt/core.h"
#include "unicode/translit.h"
namespace real_medium::utils::slug {
std::string Slugify(const std::string& str) {
UErrorCode status{U_ZERO_ERROR};
UParseError parse_error{};
const auto transliterator = std::unique_ptr<icu::Transliterator>{icu::Transliterator::createFromRules(
"Slugify",
":: Any-Latin;"
":: [:Nonspacing Mark:] Remove;"
":: [:Punctuation:] Remove;"
":: [:Symbol:] Remove;"
":: Latin-ASCII;"
":: Lower();"
"' ' {' '} > ;"
"::NULL;"
"[:Separator:] > '-'",
UTRANS_FORWARD,
parse_error,
status
)};
if (status != U_ZERO_ERROR) {
throw std::runtime_error(fmt::format("icu::Transliterator::createFromRules failed with status {}", static_cast<int32_t>(status)));
}
auto unicode_str = icu::UnicodeString::fromUTF8(str);
transliterator->transliterate(unicode_str);
std::string slug;
return unicode_str.toUTF8String(slug);
}
} // namespace real_medium::utils::slug