]> granicus.if.org Git - icu/commitdiff
ICU-12992 make tools & tests work with configured UChar=uint16_t
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Mar 2017 01:07:20 +0000 (01:07 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Mar 2017 01:07:20 +0000 (01:07 +0000)
X-SVN-Rev: 39742

15 files changed:
icu4c/source/common/normalizer2impl.h
icu4c/source/common/uinvchar.h
icu4c/source/common/unicode/umachine.h
icu4c/source/common/unicode/unistr.h
icu4c/source/common/unistr.cpp
icu4c/source/test/iotest/stream.cpp
icu4c/source/tools/ctestfw/datamap.cpp
icu4c/source/tools/gennorm2/n2builder.cpp
icu4c/source/tools/genrb/reslist.cpp
icu4c/source/tools/genrb/reslist.h
icu4c/source/tools/genrb/wrtxml.cpp
icu4c/source/tools/toolutil/dbgutil.cpp
icu4c/source/tools/toolutil/ppucd.cpp
icu4c/source/tools/toolutil/toolutil.h
icu4c/source/tools/toolutil/xmlparser.cpp

index 6dba0eab5c4daf4b48bc560fa5b8256eef711c2a..946abee98f3df10afe5d38b802b23164cc1bb571 100644 (file)
@@ -176,7 +176,7 @@ public:
         lastCC=0;
     }
     void copyReorderableSuffixTo(UnicodeString &s) const {
-        s.setTo(reorderStart, (int32_t)(limit-reorderStart));
+        s.setTo(ConstChar16Ptr(reorderStart), (int32_t)(limit-reorderStart));
     }
 private:
     /*
index 0bb5e73d04ab57ac6e15de9cd4ca8103cda289ef..2a960bdfca4438948386d0b7df241162649ea2ce 100644 (file)
@@ -64,7 +64,11 @@ uprv_isInvariantUString(const UChar *s, int32_t length);
  */
 U_INTERNAL inline UBool U_EXPORT2
 uprv_isInvariantUnicodeString(const icu::UnicodeString &s) {
-    return uprv_isInvariantUString(s.getBuffer(), s.length());
+    const char16_t *p = s.getBuffer();
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return uprv_isInvariantUString(reinterpret_cast<const UChar *>(p), s.length());
 }
 
 #endif  /* __cplusplus */
index e0fedfe95790a421b4d05634ca0d948f713d04da..4c8b5c1f1e2a4519f0cc07d611cb99b883484c3e 100644 (file)
@@ -313,6 +313,14 @@ typedef int8_t UBool;
  *
  * @stable ICU 4.4
  */
+#if 1
+    // #if 1 is normal. UChar defaults to char16_t in C++.
+    // For configuration testing of UChar=uint16_t temporarily change this to #if 0.
+    // The intltest Makefile #defines UCHAR_TYPE=char16_t,
+    // so we only #define it to uint16_t if it is undefined so far.
+#elif !defined(UCHAR_TYPE)
+#   define UCHAR_TYPE uint16_t
+#endif
 #if defined(U_COMBINED_IMPLEMENTATION) || defined(U_COMMON_IMPLEMENTATION) || \
         defined(U_I18N_IMPLEMENTATION) || defined(U_IO_IMPLEMENTATION)
     // Inside the ICU library code, never configurable.
