mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:17:45 +00:00
AK: Add StringBuilder::append(Utf32View)
This encodes the incoming UTF-32 sequence as UTF-8.
This commit is contained in:
parent
bc6f469544
commit
86242f9c18
2 changed files with 28 additions and 0 deletions
|
@ -30,6 +30,7 @@
|
||||||
#include <AK/String.h>
|
#include <AK/String.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
|
#include <AK/Utf32View.h>
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
|
@ -112,4 +113,30 @@ void StringBuilder::clear()
|
||||||
m_length = 0;
|
m_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void StringBuilder::append(const Utf32View& utf32_view)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < utf32_view.length(); ++i) {
|
||||||
|
auto codepoint = utf32_view.codepoints()[i];
|
||||||
|
if (codepoint <= 0x7f) {
|
||||||
|
append((char)codepoint);
|
||||||
|
} else if (codepoint <= 0x07ff) {
|
||||||
|
append((char)(((codepoint >> 6) & 0x1f) | 0xc0));
|
||||||
|
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
|
||||||
|
} else if (codepoint <= 0xffff) {
|
||||||
|
append((char)(((codepoint >> 12) & 0x0f) | 0xe0));
|
||||||
|
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
|
||||||
|
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
|
||||||
|
} else if (codepoint <= 0x10ffff) {
|
||||||
|
append((char)(((codepoint >> 18) & 0x07) | 0xf0));
|
||||||
|
append((char)(((codepoint >> 12) & 0x3f) | 0x80));
|
||||||
|
append((char)(((codepoint >> 6) & 0x3f) | 0x80));
|
||||||
|
append((char)(((codepoint >> 0) & 0x3f) | 0x80));
|
||||||
|
} else {
|
||||||
|
append(0xef);
|
||||||
|
append(0xbf);
|
||||||
|
append(0xbd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ public:
|
||||||
~StringBuilder() {}
|
~StringBuilder() {}
|
||||||
|
|
||||||
void append(const StringView&);
|
void append(const StringView&);
|
||||||
|
void append(const Utf32View&);
|
||||||
void append(char);
|
void append(char);
|
||||||
void append(const char*, size_t);
|
void append(const char*, size_t);
|
||||||
void appendf(const char*, ...);
|
void appendf(const char*, ...);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue