ConvertUTFWrapper.cpp revision 328027bf269bb0c108bd8533908ccb36ba11e9f0
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 "llvm/Support/ConvertUTF.h"
11
12namespace llvm {
13
14bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
15                       char *&ResultPtr, const UTF8 *&ErrorPtr) {
16  assert(WideCharWidth == 1 || WideCharWidth == 2 || WideCharWidth == 4);
17  ConversionResult result = conversionOK;
18  // Copy the character span over.
19  if (WideCharWidth == 1) {
20    const UTF8 *Pos = reinterpret_cast<const UTF8*>(Source.begin());
21    if (!isLegalUTF8String(&Pos, reinterpret_cast<const UTF8*>(Source.end()))) {
22      result = sourceIllegal;
23      ErrorPtr = Pos;
24    } else {
25      memcpy(ResultPtr, Source.data(), Source.size());
26      ResultPtr += Source.size();
27    }
28  } else if (WideCharWidth == 2) {
29    const UTF8 *sourceStart = (const UTF8*)Source.data();
30    // FIXME: Make the type of the result buffer correct instead of
31    // using reinterpret_cast.
32    UTF16 *targetStart = reinterpret_cast<UTF16*>(ResultPtr);
33    ConversionFlags flags = strictConversion;
34    result = ConvertUTF8toUTF16(
35        &sourceStart, sourceStart + Source.size(),
36        &targetStart, targetStart + 2*Source.size(), flags);
37    if (result == conversionOK)
38      ResultPtr = reinterpret_cast<char*>(targetStart);
39    else
40      ErrorPtr = sourceStart;
41  } else if (WideCharWidth == 4) {
42    const UTF8 *sourceStart = (const UTF8*)Source.data();
43    // FIXME: Make the type of the result buffer correct instead of
44    // using reinterpret_cast.
45    UTF32 *targetStart = reinterpret_cast<UTF32*>(ResultPtr);
46    ConversionFlags flags = strictConversion;
47    result = ConvertUTF8toUTF32(
48        &sourceStart, sourceStart + Source.size(),
49        &targetStart, targetStart + 4*Source.size(), flags);
50    if (result == conversionOK)
51      ResultPtr = reinterpret_cast<char*>(targetStart);
52    else
53      ErrorPtr = sourceStart;
54  }
55  assert((result != targetExhausted)
56         && "ConvertUTF8toUTFXX exhausted target buffer");
57  return result == conversionOK;
58}
59
60bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr) {
61  const UTF32 *SourceStart = &Source;
62  const UTF32 *SourceEnd = SourceStart + 1;
63  UTF8 *TargetStart = reinterpret_cast<UTF8 *>(ResultPtr);
64  UTF8 *TargetEnd = TargetStart + 4;
65  ConversionResult CR = ConvertUTF32toUTF8(&SourceStart, SourceEnd,
66                                           &TargetStart, TargetEnd,
67                                           strictConversion);
68  if (CR != conversionOK)
69    return false;
70
71  ResultPtr = reinterpret_cast<char*>(TargetStart);
72  return true;
73}
74
75} // end namespace llvm
76
77