index 9eedd2f52df08e66f4296231e322707676928b93..835e64ff7740bc2c46e9778bdf4a8f53f98cba2a 100644 (file)
@@ -2059,7 +2059,7 @@ public:
    * @stable ICU 2.0
    */
   UnicodeString &setTo(UBool isTerminated,
-                       const char16_t *text,
+                       ConstChar16Ptr text,
                        int32_t textLength);
 
   /**
@@ -3495,6 +3495,13 @@ protected:
   virtual UChar32 getChar32At(int32_t offset) const;
 
 private:
+  static inline const UChar *constUCharPtr(const char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<const UChar *>(p);
+  }
+
   // For char* constructors. Could be made public.
   UnicodeString &setToUTF8(StringPiece utf8);
   // For extract(char*).
@@ -4360,7 +4367,7 @@ UnicodeString::startsWith(const UnicodeString& srcText,
 inline UBool
 UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(srcChars);
+    srcLength = u_strlen(constUCharPtr(srcChars));
   }
   return doCompare(0, srcLength, srcChars, 0, srcLength) == 0;
 }
@@ -4368,7 +4375,7 @@ UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
 inline UBool
 UnicodeString::startsWith(const char16_t *srcChars, int32_t srcStart, int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(srcChars);
+    srcLength = u_strlen(constUCharPtr(srcChars));
   }
   return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;
 }
@@ -4391,7 +4398,7 @@ inline UBool
 UnicodeString::endsWith(ConstChar16Ptr srcChars,
             int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(srcChars);
+    srcLength = u_strlen(constUCharPtr(srcChars));
   }
   return doCompare(length() - srcLength, srcLength,
                    srcChars, 0, srcLength) == 0;
@@ -4402,7 +4409,7 @@ UnicodeString::endsWith(const char16_t *srcChars,
             int32_t srcStart,
             int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(srcChars + srcStart);
+    srcLength = u_strlen(constUCharPtr(srcChars + srcStart));
   }
   return doCompare(length() - srcLength, srcLength,
                    srcChars, srcStart, srcLength) == 0;
index 416b909917d54482f400a2ecaac2bb94ebe8bea1..1bfb71aa107b43812e1d6ffa28ce151209d05f95 100644 (file)
@@ -1258,7 +1258,7 @@ UnicodeString::getTerminatedBuffer() {
 // setTo() analogous to the readonly-aliasing constructor with the same signature
 UnicodeString &
 UnicodeString::setTo(UBool isTerminated,
-                     const UChar *text,
+                     ConstChar16Ptr textPtr,
                      int32_t textLength)
 {
   if(fUnion.fFields.fLengthAndFlags & kOpenGetBuffer) {
@@ -1266,6 +1266,7 @@ UnicodeString::setTo(UBool isTerminated,
     return *this;
   }
 
+  const UChar *text = textPtr;
   if(text == NULL) {
     // treat as an empty string, do not alias
     releaseArray();
index 427276efb26e993ce21ea862efd14ce441188168..892e0d7d58a4c42ac5a871a39948460666ed83af 100644 (file)
@@ -49,6 +49,13 @@ const char C_NEW_LINE[] = {'\n',0};
 #endif
 U_CDECL_END
 
+inline const UChar *constUCharPtr(const char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<const UChar *>(p);
+}
+
 U_CDECL_BEGIN
 static void U_CALLCONV TestStream(void)
 {
@@ -106,12 +113,12 @@ static void U_CALLCONV TestStream(void)
 
     inTestStream >> inStr >> inStr2;
     if (inStr.compare(thisMu) != 0) {
-        u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
+        u_austrncpy(inStrC, constUCharPtr(inStr.getBuffer()), inStr.length());
         inStrC[inStr.length()] = 0;
         log_err("Got: \"%s\", Expected: \"tHis\\u03BC\"\n", inStrC);
     }
     if (inStr2.compare(mu) != 0) {
-        u_austrncpy(inStrC, inStr.getBuffer(), inStr.length());
+        u_austrncpy(inStrC, constUCharPtr(inStr.getBuffer()), inStr.length());
         inStrC[inStr.length()] = 0;
         log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC);
     }
index ded0c7b609fdf6ec3d3d7a27e0c785c9e386ba07..d85341ac40c06d4a87e934e2cf5dbe537aff1eee 100644 (file)
@@ -11,6 +11,7 @@
 #include "unicode/datamap.h"
 #include "unicode/resbund.h"
 #include "hash.h"
+#include "toolutil.h"
 #include <stdlib.h>
 
 DataMap::~DataMap() {}
@@ -20,7 +21,7 @@ int32_t
 DataMap::utoi(const UnicodeString &s) const
 {
   char ch[256];
-  const UChar *u = s.getBuffer();
+  const UChar *u = constUCharPtr(s.getBuffer());
   int32_t len = s.length();
   u_UCharsToChars(u, ch, len);
   ch[len] = 0; /* include terminating \0 */
index 940db3b13c4fd4ec869d0def958e15f211400468..5c7c9c0a016601ff1ac54084d41ffae509e70cf3 100644 (file)
@@ -282,7 +282,7 @@ uint8_t Normalizer2DataBuilder::getCC(UChar32 c) const {
 
 static UBool isWellFormed(const UnicodeString &s) {
     UErrorCode errorCode=U_ZERO_ERROR;
-    u_strToUTF8(NULL, 0, NULL, s.getBuffer(), s.length(), &errorCode);
+    u_strToUTF8(NULL, 0, NULL, constUCharPtr(s.getBuffer()), s.length(), &errorCode);
     return U_SUCCESS(errorCode) || errorCode==U_BUFFER_OVERFLOW_ERROR;
 }
 
@@ -315,7 +315,7 @@ void Normalizer2DataBuilder::setRoundTripMapping(UChar32 c, const UnicodeString
                 (int)phase, (long)c);
         exit(U_INVALID_FORMAT_ERROR);
     }
