LLVM 23.0.0git
TextEncoding.h
Go to the documentation of this file.
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
19#include "llvm/ADT/StringRef.h"
22
23#include <string>
24#include <system_error>
25
26namespace llvm {
27
28template <typename T> class SmallVectorImpl;
29
30namespace details {
32
33private:
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
59public:
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.
72enum class TextEncoding {
73 /// UTF-8 character set encoding.
75
76 /// IBM EBCDIC 1047 character set encoding.
78};
79
80/// Utility class to convert between different character encodings.
81class TextEncodingConverter {
82 std::unique_ptr<details::TextEncodingConverterImplBase> Converter;
83
84 TextEncodingConverter(
85 std::unique_ptr<details::TextEncodingConverterImplBase> Converter)
86 : Converter(std::move(Converter)) {}
87
88public:
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
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
105 StringRef To);
106
107 TextEncodingConverter(const TextEncodingConverter &) = delete;
108 TextEncodingConverter &operator=(const TextEncodingConverter &) = delete;
109
110 TextEncodingConverter(TextEncodingConverter &&Other)
111 : Converter(std::move(Other.Converter)) {}
112
113 TextEncodingConverter &operator=(TextEncodingConverter &&Other) {
114 if (this != &Other)
115 Converter = std::move(Other.Converter);
116 return *this;
117 }
118
120
121 /// Converts a string.
122 /// \param[in] Source source string
123 /// \param[out] Result container for converted string
124 /// \return error code in case something went wrong
125 std::error_code convert(StringRef Source,
126 SmallVectorImpl<char> &Result) const {
127 return Converter->convert(Source, Result);
128 }
129
131 SmallString<100> Result;
132 auto EC = Converter->convert(Source, Result);
133 if (!EC)
134 return std::string(Result);
135 return EC;
136 }
137};
138
139} // namespace llvm
140
141#endif
#define LLVM_ABI
Definition Compiler.h:215
Early If Converter
Provides ErrorOr<T> smart pointer.
This file defines the SmallString class.
Represents either an error or a value T.
Definition ErrorOr.h:56
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TextEncodingConverter(const TextEncodingConverter &)=delete
TextEncodingConverter & operator=(const TextEncodingConverter &)=delete
std::error_code convert(StringRef Source, SmallVectorImpl< char > &Result) const
Converts a string.
TextEncodingConverter(TextEncodingConverter &&Other)
ErrorOr< std::string > convert(StringRef Source) const
static LLVM_ABI ErrorOr< TextEncodingConverter > create(TextEncoding From, TextEncoding To)
Creates a TextEncodingConverter instance.
TextEncodingConverter & operator=(TextEncodingConverter &&Other)
std::error_code convert(StringRef Source, SmallVectorImpl< char > &Result)
Converts a string and resets the converter to the initial state.
This is an optimization pass for GlobalISel generic memory operations.
unsigned char UTF8
Definition ConvertUTF.h:131
@ IBM1047
IBM EBCDIC 1047 character set encoding.
@ Other
Any other memory.
Definition ModRef.h:68
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:860