-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Create a EncodingConverter class with both iconv and icu support. #138893
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3192c7b
Create a CharSetConverter class with both iconv and icu support.
abhina-sree 6d40922
address comments
abhina-sree 52635f2
rename CharSetConverter to EncodingConverter
abhina-sree a39b13e
address comments, rename CharSet to EncodingConverter
abhina-sree b32b472
rename filename, class to use TextEncoding, address comments
abhina-sree b9dab1f
address latest comments
abhina-sree 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
//===-- EncodingConverter.h - Encoding conversion class -----------*- C++ -*-=// | ||
abhina-sree marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file provides a utility class to convert between different character | ||
/// set encodings. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_SUPPORT_ENCODING_CONVERTER_H | ||
#define LLVM_SUPPORT_ENCODING_CONVERTER_H | ||
|
||
#include "llvm/ADT/SmallString.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Config/config.h" | ||
#include "llvm/Support/ErrorOr.h" | ||
|
||
#include <string> | ||
#include <system_error> | ||
|
||
namespace llvm { | ||
|
||
template <typename T> class SmallVectorImpl; | ||
|
||
namespace details { | ||
class EncodingConverterImplBase { | ||
|
||
private: | ||
/// Converts a string. | ||
/// \param[in] Source source string | ||
/// \param[out] Result container for converted string | ||
/// \return error code in case something went wrong | ||
/// | ||
/// The following error codes can occur, among others: | ||
/// - std::errc::argument_list_too_long: The result requires more than | ||
/// std::numeric_limits<size_t>::max() bytes. | ||
/// - std::errc::illegal_byte_sequence: The input contains an invalid | ||
/// multibyte sequence. | ||
/// - std::errc::invalid_argument: The input contains an incomplete | ||
/// multibyte sequence. | ||
/// | ||
/// If the destination encoding is stateful, the shift state will be set | ||
/// to the initial state. | ||
/// | ||
/// In case of an error, the result string contains the successfully converted | ||
/// part of the input string. | ||
/// | ||
virtual std::error_code convertString(StringRef Source, | ||
SmallVectorImpl<char> &Result) = 0; | ||
|
||
/// Resets the converter to the initial state. | ||
virtual void reset() = 0; | ||
|
||
public: | ||
virtual ~EncodingConverterImplBase() = default; | ||
|
||
/// Converts a string and resets the converter to the initial state. | ||
std::error_code convert(StringRef Source, SmallVectorImpl<char> &Result) { | ||
auto EC = convertString(Source, Result); | ||
reset(); | ||
return EC; | ||
} | ||
}; | ||
} // namespace details | ||
|
||
// Names inspired by https://wg21.link/p1885. | ||
enum class TextEncoding { | ||
/// UTF-8 character set encoding. | ||
UTF8, | ||
|
||
/// IBM EBCDIC 1047 character set encoding. | ||
IBM1047 | ||
}; | ||
|
||
/// Utility class to convert between different character encodings. | ||
class EncodingConverter { | ||
std::unique_ptr<details::EncodingConverterImplBase> Converter; | ||
|
||
EncodingConverter( | ||
std::unique_ptr<details::EncodingConverterImplBase> Converter) | ||
: Converter(std::move(Converter)) {} | ||
|
||
public: | ||
/// Creates a EncodingConverter instance. | ||
/// Returns std::errc::invalid_argument in case the requested conversion is | ||
/// not supported. | ||
/// \param[in] From the source character encoding | ||
/// \param[in] To the target character encoding | ||
/// \return a EncodingConverter instance or an error code | ||
static ErrorOr<EncodingConverter> create(TextEncoding From, TextEncoding To); | ||
|
||
/// Creates a EncodingConverter instance. | ||
/// Returns std::errc::invalid_argument in case the requested conversion is | ||
/// not supported. | ||
/// \param[in] From name of the source character encoding | ||
/// \param[in] To name of the target character encoding | ||
/// \return a EncodingConverter instance or an error code | ||
static ErrorOr<EncodingConverter> create(StringRef From, StringRef To); | ||
|
||
EncodingConverter(const EncodingConverter &) = delete; | ||
EncodingConverter &operator=(const EncodingConverter &) = delete; | ||
|
||
EncodingConverter(EncodingConverter &&Other) | ||
: Converter(std::move(Other.Converter)) {} | ||
|
||
EncodingConverter &operator=(EncodingConverter &&Other) { | ||
if (this != &Other) | ||
Converter = std::move(Other.Converter); | ||
return *this; | ||
} | ||
|
||
~EncodingConverter() = default; | ||
|
||
/// Converts a string. | ||
/// \param[in] Source source string | ||
/// \param[out] Result container for converted string | ||
/// \return error code in case something went wrong | ||
std::error_code convert(StringRef Source, | ||
SmallVectorImpl<char> &Result) const { | ||
return Converter->convert(Source, Result); | ||
} | ||
|
||
ErrorOr<std::string> convert(StringRef Source) const { | ||
SmallString<100> Result; | ||
auto EC = Converter->convert(Source, Result); | ||
if (!EC) | ||
return std::string(Result); | ||
return EC; | ||
} | ||
}; | ||
|
||
} // namespace llvm | ||
|
||
#endif |
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
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.