-
Notifications
You must be signed in to change notification settings - Fork 10
feat(encoding): name and decode text encodings #665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,67 @@ | ||
| #include <odr/internal/encoding/detect.hpp> | ||
|
|
||
| #include <odr/internal/encoding/text_encoding_table.hpp> | ||
|
|
||
| #include <istream> | ||
| #include <string_view> | ||
|
|
||
| #include <uchardet/uchardet.h> | ||
|
|
||
| namespace odr::internal { | ||
|
|
||
| namespace { | ||
|
|
||
| using namespace std::string_view_literals; | ||
|
|
||
| /// The encoding a byte-order mark names, `unknown` when there is none. | ||
| TextEncoding encoding_by_bom(const std::string_view probe) { | ||
| const auto starts_with = [&](const std::string_view bom) { | ||
| return probe.substr(0, bom.size()) == bom; | ||
| }; | ||
|
|
||
| // utf-32le opens with the utf-16le mark, so it has to be tested first | ||
| if (starts_with("\xff\xfe\x00\x00"sv)) { | ||
| return TextEncoding::utf32le; | ||
| } | ||
| if (starts_with("\x00\x00\xfe\xff"sv)) { | ||
| return TextEncoding::utf32be; | ||
| } | ||
| if (starts_with("\xef\xbb\xbf"sv)) { | ||
| return TextEncoding::utf8; | ||
| } | ||
| if (starts_with("\xff\xfe"sv)) { | ||
| return TextEncoding::utf16le; | ||
| } | ||
| if (starts_with("\xfe\xff"sv)) { | ||
| return TextEncoding::utf16be; | ||
| } | ||
| return TextEncoding::unknown; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::string encoding::read_probe(std::istream &in, | ||
| const std::size_t max_bytes) { | ||
| std::string result(max_bytes, '\0'); | ||
| in.read(result.data(), static_cast<std::streamsize>(max_bytes)); | ||
| result.resize(static_cast<std::size_t>(in.gcount())); | ||
| return result; | ||
| } | ||
|
|
||
| TextEncoding encoding::detect(const std::string_view probe) { | ||
| if (const TextEncoding by_bom = encoding_by_bom(probe); | ||
| by_bom != TextEncoding::unknown) { | ||
| return by_bom; | ||
| } | ||
|
|
||
| auto *const detector = uchardet_new(); | ||
| uchardet_handle_data(detector, probe.data(), probe.size()); | ||
| uchardet_data_end(detector); | ||
| const std::string name = uchardet_get_charset(detector); | ||
| uchardet_delete(detector); | ||
|
|
||
| const auto *row = text_encoding_table::find_by_name(name); | ||
| return row == nullptr ? TextEncoding::unknown : row->encoding; | ||
| } | ||
|
|
||
| } // namespace odr::internal |
This file contains hidden or 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,25 @@ | ||
| #pragma once | ||
|
|
||
| #include <odr/file.hpp> | ||
|
|
||
| #include <cstddef> | ||
| #include <iosfwd> | ||
| #include <string> | ||
| #include <string_view> | ||
|
|
||
| namespace odr::internal::encoding { | ||
|
|
||
| /// Enough for uchardet, and bounded so classifying a large file costs no pass | ||
| /// over it. | ||
| constexpr std::size_t default_probe_size = std::size_t{64} * 1024; | ||
|
|
||
| /// Reads at most @p max_bytes from @p in, from wherever it stands. | ||
| [[nodiscard]] std::string | ||
| read_probe(std::istream &in, std::size_t max_bytes = default_probe_size); | ||
|
|
||
| /// The encoding of @p probe, @ref TextEncoding::unknown if it cannot be named. | ||
| /// A byte-order mark decides alone, else uchardet guesses β from the probe | ||
| /// only, so an encoding invisible in an ASCII prefix is missed. | ||
| [[nodiscard]] TextEncoding detect(std::string_view probe); | ||
|
|
||
| } // namespace odr::internal::encoding |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.