|
| 1 | +//===-- TextEncoding.h - Text encoding conversion class -----------*- 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_TEXT_ENCODING_H |
| 16 | +#define LLVM_SUPPORT_TEXT_ENCODING_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 <string> |
| 24 | +#include <system_error> |
| 25 | + |
| 26 | +namespace llvm { |
| 27 | + |
| 28 | +template <typename T> class SmallVectorImpl; |
| 29 | + |
| 30 | +namespace details { |
| 31 | +class TextEncodingConverterImplBase { |
| 32 | + |
| 33 | +private: |
| 34 | + /// Converts a string. |
| 35 | + /// \param[in] Source source string |
| 36 | + /// \param[out] Result container for converted string |
| 37 | + /// \return error code in case something went wrong |
| 38 | + /// |
| 39 | + /// The following error codes can occur, among others: |
| 40 | + /// - std::errc::argument_list_too_long: The result requires more than |
| 41 | + /// std::numeric_limits<size_t>::max() bytes. |
| 42 | + /// - std::errc::illegal_byte_sequence: The input contains an invalid |
| 43 | + /// multibyte sequence. |
| 44 | + /// - std::errc::invalid_argument: The input contains an incomplete |
| 45 | + /// multibyte sequence. |
| 46 | + /// |
| 47 | + /// If the destination encoding is stateful, the shift state will be set |
| 48 | + /// to the initial state. |
| 49 | + /// |
| 50 | + /// In case of an error, the result string contains the successfully converted |
| 51 | + /// part of the input string. |
| 52 | + /// |
| 53 | + virtual std::error_code convertString(StringRef Source, |
| 54 | + SmallVectorImpl<char> &Result) = 0; |
| 55 | + |
| 56 | + /// Resets the converter to the initial state. |
| 57 | + virtual void reset() = 0; |
| 58 | + |
| 59 | +public: |
| 60 | + virtual ~TextEncodingConverterImplBase() = default; |
| 61 | + |
| 62 | + /// Converts a string and resets the converter to the initial state. |
| 63 | + std::error_code convert(StringRef Source, SmallVectorImpl<char> &Result) { |
| 64 | + auto EC = convertString(Source, Result); |
| 65 | + reset(); |
| 66 | + return EC; |
| 67 | + } |
| 68 | +}; |
| 69 | +} // namespace details |
| 70 | + |
| 71 | +// Names inspired by https://wg21.link/p1885. |
| 72 | +enum class TextEncoding { |
| 73 | + /// UTF-8 character set encoding. |
| 74 | + UTF8, |
| 75 | + |
| 76 | + /// IBM EBCDIC 1047 character set encoding. |
| 77 | + IBM1047 |
| 78 | +}; |
| 79 | + |
| 80 | +/// Utility class to convert between different character encodings. |
| 81 | +class TextEncodingConverter { |
| 82 | + std::unique_ptr<details::TextEncodingConverterImplBase> Converter; |
| 83 | + |
| 84 | + TextEncodingConverter( |
| 85 | + std::unique_ptr<details::TextEncodingConverterImplBase> Converter) |
| 86 | + : Converter(std::move(Converter)) {} |
| 87 | + |
| 88 | +public: |
| 89 | + /// Creates a TextEncodingConverter instance. |
| 90 | + /// Returns std::errc::invalid_argument in case the requested conversion is |
| 91 | + /// not supported. |
| 92 | + /// \param[in] From the source character encoding |
| 93 | + /// \param[in] To the target character encoding |
| 94 | + /// \return a TextEncodingConverter instance or an error code |
| 95 | + static ErrorOr<TextEncodingConverter> create(TextEncoding From, |
| 96 | + TextEncoding To); |
| 97 | + |
| 98 | + /// Creates a TextEncodingConverter instance. |
| 99 | + /// Returns std::errc::invalid_argument in case the requested conversion is |
| 100 | + /// not supported. |
| 101 | + /// \param[in] From name of the source character encoding |
| 102 | + /// \param[in] To name of the target character encoding |
| 103 | + /// \return a TextEncodingConverter instance or an error code |
| 104 | + static ErrorOr<TextEncodingConverter> create(StringRef From, StringRef To); |
| 105 | + |
| 106 | + TextEncodingConverter(const TextEncodingConverter &) = delete; |
| 107 | + TextEncodingConverter &operator=(const TextEncodingConverter &) = delete; |
| 108 | + |
| 109 | + TextEncodingConverter(TextEncodingConverter &&Other) |
| 110 | + : Converter(std::move(Other.Converter)) {} |
| 111 | + |
| 112 | + TextEncodingConverter &operator=(TextEncodingConverter &&Other) { |
| 113 | + if (this != &Other) |
| 114 | + Converter = std::move(Other.Converter); |
| 115 | + return *this; |
| 116 | + } |
| 117 | + |
| 118 | + ~TextEncodingConverter() = default; |
| 119 | + |
| 120 | + /// Converts a string. |
| 121 | + /// \param[in] Source source string |
| 122 | + /// \param[out] Result container for converted string |
| 123 | + /// \return error code in case something went wrong |
| 124 | + std::error_code convert(StringRef Source, |
| 125 | + SmallVectorImpl<char> &Result) const { |
| 126 | + return Converter->convert(Source, Result); |
| 127 | + } |
| 128 | + |
| 129 | + ErrorOr<std::string> convert(StringRef Source) const { |
| 130 | + SmallString<100> Result; |
| 131 | + auto EC = Converter->convert(Source, Result); |
| 132 | + if (!EC) |
| 133 | + return std::string(Result); |
| 134 | + return EC; |
| 135 | + } |
| 136 | +}; |
| 137 | + |
| 138 | +} // namespace llvm |
| 139 | + |
| 140 | +#endif |
0 commit comments