/src/serenity/AK/Slugify.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2023, Gurkirat Singh <tbhaxor@gmail.com> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <AK/CharacterTypes.h> |
8 | | #include <AK/Slugify.h> |
9 | | #include <AK/StringView.h> |
10 | | |
11 | | namespace AK { |
12 | | ErrorOr<String> slugify(String const& input, char const glue) |
13 | 0 | { |
14 | 0 | StringBuilder sb; |
15 | 0 | bool just_processed_space = false; |
16 | |
|
17 | 0 | for (auto const& code_point : input.code_points()) { |
18 | 0 | if (is_ascii_alphanumeric(code_point)) { |
19 | 0 | sb.append_code_point(to_ascii_lowercase(code_point)); |
20 | 0 | just_processed_space = false; |
21 | 0 | } else if ((code_point == static_cast<u32>(glue) || is_ascii_space(code_point)) && !just_processed_space) { |
22 | 0 | sb.append_code_point(glue); |
23 | 0 | just_processed_space = true; |
24 | 0 | } |
25 | 0 | } |
26 | |
|
27 | 0 | auto output = TRY(sb.to_string()); |
28 | 0 | if (output.ends_with(static_cast<u32>(glue))) { |
29 | 0 | return output.trim(StringView { &glue, 1 }, TrimMode::Right); |
30 | 0 | } |
31 | 0 | return output; |
32 | 0 | } |
33 | | } |