forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlugify.cpp
33 lines (29 loc) · 952 Bytes
/
Slugify.cpp
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
/*
* Copyright (c) 2023, Gurkirat Singh <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/CharacterTypes.h>
#include <AK/Slugify.h>
#include <AK/StringView.h>
namespace AK {
ErrorOr<String> slugify(String const& input, char const glue)
{
StringBuilder sb;
bool just_processed_space = false;
for (auto const& code_point : input.code_points()) {
if (is_ascii_alphanumeric(code_point)) {
sb.append_code_point(to_ascii_lowercase(code_point));
just_processed_space = false;
} else if ((code_point == static_cast<u32>(glue) || is_ascii_space(code_point)) && !just_processed_space) {
sb.append_code_point(glue);
just_processed_space = true;
}
}
auto output = TRY(sb.to_string());
if (output.ends_with(static_cast<u32>(glue))) {
return output.trim(StringView { &glue, 1 }, TrimMode::Right);
}
return output;
}
}