mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 02:47:34 +00:00
LibTextCodec: Add a simple UTF-16BE decoder
This commit is contained in:
parent
01e00bac9d
commit
0538dc4e28
2 changed files with 26 additions and 1 deletions
|
@ -47,6 +47,14 @@ UTF8Decoder& utf8_decoder()
|
||||||
return *decoder;
|
return *decoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UTF16BEDecoder& utf16be_decoder()
|
||||||
|
{
|
||||||
|
static UTF16BEDecoder* decoder;
|
||||||
|
if (!decoder)
|
||||||
|
decoder = new UTF16BEDecoder;
|
||||||
|
return *decoder;
|
||||||
|
}
|
||||||
|
|
||||||
Latin2Decoder& latin2_decoder()
|
Latin2Decoder& latin2_decoder()
|
||||||
{
|
{
|
||||||
static Latin2Decoder* decoder = nullptr;
|
static Latin2Decoder* decoder = nullptr;
|
||||||
|
@ -64,6 +72,8 @@ Decoder* decoder_for(const String& a_encoding)
|
||||||
return &latin1_decoder();
|
return &latin1_decoder();
|
||||||
if (encoding.equals_ignoring_case("utf-8"))
|
if (encoding.equals_ignoring_case("utf-8"))
|
||||||
return &utf8_decoder();
|
return &utf8_decoder();
|
||||||
|
if (encoding.equals_ignoring_case("utf-16be"))
|
||||||
|
return &utf16be_decoder();
|
||||||
if (encoding.equals_ignoring_case("iso-8859-2"))
|
if (encoding.equals_ignoring_case("iso-8859-2"))
|
||||||
return &latin2_decoder();
|
return &latin2_decoder();
|
||||||
dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
|
dbgln("TextCodec: No decoder implemented for encoding '{}'", a_encoding);
|
||||||
|
@ -170,6 +180,16 @@ String UTF8Decoder::to_utf8(const StringView& input)
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String UTF16BEDecoder::to_utf8(const StringView& input)
|
||||||
|
{
|
||||||
|
StringBuilder builder(input.length() / 2);
|
||||||
|
for (size_t i = 0; i < input.length(); i += 2) {
|
||||||
|
u16 code_point = (input[i] << 8) | input[i + 1];
|
||||||
|
builder.append_code_point(code_point);
|
||||||
|
}
|
||||||
|
return builder.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
String Latin1Decoder::to_utf8(const StringView& input)
|
String Latin1Decoder::to_utf8(const StringView& input)
|
||||||
{
|
{
|
||||||
StringBuilder builder(input.length());
|
StringBuilder builder(input.length());
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -40,6 +40,11 @@ public:
|
||||||
virtual String to_utf8(const StringView&) override;
|
virtual String to_utf8(const StringView&) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class UTF16BEDecoder final : public Decoder {
|
||||||
|
public:
|
||||||
|
virtual String to_utf8(const StringView&) override;
|
||||||
|
};
|
||||||
|
|
||||||
class Latin1Decoder final : public Decoder {
|
class Latin1Decoder final : public Decoder {
|
||||||
public:
|
public:
|
||||||
virtual String to_utf8(const StringView&) override;
|
virtual String to_utf8(const StringView&) override;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue