diff --git a/Source/Engine/Core/Encoding.h b/Source/Engine/Core/Encoding.h index 6ac716ba8..3e74144e2 100644 --- a/Source/Engine/Core/Encoding.h +++ b/Source/Engine/Core/Encoding.h @@ -4,4 +4,4 @@ #include "Enums.h" -DECLARE_ENUM_3(Encoding, ANSI, Unicode, UnicodeBigEndian); +DECLARE_ENUM_4(Encoding, ANSI, Unicode, UnicodeBigEndian, UTF8); diff --git a/Source/Engine/Platform/Base/FileBase.cpp b/Source/Engine/Platform/Base/FileBase.cpp index f45685c7f..4794e2452 100644 --- a/Source/Engine/Platform/Base/FileBase.cpp +++ b/Source/Engine/Platform/Base/FileBase.cpp @@ -309,6 +309,45 @@ bool FileBase::WriteAllText(const StringView& path, const Char* data, int32 leng return WriteAllBytes(path, tmp.Get(), tmp.Count()); } + case Encoding::UTF8: + { + Array tmp; + tmp.SetCapacity(length); + + for (int i=0; i> 6)); + tmp.Add(0x80 | (c & 0x3F)); + } + else if (c < 0xD800 || c >= 0xE000) + { + tmp.Add(0xE0 | (c >> 12)); + tmp.Add(0x80 | ((c >> 6) & 0x3F)); + tmp.Add(0x80 | (c & 0x3F)); + } + else // Surrogate pair + { + ++i; + if (i >= length) + return true; + + uint32 p = 0x10000 + (((c & 0x3FF) << 10) | (data[i] & 0x3FF)); + tmp.Add(0xF0 | (p >> 18)); + tmp.Add(0x80 | ((p >> 12) & 0x3F)); + tmp.Add(0x80 | ((p >> 6) & 0x3F)); + tmp.Add(0x80 | (p & 0x3F)); + } + } + + return WriteAllBytes(path, tmp.Get(), tmp.Count()); + } default: return true; }