-    int32_t numCP=u_countChar32(m.getBuffer(), m.length());
+    int32_t numCP=u_countChar32(constUCharPtr(m.getBuffer()), m.length());
     if(numCP!=2) {
         fprintf(stderr,
                 "error in gennorm2 phase %d: "
@@ -452,7 +452,7 @@ Normalizer2DataBuilder::decompose(UChar32 start, UChar32 end, uint32_t value) {
         Norm &norm=norms[value];
         const UnicodeString &m=*norm.mapping;
         UnicodeString *decomposed=NULL;
-        const UChar *s=m.getBuffer();
+        const UChar *s=constUCharPtr(m.getBuffer());
         int32_t length=m.length();
         int32_t prev, i=0;
         UChar32 c;
@@ -607,7 +607,7 @@ Normalizer2DataBuilder::reorder(Norm *p, BuilderReorderingBuffer &buffer) {
     if(length>Normalizer2Impl::MAPPING_LENGTH_MASK) {
         return;  // writeMapping() will complain about it and print the code point.
     }
-    const UChar *s=m.getBuffer();
+    const UChar *s=constUCharPtr(m.getBuffer());
     int32_t i=0;
     UChar32 c;
     while(i<length) {
@@ -1209,7 +1209,7 @@ void Normalizer2DataBuilder::writeBinaryFile(const char *filename) {
     }
     udata_writeBlock(pData, indexes, sizeof(indexes));
     udata_writeBlock(pData, norm16TrieBytes.getAlias(), norm16TrieLength);
-    udata_writeUString(pData, extraData.getBuffer(), extraData.length());
+    udata_writeUString(pData, constUCharPtr(extraData.getBuffer()), extraData.length());
     udata_writeBlock(pData, smallFCD, sizeof(smallFCD));
     int32_t writtenSize=udata_finish(pData, errorCode);
     if(errorCode.isFailure()) {
index abeaf0e49fc99f294b2ef705fb7492b21484c770..2e04bbce21ef745a7a3536fb96f2a2d54bcc39ae 100644 (file)
@@ -271,7 +271,7 @@ StringBaseResource::StringBaseResource(SRBRoot *bundle, const char *tag, int8_t
         return;
     }
 
-    fString.setTo(value, len);
+    fString.setTo(ConstChar16Ptr(value), len);
     fString.getTerminatedBuffer();  // Some code relies on NUL-termination.
     if (U_SUCCESS(errorCode) && fString.isBogus()) {
         errorCode = U_MEMORY_ALLOCATION_ERROR;
index d5e0945da1217bafbd2f3616f1a8d418b85b0730..4ff892cb16dd34656656445240c47b8171619a28 100644 (file)
@@ -29,6 +29,7 @@
 #include "unicode/ustring.h"
 #include "cmemory.h"
 #include "cstring.h"
+#include "toolutil.h"
 #include "uhash.h"
 #include "unewdata.h"
 #include "uresdata.h"
@@ -304,7 +305,7 @@ public:
     StringBaseResource(int8_t type, const UChar *value, int32_t len, UErrorCode &errorCode);
     virtual ~StringBaseResource();
 
-    const UChar *getBuffer() const { return fString.getBuffer(); }
+    const UChar *getBuffer() const { return icu::constUCharPtr(fString.getBuffer()); }
     int32_t length() const { return fString.length(); }
 
     virtual void handlePreWrite(uint32_t *byteOffset);
index ba6ad86536293ab717fb6e4261fcb7249c3d12c3..3208f1d31a354c801802128e3e30f112f1dda376 100644 (file)
@@ -44,6 +44,7 @@
 #include "unicode/uchar.h"
 #include "ustr.h"
 #include "prscmnts.h"
+#include "toolutil.h"
 #include "unicode/unistr.h"
 #include "unicode/utf8.h"
 #include "unicode/utf16.h"
@@ -73,7 +74,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
     u_strToUTF8(NULL,
                 0,
                 &len,
-                outString.getBuffer(),
+                constUCharPtr(outString.getBuffer()),
                 outString.length(),
                 &status);
 
@@ -85,7 +86,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
     u_strToUTF8(dest,
                 len,
                 &len,
-                outString.getBuffer(),
+                constUCharPtr(outString.getBuffer()),
                 outString.length(),
                 &status);
 
index b9f6372440ed93a8412a6f3cb5957c18e8714f9c..b214e3785c229edaa7825963668b1f8f948cd466 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "unicode/unistr.h"
 #include "unicode/ustring.h"
+#include "toolutil.h"
 #include "util.h"
 #include "ucln.h"
 
@@ -117,7 +118,7 @@ U_CAPI int32_t
 udbg_stoi(const UnicodeString &s)
 {
     char ch[256];
-    const UChar *u = s.getBuffer();
+    const UChar *u = constUCharPtr(s.getBuffer());
     int32_t len = s.length();
     u_UCharsToChars(u, ch, len);
     ch[len] = 0; /* include terminating \0 */
@@ -129,7 +130,7 @@ U_CAPI double
 udbg_stod(const UnicodeString &s)
 {
     char ch[256];
-    const UChar *u = s.getBuffer();
+    const UChar *u = constUCharPtr(s.getBuffer());
     int32_t len = s.length();
     u_UCharsToChars(u, ch, len);
     ch[len] = 0; /* include terminating \0 */
index c57a1ef1767d2d7535d8ebbcf9ffac77c27c2ef5..9b1d91425e43c3980fbbce6991800fae259f4c41 100644 (file)
@@ -19,6 +19,7 @@
 #include "charstr.h"
 #include "cstring.h"
 #include "ppucd.h"
+#include "toolutil.h"
 #include "uassert.h"
 #include "uparse.h"
 
@@ -515,12 +516,12 @@ PreparsedUCD::parseCodePointRange(const char *s, UChar32 &start, UChar32 &end, U
 
 void
 PreparsedUCD::parseString(const char *s, UnicodeString &uni, UErrorCode &errorCode) {
-    UChar *buffer=uni.getBuffer(-1);
+    UChar *buffer=UCharPtr(uni.getBuffer(-1));
     int32_t length=u_parseString(s, buffer, uni.getCapacity(), NULL, &errorCode);
     if(errorCode==U_BUFFER_OVERFLOW_ERROR) {
         errorCode=U_ZERO_ERROR;
         uni.releaseBuffer(0);
-        buffer=uni.getBuffer(length);
+        buffer=UCharPtr(uni.getBuffer(length));
         length=u_parseString(s, buffer, uni.getCapacity(), NULL, &errorCode);
     }
     uni.releaseBuffer(length);
index 7ab665cf5068a6e62b22a176d0dbeda17daa157d..d9c57ef9eb51d60b01210f58417411e0c2e493f0 100644 (file)
 
 U_NAMESPACE_BEGIN
 
+inline const UChar *constUCharPtr(const char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<const UChar *>(p);
+}
+
+inline UChar *UCharPtr(char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<UChar *>(p);
+}
+
 /**
  * ErrorCode subclass for use in ICU command-line tools.
  * The destructor calls handleFailure() which calls exit(errorCode) when isFailure().
index 3fbf1a96f3c4e94050dd5502b2508fafdf64973d..baf9a73bc3ebf700e907e14adb5f05a9212ccb7d 100644 (file)
@@ -21,6 +21,7 @@
 #include "unicode/ucnv.h"
 #include "unicode/regex.h"
 #include "filestrm.h"
+#include "toolutil.h"
 #include "xmlparser.h"
 
 #if !UCONFIG_NO_REGULAR_EXPRESSIONS && !UCONFIG_NO_CONVERSION
@@ -209,7 +210,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) {
             goto exit;
         }
 
-        buffer=src.getBuffer(bytesLength);
+        buffer=UCharPtr(src.getBuffer(bytesLength));
         if(buffer==NULL) {
             // unexpected failure to reserve some string capacity
             errorCode=U_MEMORY_ALLOCATION_ERROR;
@@ -278,7 +279,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) {
         pb=bytes;
         for(;;) {
             length=src.length();
-            buffer=src.getBuffer(capacity);
+            buffer=UCharPtr(src.getBuffer(capacity));
             if(buffer==NULL) {
                 // unexpected failure to reserve some string capacity
                 errorCode=U_MEMORY_ALLOCATION_ERROR;