|
| 1 | +//===-- CharSet.h - Utility class to convert between char sets ----*- C++ -*-=// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file |
| 10 | +/// This file provides a utility class to convert between different character |
| 11 | +/// set encodings. |
| 12 | +/// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#ifndef LLVM_SUPPORT_CHARSET_H |
| 16 | +#define LLVM_SUPPORT_CHARSET_H |
| 17 | + |
| 18 | +#include "llvm/ADT/SmallString.h" |
| 19 | +#include "llvm/ADT/StringRef.h" |
| 20 | +#include "llvm/Config/config.h" |
| 21 | +#include "llvm/Support/ErrorOr.h" |
| 22 | + |
| 23 | +#include <functional> |
| 24 | +#include <string> |
| 25 | +#include <system_error> |
| 26 | + |
| 27 | +namespace llvm { |
| 28 | + |
| 29 | +template <typename T> class SmallVectorImpl; |
| 30 | + |
| 31 | +namespace details { |
| 32 | +class CharSetConverterImplBase { |
| 33 | +public: |
| 34 | + virtual ~CharSetConverterImplBase() = default; |
| 35 | + |
| 36 | + /// Converts a string. |
| 37 | + /// \param[in] Source source string |
| 38 | + /// \param[in,out] Result container for converted string |
| 39 | + /// \param[in] ShouldAutoFlush Append shift-back sequence after conversion |
| 40 | + /// for multi-byte encodings iff true. |
| 41 | + /// \return error code in case something went wrong |
| 42 | + /// |
| 43 | + /// The following error codes can occur, among others: |
| 44 | + /// - std::errc::argument_list_too_long: The result requires more than |
| 45 | + /// std::numeric_limits<size_t>::max() bytes. |
| 46 | + /// - std::errc::illegal_byte_sequence: The input contains an invalid |
| 47 | + /// multibyte sequence. |
| 48 | + /// - std::errc::invalid_argument: The input contains an incomplete |
| 49 | + /// multibyte sequence. |
| 50 | + /// |
| 51 | + /// In case of an error, the result string contains the successfully converted |
| 52 | + /// part of the input string. |
| 53 | + /// |
| 54 | + |
| 55 | + virtual std::error_code convert(StringRef Source, |
| 56 | + SmallVectorImpl<char> &Result, |
| 57 | + bool ShouldAutoFlush) const = 0; |
| 58 | + |
| 59 | + /// Restore the conversion to the original state. |
| 60 | + /// \return error code in case something went wrong |
| 61 | + /// |
| 62 | + /// If the original character set or the destination character set |
| 63 | + /// are multi-byte character sets, set the shift state to the initial |
| 64 | + /// state. Otherwise this is a no-op. |
| 65 | + virtual std::error_code flush() const = 0; |
| 66 | + |
| 67 | + virtual std::error_code flush(SmallVectorImpl<char> &Result) const = 0; |
| 68 | +}; |
| 69 | +} // namespace details |
| 70 | + |
| 71 | +// Names inspired by https://wg21.link/p1885. |
| 72 | +namespace text_encoding { |
| 73 | +enum class id { |
| 74 | + /// UTF-8 character set encoding. |
| 75 | + UTF8, |
| 76 | + |
| 77 | + /// IBM EBCDIC 1047 character set encoding. |
| 78 | + IBM1047 |
| 79 | +}; |
| 80 | +} // end namespace text_encoding |
| 81 | + |
| 82 | +/// Utility class to convert between different character set encodings. |
| 83 | +/// The class always supports converting between EBCDIC 1047 and Latin-1/UTF-8. |
| 84 | +class CharSetConverter { |
| 85 | + // details::CharSetConverterImplBase *Converter; |
| 86 | + std::unique_ptr<details::CharSetConverterImplBase> Converter; |
| 87 | + |
| 88 | + CharSetConverter(std::unique_ptr<details::CharSetConverterImplBase> Converter) |
| 89 | + : Converter(std::move(Converter)) {} |
| 90 | + |
| 91 | +public: |
| 92 | + /// Creates a CharSetConverter instance. |
| 93 | + /// \param[in] CSFrom name of the source character encoding |
| 94 | + /// \param[in] CSTo name of the target character encoding |
| 95 | + /// \return a CharSetConverter instance |
| 96 | + static CharSetConverter create(text_encoding::id CSFrom, |
| 97 | + text_encoding::id CSTo); |
| 98 | + |
| 99 | + /// Creates a CharSetConverter instance. |
| 100 | + /// Returns std::errc::invalid_argument in case the requested conversion is |
| 101 | + /// not supported. |
| 102 | + /// \param[in] CPFrom name of the source character encoding |
| 103 | + /// \param[in] CPTo name of the target character encoding |
| 104 | + /// \return a CharSetConverter instance or an error code |
| 105 | + static ErrorOr<CharSetConverter> create(StringRef CPFrom, StringRef CPTo); |
| 106 | + |
| 107 | + CharSetConverter(const CharSetConverter &) = delete; |
| 108 | + CharSetConverter &operator=(const CharSetConverter &) = delete; |
| 109 | + |
| 110 | + CharSetConverter(CharSetConverter &&Other) { |
| 111 | + Converter = std::move(Other.Converter); |
| 112 | + } |
| 113 | + |
| 114 | + CharSetConverter &operator=(CharSetConverter &&Other) { |
| 115 | + if (this != &Other) |
| 116 | + Converter = std::move(Other.Converter); |
| 117 | + return *this; |
| 118 | + } |
| 119 | + |
| 120 | + ~CharSetConverter() = default; |
| 121 | + |
| 122 | + /// Converts a string. |
| 123 | + /// \param[in] Source source string |
| 124 | + /// \param[in,out] Result container for converted string |
| 125 | + /// \param[in] ShouldAutoFlush Append shift-back sequence after conversion |
| 126 | + /// for multi-byte encodings. |
| 127 | + /// \return error code in case something went wrong |
| 128 | + std::error_code convert(StringRef Source, SmallVectorImpl<char> &Result, |
| 129 | + bool ShouldAutoFlush = true) const { |
| 130 | + return Converter->convert(Source, Result, ShouldAutoFlush); |
| 131 | + } |
| 132 | + |
| 133 | + char convert(char SingleChar) const { |
| 134 | + SmallString<1> Result; |
| 135 | + Converter->convert(StringRef(&SingleChar, 1), Result, false); |
| 136 | + return Result[0]; |
| 137 | + } |
| 138 | + |
| 139 | + /// Converts a string. |
| 140 | + /// \param[in] Source source string |
| 141 | + /// \param[in,out] Result container for converted string |
| 142 | + /// \param[in] ShouldAutoFlush Append shift-back sequence after conversion |
| 143 | + /// for multi-byte encodings iff true. |
| 144 | + /// \return error code in case something went wrong |
| 145 | + std::error_code convert(const std::string &Source, |
| 146 | + SmallVectorImpl<char> &Result, |
| 147 | + bool ShouldAutoFlush = true) const { |
| 148 | + return convert(StringRef(Source), Result, ShouldAutoFlush); |
| 149 | + } |
| 150 | + |
| 151 | + std::error_code flush() const { return Converter->flush(); } |
| 152 | + |
| 153 | + std::error_code flush(SmallVectorImpl<char> &Result) const { |
| 154 | + return Converter->flush(Result); |
| 155 | + } |
| 156 | +}; |
| 157 | + |
| 158 | +} // namespace llvm |
| 159 | + |
| 160 | +#endif |
0 commit comments