AK: Add BOM handling to String::from_utf8_with_replacement_character

This commit is contained in:
Shannon Booth 2024-08-11 15:19:47 +12:00 committed by Tim Flynn
commit b3bf5c4ea8
Notes: github-actions[bot] 2024-08-12 10:39:57 +00:00
4 changed files with 21 additions and 7 deletions

View file

@ -20,8 +20,11 @@
namespace AK {
String String::from_utf8_with_replacement_character(StringView view)
String String::from_utf8_with_replacement_character(StringView view, WithBOMHandling with_bom_handling)
{
if (auto bytes = view.bytes(); with_bom_handling == WithBOMHandling::Yes && bytes.size() >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF)
view = view.substring_view(3);
if (Utf8View(view).validate())
return String::from_utf8_without_validation(view.bytes());