mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add generation of roman numerals to AK::String
We now can generate roman numbers using String::roman_number_from() similar to String::bijective_base_from().
This commit is contained in:
parent
24e122590c
commit
87033ce7d1
Notes:
sideshowbarker
2024-07-18 10:27:22 +09:00
Author: https://github.com/TobyAsE Commit: https://github.com/SerenityOS/serenity/commit/87033ce7d1b Pull-request: https://github.com/SerenityOS/serenity/pull/8434 Reviewed-by: https://github.com/MaxWipfli ✅
2 changed files with 54 additions and 0 deletions
|
@ -273,6 +273,59 @@ String String::bijective_base_from(size_t value, unsigned base, StringView map)
|
|||
return String { ReadonlyBytes(buffer.data(), i) };
|
||||
}
|
||||
|
||||
String String::roman_number_from(size_t value)
|
||||
{
|
||||
if (value > 3999)
|
||||
return String::number(value);
|
||||
|
||||
StringBuilder builder;
|
||||
|
||||
while (value > 0) {
|
||||
if (value >= 1000) {
|
||||
builder.append('M');
|
||||
value -= 1000;
|
||||
} else if (value >= 900) {
|
||||
builder.append("CM"sv);
|
||||
value -= 900;
|
||||
} else if (value >= 500) {
|
||||
builder.append('D');
|
||||
value -= 500;
|
||||
} else if (value >= 400) {
|
||||
builder.append("CD"sv);
|
||||
value -= 400;
|
||||
} else if (value >= 100) {
|
||||
builder.append('C');
|
||||
value -= 100;
|
||||
} else if (value >= 90) {
|
||||
builder.append("XC"sv);
|
||||
value -= 90;
|
||||
} else if (value >= 50) {
|
||||
builder.append('L');
|
||||
value -= 50;
|
||||
} else if (value >= 40) {
|
||||
builder.append("XL"sv);
|
||||
value -= 40;
|
||||
} else if (value >= 10) {
|
||||
builder.append('X');
|
||||
value -= 10;
|
||||
} else if (value == 9) {
|
||||
builder.append("IX"sv);
|
||||
value -= 9;
|
||||
} else if (value >= 5 && value <= 8) {
|
||||
builder.append('V');
|
||||
value -= 5;
|
||||
} else if (value == 4) {
|
||||
builder.append("IV"sv);
|
||||
value -= 4;
|
||||
} else if (value <= 3) {
|
||||
builder.append('I');
|
||||
value -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
bool String::matches(const StringView& mask, Vector<MaskSpan>& mask_spans, CaseSensitivity case_sensitivity) const
|
||||
{
|
||||
return StringUtils::matches(*this, mask, case_sensitivity, &mask_spans);
|
||||
|
|
|
@ -99,6 +99,7 @@ public:
|
|||
[[nodiscard]] static String repeated(const StringView&, size_t count);
|
||||
|
||||
[[nodiscard]] static String bijective_base_from(size_t value, unsigned base = 26, StringView map = {});
|
||||
[[nodiscard]] static String roman_number_from(size_t value);
|
||||
|
||||
template<class SeparatorType, class CollectionType>
|
||||
[[nodiscard]] static String join(const SeparatorType& separator, const CollectionType& collection)
|
||||
|
|
Loading…
Reference in a new issue