Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OrcLib: Text: add enum class Encoding
- Loading branch information
1 parent
15ea817
commit 1803a40
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2021 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl | ||
// | ||
|
||
#include "Encoding.h" | ||
|
||
#include <string> | ||
|
||
namespace Orc { | ||
|
||
constexpr std::wstring_view kUtf8 = L"utf-8"; | ||
constexpr std::wstring_view kUtf16 = L"utf-16"; | ||
constexpr std::wstring_view kUnknown = L"unknown"; | ||
|
||
std::wstring_view ToString(Text::Encoding encoding) | ||
{ | ||
switch (encoding) | ||
{ | ||
case Text::Encoding::Utf8: | ||
return kUtf8; | ||
case Text::Encoding::Utf16: | ||
return kUtf16; | ||
default: | ||
return kUnknown; | ||
} | ||
} | ||
|
||
Text::Encoding ToEncoding(const std::wstring& disposition, std::error_code& ec) | ||
{ | ||
const std::map<std::wstring_view, Text::Encoding> map = { | ||
{kUtf8, Text::Encoding::Utf8}, {kUtf16, Text::Encoding::Utf16}}; | ||
|
||
auto it = map.find(disposition); | ||
if (it == std::cend(map)) | ||
{ | ||
ec = std::make_error_code(std::errc::invalid_argument); | ||
return Text::Encoding::Unknown; | ||
} | ||
|
||
return it->second; | ||
} | ||
|
||
} // namespace Orc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// SPDX-License-Identifier: LGPL-2.1-or-later | ||
// | ||
// Copyright © 2021 ANSSI. All Rights Reserved. | ||
// | ||
// Author(s): fabienfl | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string_view> | ||
#include <string> | ||
|
||
namespace Orc { | ||
namespace Text { | ||
|
||
enum class Encoding | ||
{ | ||
Unknown, | ||
Utf8, | ||
Utf16, | ||
}; | ||
|
||
} // namespace Text | ||
|
||
std::wstring_view ToString(Text::Encoding encoding); | ||
Text::Encoding ToEncoding(const std::wstring& encoding, std::error_code& ec = std::error_code()); | ||
|
||
} // namespace Orc |