Index: llvm/cmake/config-ix.cmake =================================================================== --- llvm/cmake/config-ix.cmake +++ llvm/cmake/config-ix.cmake @@ -194,6 +194,14 @@ set(XAR_LIB xar) endif() +# Check for iconv. +find_package(Iconv) +if(Iconv_FOUND) + set(HAVE_ICONV 1) +else() + set(HAVE_ICONV 0) +endif() + # function checks check_symbol_exists(arc4random "stdlib.h" HAVE_DECL_ARC4RANDOM) find_package(Backtrace) Index: llvm/include/llvm/Config/config.h.cmake =================================================================== --- llvm/include/llvm/Config/config.h.cmake +++ llvm/include/llvm/Config/config.h.cmake @@ -97,6 +97,9 @@ /* Define to 1 if you have the `getrusage' function. */ #cmakedefine HAVE_GETRUSAGE ${HAVE_GETRUSAGE} +/* Define to 1 if you have the iconv library functions. */ +#cmakedefine HAVE_ICONV ${HAVE_ICONV} + /* Define to 1 if you have the `isatty' function. */ #cmakedefine HAVE_ISATTY 1 Index: llvm/include/llvm/Support/CharSet.h =================================================================== --- /dev/null +++ llvm/include/llvm/Support/CharSet.h @@ -0,0 +1,117 @@ +//===-- CharSet.h - Utility class to convert between char sets ----*- C++ -*-=// +// +// 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_CHARSET_H +#define LLVM_SUPPORT_CHARSET_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Config/config.h" +#include "llvm/Support/ErrorOr.h" + +#include +#include +#include + +namespace llvm { + +template class SmallVectorImpl; + +/// Utility class to convert between different character set encodings. +/// The class always supports converting between EBCDIC 1047 and Latin-1/UTF-8. +/// If the iconv library is available, then arbitrary conversions are supported. +/// TODO Add Windows support. +class CharSetConverter { +public: + using ConverterFunc = + std::function &)>; + using CleanupFunc = std::function; + +private: + ConverterFunc Convert; + CleanupFunc Cleanup; + +public: + enum CharSetNames { + /// UTF-8 character set encoding. + CS_UTF8, + + /// ISO 8859-1 (Latin-1) character set encoding. + CS_LATIN1, + + /// IBM EBCDIC 1047 character set encoding. + CS_IBM1047 + }; + +private: + CharSetConverter(ConverterFunc Convert, CleanupFunc Cleanup) + : Convert(Convert), Cleanup(Cleanup) {} + +public: + /// Creates a CharSetConverter instance. + /// \param[in] CSFrom name of the source character encoding + /// \param[in] CSTo name of the target character encoding + /// \return a CharSetConverter instance + static CharSetConverter create(CharSetNames CSFrom, CharSetNames CSTo); + + /// Creates a CharSetConverter instance. + /// Returns std::errc::invalid_argument in case the requested conversion is + /// not supported. + /// \param[in] CSFrom name of the source character encoding + /// \param[in] CSTo name of the target character encoding + /// \return a CharSetConverter instance or an error code + static ErrorOr create(StringRef CSFrom, StringRef CSTo); + + CharSetConverter(const CharSetConverter &) = delete; + CharSetConverter &operator=(const CharSetConverter &) = delete; + + CharSetConverter(CharSetConverter &&Other) { + this->Convert = Other.Convert; + this->Cleanup = Other.Cleanup; + Other.Cleanup = nullptr; + } + + CharSetConverter &operator=(CharSetConverter &&Other) { + this->Convert = Other.Convert; + this->Cleanup = Other.Cleanup; + Other.Cleanup = nullptr; + return *this; + } + + ~CharSetConverter() { + if (Cleanup) + Cleanup(); + } + + /// Converts a string. + /// \param[in] Source source string + /// \param[in,out] Result container for converted string + /// \return error code in case something went wrong + std::error_code convert(StringRef Source, + SmallVectorImpl &Result) const { + return Convert(Source, Result); + } + + /// Converts a string. + /// \param[in] Source source string + /// \param[in,out] Result container for converted string + /// \return error code in case something went wrong + std::error_code convert(const std::string &Source, + SmallVectorImpl &Result) const { + return convert(StringRef(Source), Result); + } +}; + +} // namespace llvm + +#endif Index: llvm/lib/Support/CMakeLists.txt =================================================================== --- llvm/lib/Support/CMakeLists.txt +++ llvm/lib/Support/CMakeLists.txt @@ -53,6 +53,11 @@ set(system_libs ${system_libs} ${Z3_LIBRARIES}) endif() +# Link iconv library if it is an external library. +if(Iconv_FOUND AND NOT Iconv_IS_BUILT_IN) + set(system_libs ${system_libs} ${Iconv_LIBRARIES}) +endif() + # Override the C runtime allocator on Windows and embed it into LLVM tools & libraries if(LLVM_INTEGRATED_CRT_ALLOC) if (CMAKE_BUILD_TYPE AND NOT ${LLVM_USE_CRT_${uppercase_CMAKE_BUILD_TYPE}} MATCHES "^(MT|MTd)$") @@ -102,6 +107,7 @@ BuryPointer.cpp CachePruning.cpp circular_raw_ostream.cpp + CharSet.cpp Chrono.cpp COM.cpp CodeGenCoverage.cpp @@ -229,6 +235,7 @@ ${LLVM_MAIN_INCLUDE_DIR}/llvm/ADT ${LLVM_MAIN_INCLUDE_DIR}/llvm/Support ${Backtrace_INCLUDE_DIRS} + ${Iconv_INCLUDE_DIRS} LINK_LIBS ${system_libs} ${imported_libs} ${delayload_flags} Index: llvm/lib/Support/CharSet.cpp =================================================================== --- /dev/null +++ llvm/lib/Support/CharSet.cpp @@ -0,0 +1,203 @@ +//===-- CharSet.cpp - Utility class to convert between char sets --*- C++ -*-=// +// +// 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 utility classes to convert between different character +/// set encoding. +/// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/CharSet.h" +#include "llvm/ADT/SmallVector.h" +#include +#include + +#ifdef HAVE_ICONV +#include +#endif + +using namespace llvm; + +namespace { + +// Maps the charset name to enum constant if possible. +Optional getKnownCharSet(StringRef CSName) { +#define CSNAME(CS, STR) \ + if (CSName == STR) \ + return CS + CSNAME(CharSetConverter::CS_UTF8, "UTF-8"); + CSNAME(CharSetConverter::CS_LATIN1, "ISO8859-1"); + CSNAME(CharSetConverter::CS_IBM1047, "IBM-1047"); +#undef CSNAME + return None; +} + +// Character conversion between Enhanced ASCII and EBCDIC (IBM-1047). +const unsigned char ISO88591ToIBM1047[256] = { + 0x00, 0x01, 0x02, 0x03, 0x37, 0x2d, 0x2e, 0x2f, 0x16, 0x05, 0x15, 0x0b, + 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x3c, 0x3d, 0x32, 0x26, + 0x18, 0x19, 0x3f, 0x27, 0x1c, 0x1d, 0x1e, 0x1f, 0x40, 0x5a, 0x7f, 0x7b, + 0x5b, 0x6c, 0x50, 0x7d, 0x4d, 0x5d, 0x5c, 0x4e, 0x6b, 0x60, 0x4b, 0x61, + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0x7a, 0x5e, + 0x4c, 0x7e, 0x6e, 0x6f, 0x7c, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xe2, + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xad, 0xe0, 0xbd, 0x5f, 0x6d, + 0x79, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x91, 0x92, + 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, + 0xa7, 0xa8, 0xa9, 0xc0, 0x4f, 0xd0, 0xa1, 0x07, 0x20, 0x21, 0x22, 0x23, + 0x24, 0x25, 0x06, 0x17, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x09, 0x0a, 0x1b, + 0x30, 0x31, 0x1a, 0x33, 0x34, 0x35, 0x36, 0x08, 0x38, 0x39, 0x3a, 0x3b, + 0x04, 0x14, 0x3e, 0xff, 0x41, 0xaa, 0x4a, 0xb1, 0x9f, 0xb2, 0x6a, 0xb5, + 0xbb, 0xb4, 0x9a, 0x8a, 0xb0, 0xca, 0xaf, 0xbc, 0x90, 0x8f, 0xea, 0xfa, + 0xbe, 0xa0, 0xb6, 0xb3, 0x9d, 0xda, 0x9b, 0x8b, 0xb7, 0xb8, 0xb9, 0xab, + 0x64, 0x65, 0x62, 0x66, 0x63, 0x67, 0x9e, 0x68, 0x74, 0x71, 0x72, 0x73, + 0x78, 0x75, 0x76, 0x77, 0xac, 0x69, 0xed, 0xee, 0xeb, 0xef, 0xec, 0xbf, + 0x80, 0xfd, 0xfe, 0xfb, 0xfc, 0xba, 0xae, 0x59, 0x44, 0x45, 0x42, 0x46, + 0x43, 0x47, 0x9c, 0x48, 0x54, 0x51, 0x52, 0x53, 0x58, 0x55, 0x56, 0x57, + 0x8c, 0x49, 0xcd, 0xce, 0xcb, 0xcf, 0xcc, 0xe1, 0x70, 0xdd, 0xde, 0xdb, + 0xdc, 0x8d, 0x8e, 0xdf}; + +const unsigned char IBM1047ToISO88591[256] = { + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d, 0x8e, 0x0b, + 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87, + 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, 0x80, 0x81, 0x82, 0x83, + 0x84, 0x85, 0x17, 0x1b, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b, + 0x14, 0x15, 0x9e, 0x1a, 0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5, + 0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, 0x26, 0xe9, 0xea, 0xeb, + 0xe8, 0xed, 0xee, 0xef, 0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e, + 0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5, 0xc7, 0xd1, 0xa6, 0x2c, + 0x25, 0x5f, 0x3e, 0x3f, 0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf, + 0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, 0xd8, 0x61, 0x62, 0x63, + 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1, + 0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0xaa, 0xba, + 0xe6, 0xb8, 0xc6, 0xa4, 0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, + 0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae, 0xac, 0xa3, 0xa5, 0xb7, + 0xa9, 0xa7, 0xb6, 0xbc, 0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xad, 0xf4, + 0xf6, 0xf2, 0xf3, 0xf5, 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, + 0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff, 0x5c, 0xf7, 0x53, 0x54, + 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xb3, 0xdb, + 0xdc, 0xd9, 0xda, 0x9f}; + +enum { NoUTF8 = 0x0, SrcIsUTF8 = 0x1, DstIsUTF8 = 0x2 }; + +std::error_code convertWithTable(const unsigned char *Table, unsigned Flags, + StringRef Source, + SmallVectorImpl &Result) { + const unsigned char *Ptr = + reinterpret_cast(Source.data()); + size_t Length = Source.size(); + while (Length--) { + unsigned char Ch = *Ptr++; + // Handle UTF-8 2-byte-sequences in input. + if (Flags & SrcIsUTF8) { + if (Ch >= 128) { + // Only two-byte sequences can be decoded. + if (Ch != 0xc2 && Ch != 0xc3) + return std::make_error_code(std::errc::illegal_byte_sequence); + // Is buffer truncated? + if (!Length) + return std::make_error_code(std::errc::invalid_argument); + unsigned char Ch2 = *Ptr++; + // Is second byte well-formed? + if ((Ch2 & 0xc0) != 0x80) + return std::make_error_code(std::errc::illegal_byte_sequence); + Ch = Ch2 | (Ch << 6); + Length--; + } + } + // Translate the character. + Ch = Table ? Table[Ch] : Ch; + // Handle UTF-8 2-byte-sequences in output. + if (Flags & DstIsUTF8) { + if (Ch >= 128) { + // First byte prefixed with either 0xc2 or 0xc3. + Result.push_back(static_cast(0xc0 | (Ch >> 6))); + // Second byte is either the same as the ASCII byte or ASCII byte -64. + Ch = Ch & 0xbf; + } + } + Result.push_back(static_cast(Ch)); + } + return std::error_code(); +} + +#ifdef HAVE_ICONV +std::error_code convertWithIconv(iconv_t ConvDesc, StringRef Source, + SmallVectorImpl &Result) { + // Setup the input. + size_t InputLength = Source.size(); + char *Input = const_cast(Source.data()); + // Setup the output. We directly write into the SmallVector. + size_t Capacity = Result.capacity(); + Result.resize(Capacity); + char *Output = static_cast(Result.data()); + size_t OutputLength = Capacity; + + while (iconv(ConvDesc, &Input, &InputLength, &Output, &OutputLength) == + static_cast(-1)) { + if (errno == E2BIG) { + // No space left in output buffer. Double the size of the underlying + // memory in the SmallVectorImpl, adjust pointer and length and continue + // the conversion. + const size_t Used = Capacity - OutputLength; + Capacity *= 2; + Result.resize(Capacity); + Output = static_cast(Result.data()) + Used; + OutputLength = Capacity - Used; + } else + // Some other error occured. + return std::error_code(errno, std::generic_category()); + } + + // Re-adjust size to actual size. + Result.resize(Capacity - OutputLength); + return std::error_code(); +} +#endif +} // namespace + +CharSetConverter CharSetConverter::create(CharSetNames CSFrom, + CharSetNames CSTo) { + unsigned Flags = NoUTF8; + if (CSFrom == CS_UTF8) + Flags |= SrcIsUTF8; + if (CSTo == CS_UTF8) + Flags |= DstIsUTF8; + const unsigned char *Table = nullptr; + if (CSFrom == CS_IBM1047) + Table = IBM1047ToISO88591; + if (CSTo == CS_IBM1047) + Table = ISO88591ToIBM1047; + return CharSetConverter{ + [Table, Flags](StringRef Source, SmallVectorImpl &Result) { + return convertWithTable(Table, Flags, Source, Result); + }, + nullptr}; +} + +ErrorOr CharSetConverter::create(StringRef CSFrom, + StringRef CSTo) { + Optional From = getKnownCharSet(CSFrom); + Optional To = getKnownCharSet(CSTo); + if (From && To) + return create(*From, *To); +#ifdef HAVE_ICONV + iconv_t ConvDesc = iconv_open(CSTo.str().c_str(), CSFrom.str().c_str()); + if (ConvDesc == reinterpret_cast(-1)) + return std::error_code(errno, std::generic_category()); + return CharSetConverter{ + [ConvDesc](StringRef Source, SmallVectorImpl &Result) { + return convertWithIconv(ConvDesc, Source, Result); + }, + [ConvDesc]() { iconv_close(ConvDesc); }}; +#endif + return std::make_error_code(std::errc::invalid_argument); +} \ No newline at end of file Index: llvm/unittests/Support/CMakeLists.txt =================================================================== --- llvm/unittests/Support/CMakeLists.txt +++ llvm/unittests/Support/CMakeLists.txt @@ -14,6 +14,7 @@ BlockFrequencyTest.cpp BranchProbabilityTest.cpp CachePruningTest.cpp + CharSetTest.cpp CrashRecoveryTest.cpp Casting.cpp CheckedArithmeticTest.cpp Index: llvm/unittests/Support/CharSetTest.cpp =================================================================== --- /dev/null +++ llvm/unittests/Support/CharSetTest.cpp @@ -0,0 +1,191 @@ +//===- unittests/Support/CharSetTest.cpp - Charset conversion tests -------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/CharSet.h" +#include "llvm/ADT/SmallString.h" +#include "gtest/gtest.h" +using namespace llvm; + +namespace { + +// String "Hello World!" +static const char HelloA[] = + "\x48\x65\x6C\x6C\x6F\x20\x57\x6F\x72\x6C\x64\x21\x0a"; +static const char HelloE[] = + "\xC8\x85\x93\x93\x96\x40\xE6\x96\x99\x93\x84\x5A\x15"; + +// String "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +static const char ABCStrA[] = + "\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F\x50\x51\x52" + "\x53\x54\x55\x56\x57\x58\x59\x5A\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A" + "\x6B\x6C\x6D\x6E\x6F\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A"; +static const char ABCStrE[] = + "\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9" + "\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91" + "\x92\x93\x94\x95\x96\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9"; + +// String "¡¢£AÄÅÆEÈÉÊaàáâãäeèéêë" +static const char AccentUTF[] = + "\xc2\xa1\xc2\xa2\xc2\xa3\x41\xc3\x84\xc3\x85\xc3\x86\x45\xc3\x88\xc3\x89" + "\xc3\x8a\x61\xc3\xa0\xc3\xa1\xc3\xa2\xc3\xa3\xc3\xa4\x65\xc3\xa8\xc3\xa9" + "\xc3\xaa\xc3\xab"; +static const char AccentE[] = "\xaa\x4a\xb1\xc1\x63\x67\x9e\xc5\x74\x71\x72" + "\x81\x44\x45\x42\x46\x43\x85\x54\x51\x52\x53"; + +TEST(CharSet, FromASCII) { + // Hello string. + StringRef Src(HelloA); + SmallString<64> Dst; + + CharSetConverter Conv = CharSetConverter::create( + CharSetConverter::CS_LATIN1, CharSetConverter::CS_IBM1047); + std::error_code EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(HelloE, static_cast(Dst).c_str()); + + // ABC string. + Src = ABCStrA; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(ABCStrE, static_cast(Dst).c_str()); +} + +TEST(CharSet, ToASCII) { + // Hello string. + StringRef Src(HelloE); + SmallString<64> Dst; + + CharSetConverter Conv = CharSetConverter::create(CharSetConverter::CS_IBM1047, + CharSetConverter::CS_LATIN1); + std::error_code EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(HelloA, static_cast(Dst).c_str()); + + // ABC string. + Src = ABCStrE; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(ABCStrA, static_cast(Dst).c_str()); +} + +TEST(CharSet, FromUTF8) { + // Hello string. + StringRef Src(HelloA); + SmallString<64> Dst; + + CharSetConverter Conv = CharSetConverter::create( + CharSetConverter::CS_UTF8, CharSetConverter::CS_IBM1047); + std::error_code EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(HelloE, static_cast(Dst).c_str()); + + // ABC string. + Src = ABCStrA; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(ABCStrE, static_cast(Dst).c_str()); + + // Accent string. + Src = AccentUTF; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(AccentE, static_cast(Dst).c_str()); +} + +TEST(CharSet, ToUTF8) { + // Hello string. + StringRef Src(HelloE); + SmallString<64> Dst; + + CharSetConverter Conv = CharSetConverter::create(CharSetConverter::CS_IBM1047, + CharSetConverter::CS_UTF8); + std::error_code EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(HelloA, static_cast(Dst).c_str()); + + // ABC string. + Src = ABCStrE; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(ABCStrA, static_cast(Dst).c_str()); + + // Accent string. + Src = AccentE; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(AccentUTF, static_cast(Dst).c_str()); +} + +TEST(CharSet, Identity) { + // Hello string. + StringRef Src(HelloA); + SmallString<64> Dst; + + CharSetConverter Conv = CharSetConverter::create(CharSetConverter::CS_LATIN1, + CharSetConverter::CS_LATIN1); + std::error_code EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(HelloA, static_cast(Dst).c_str()); + + // ABC string. + Src = ABCStrA; + Dst.clear(); + EC = Conv.convert(Src, Dst); + EXPECT_TRUE(!EC); + EXPECT_STREQ(ABCStrA, static_cast(Dst).c_str()); +} + +TEST(CharSet, RoundTrip) { + ErrorOr ConvToUTF16 = + CharSetConverter::create("IBM-1047", "UTF-16"); + // Stop test if conversion is not supported (no underlying iconv support). + if (!ConvToUTF16) { + ASSERT_EQ(ConvToUTF16.getError(), + std::make_error_code(std::errc::invalid_argument)); + return; + } + ErrorOr ConvToUTF32 = + CharSetConverter::create("UTF-16", "UTF-32"); + // Stop test if conversion is not supported (no underlying iconv support). + if (!ConvToUTF32) { + ASSERT_EQ(ConvToUTF32.getError(), + std::make_error_code(std::errc::invalid_argument)); + return; + } + ErrorOr ConvToEBCDIC = + CharSetConverter::create("UTF-32", "IBM-1047"); + // Stop test if conversion is not supported (no underlying iconv support). + if (!ConvToEBCDIC) { + ASSERT_EQ(ConvToEBCDIC.getError(), + std::make_error_code(std::errc::invalid_argument)); + return; + } + + // Setup source string. + char SrcStr[256]; + for (size_t I = 0; I < 256; ++I) + SrcStr[I] = (I + 1) % 256; + + SmallString<99> Dst1Str, Dst2Str, Dst3Str; + + std::error_code EC = ConvToUTF16->convert(StringRef(SrcStr), Dst1Str); + EXPECT_TRUE(!EC); + EC = ConvToUTF32->convert(Dst1Str, Dst2Str); + EXPECT_TRUE(!EC); + EC = ConvToEBCDIC->convert(Dst2Str, Dst3Str); + EXPECT_TRUE(!EC); + EXPECT_STREQ(SrcStr, static_cast(Dst3Str).c_str()); +} + +} // namespace