Nico Weber | 941e47c | 2012-07-03 02:24:52 +0000 | [diff] [blame] | 1 | //===-- ConvertUTFWrapper.cpp - Wrap ConvertUTF.h with clang data types -----=== |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Basic/ConvertUTF.h" |
| 11 | #include "clang/Basic/LLVM.h" |
| 12 | |
| 13 | namespace clang { |
| 14 | |
| 15 | bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, |
| 16 | char *&ResultPtr) { |
| 17 | assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4); |
| 18 | ConversionResult result = conversionOK; |
| 19 | // Copy the character span over. |
| 20 | if (WideCharWidth == 1) { |
| 21 | if (!isLegalUTF8String(reinterpret_cast<const UTF8*>(Source.begin()), |
| 22 | reinterpret_cast<const UTF8*>(Source.end()))) |
| 23 | result = sourceIllegal; |
| 24 | memcpy(ResultPtr, Source.data(), Source.size()); |
| 25 | ResultPtr += Source.size(); |
| 26 | } else if (WideCharWidth == 2) { |
| 27 | const UTF8 *sourceStart = (const UTF8*)Source.data(); |
| 28 | // FIXME: Make the type of the result buffer correct instead of |
| 29 | // using reinterpret_cast. |
| 30 | UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr); |
| 31 | ConversionFlags flags = strictConversion; |
| 32 | result = ConvertUTF8toUTF16( |
| 33 | &sourceStart, sourceStart + Source.size(), |
| 34 | &targetStart, targetStart + 2*Source.size(), flags); |
| 35 | if (result == conversionOK) |
| 36 | ResultPtr = reinterpret_cast<char*>(targetStart); |
| 37 | } else if (WideCharWidth == 4) { |
| 38 | const UTF8 *sourceStart = (const UTF8*)Source.data(); |
| 39 | // FIXME: Make the type of the result buffer correct instead of |
| 40 | // using reinterpret_cast. |
| 41 | UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr); |
| 42 | ConversionFlags flags = strictConversion; |
| 43 | result = ConvertUTF8toUTF32( |
| 44 | &sourceStart, sourceStart + Source.size(), |
| 45 | &targetStart, targetStart + 4*Source.size(), flags); |
| 46 | if (result == conversionOK) |
| 47 | ResultPtr = reinterpret_cast<char*>(targetStart); |
| 48 | } |
| 49 | assert((result != targetExhausted) |
| 50 | && "ConvertUTF8toUTFXX exhausted target buffer"); |
| 51 | return result == conversionOK; |
| 52 | } |
| 53 | |
Dmitri Gribenko | e430313 | 2012-07-27 20:36:22 +0000 | [diff] [blame] | 54 | bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) { |
| 55 | const UTF32 *SourceStart = &Source; |
| 56 | const UTF32 *SourceEnd = SourceStart + 1; |
| 57 | UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr); |
| 58 | UTF8 *TargetEnd = TargetStart + 4; |
| 59 | ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd, |
| 60 | &TargetStart, TargetEnd, |
| 61 | strictConversion); |
| 62 | if (CR != conversionOK) |
| 63 | return false; |
| 64 | |
| 65 | ResultPtr = reinterpret_cast<char*>(TargetStart); |
| 66 | return true; |
Nico Weber | 941e47c | 2012-07-03 02:24:52 +0000 | [diff] [blame] | 67 | } |
Dmitri Gribenko | e430313 | 2012-07-27 20:36:22 +0000 | [diff] [blame] | 68 | |
| 69 | } // end namespace clang |
| 70 | |