]> granicus.if.org Git - icu/commitdiff
ICU-12992 add OldUChar type to match ICU 58 UChar; add public conversion functions...
authorMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Mar 2017 22:08:12 +0000 (22:08 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Wed, 8 Mar 2017 22:08:12 +0000 (22:08 +0000)
X-SVN-Rev: 39747

14 files changed:
icu4c/source/common/uinvchar.h
icu4c/source/common/unicode/char16ptr.h
icu4c/source/common/unicode/normlzr.h
icu4c/source/common/unicode/umachine.h
icu4c/source/common/unicode/unistr.h
icu4c/source/test/iotest/stream.cpp
icu4c/source/tools/ctestfw/datamap.cpp
icu4c/source/tools/gennorm2/n2builder.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 2a960bdfca4438948386d0b7df241162649ea2ce..c4f9f88b9ad32ffa6e89ff220aeb58bf9ceaa7f9 100644 (file)
@@ -64,11 +64,7 @@ uprv_isInvariantUString(const UChar *s, int32_t length);
  */
 U_INTERNAL inline UBool U_EXPORT2
 uprv_isInvariantUnicodeString(const icu::UnicodeString &s) {
-    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());
+    return uprv_isInvariantUString(icu::toUCharPtr(s.getBuffer()), s.length());
 }
 
 #endif  /* __cplusplus */
index d484cfdf8ac2d0595a9c25b749e0baaccb1d2333..2e85aa71aba15f32e79b505f220f5d5b5a3dc498 100644 (file)
@@ -14,6 +14,7 @@
  * \file
  * \brief C++ API: char16_t pointer wrappers with
  *        implicit conversion to/from bit-compatible raw pointer types.
+ *        Also conversion functions from char16_t * to UChar * and OldUChar *.
  */
 
 U_NAMESPACE_BEGIN
@@ -227,6 +228,62 @@ const char16_t *ConstChar16Ptr::get() const { return u.cp; }
 
 #endif
 
+/**
+ * Converts from const char16_t * to const UChar *.
+ * Includes an aliasing barrier if available.
+ * @param p pointer
+ * @return p as const UChar *
+ * @draft ICU 59
+ */
+inline U_COMMON_API const UChar *toUCharPtr(const char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<const UChar *>(p);
+}
+
+/**
+ * Converts from char16_t * to UChar *.
+ * Includes an aliasing barrier if available.
+ * @param p pointer
+ * @return p as UChar *
+ * @draft ICU 59
+ */
+inline U_COMMON_API UChar *toUCharPtr(char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<UChar *>(p);
+}
+
+/**
+ * Converts from const char16_t * to const OldUChar *.
+ * Includes an aliasing barrier if available.
+ * @param p pointer
+ * @return p as const OldUChar *
+ * @draft ICU 59
+ */
+inline U_COMMON_API const OldUChar *toOldUCharPtr(const char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<const OldUChar *>(p);
+}
+
+/**
+ * Converts from char16_t * to OldUChar *.
+ * Includes an aliasing barrier if available.
+ * @param p pointer
+ * @return p as OldUChar *
+ * @draft ICU 59
+ */
+inline U_COMMON_API OldUChar *toOldUCharPtr(char16_t *p) {
+#ifdef U_ALIASING_BARRIER
+    U_ALIASING_BARRIER(p);
+#endif
+    return reinterpret_cast<OldUChar *>(p);
+}
+
 U_NAMESPACE_END
 
 #endif  // __CHAR16PTR_H__
index 02ef13f2fd3e40069fbe106c55ae3af576c4762f..0141058fb0014a1928ac2b094548e9c456f1eb62 100644 (file)
@@ -795,15 +795,9 @@ inline int32_t
 Normalizer::compare(const UnicodeString &s1, const UnicodeString &s2,
                     uint32_t options,
                     UErrorCode &errorCode) {
-  const char16_t *p1 = s1.getBuffer();
-  const char16_t *p2 = s2.getBuffer();
-#ifdef U_ALIASING_BARRIER
-  U_ALIASING_BARRIER(p1);
-  U_ALIASING_BARRIER(p2);
-#endif
   // all argument checking is done in unorm_compare
-  return unorm_compare(reinterpret_cast<const UChar *>(p1), s1.length(),
-                       reinterpret_cast<const UChar *>(p2), s2.length(),
+  return unorm_compare(toUCharPtr(s1.getBuffer()), s1.length(),
+                       toUCharPtr(s2.getBuffer()), s2.length(),
                        options,
                        &errorCode);
 }
index 4c8b5c1f1e2a4519f0cc07d611cb99b883484c3e..8c96c15cfb65bf08df8a853ba0ecd07054189c83 100644 (file)
@@ -313,6 +313,7 @@ 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.
@@ -321,6 +322,7 @@ typedef int8_t UBool;
 #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.
@@ -333,6 +335,33 @@ typedef int8_t UBool;
     typedef uint16_t UChar;
 #endif
 
+/**
+ * \var OldUChar
+ * Default ICU 58 definition of UChar.
+ * A base type for UTF-16 code units and pointers.
+ * Unsigned 16-bit integer.
+ *
+ * Define OldUChar to be wchar_t if that is 16 bits wide.
+ * If wchar_t is not 16 bits wide, then define UChar to be uint16_t.
+ *
+ * This makes the definition of OldUChar platform-dependent
+ * but allows direct string type compatibility with platforms with
+ * 16-bit wchar_t types.
+ *
+ * This is how UChar was defined in ICU 58, for transition convenience.
+ * Exception: ICU 58 UChar was defined to UCHAR_TYPE if that macro was defined.
+ * The current UChar responds to UCHAR_TYPE but OldUChar does not.
+ *
+ * @draft ICU 59
+ */
+#if U_SIZEOF_WCHAR_T==2
+    typedef wchar_t OldUChar;
+#elif defined(__CHAR16_TYPE__)
+    typedef __CHAR16_TYPE__ OldUChar;
+#else
+    typedef uint16_t OldUChar;
+#endif
+
 /**
  * Define UChar32 as a type for single Unicode code points.
  * UChar32 is a signed 32-bit integer (same as int32_t).
index 835e64ff7740bc2c46e9778bdf4a8f53f98cba2a..2aa5d8e3a34a448765b1c7b728a9cedef1f3d06a 100644 (file)
@@ -3495,13 +3495,6 @@ 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*).
@@ -4367,7 +4360,7 @@ UnicodeString::startsWith(const UnicodeString& srcText,
 inline UBool
 UnicodeString::startsWith(ConstChar16Ptr srcChars, int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(constUCharPtr(srcChars));
+    srcLength = u_strlen(toUCharPtr(srcChars));
   }
   return doCompare(0, srcLength, srcChars, 0, srcLength) == 0;
 }
@@ -4375,7 +4368,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(constUCharPtr(srcChars));
+    srcLength = u_strlen(toUCharPtr(srcChars));
   }
   return doCompare(0, srcLength, srcChars, srcStart, srcLength) == 0;
 }
@@ -4398,7 +4391,7 @@ inline UBool
 UnicodeString::endsWith(ConstChar16Ptr srcChars,
             int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(constUCharPtr(srcChars));
+    srcLength = u_strlen(toUCharPtr(srcChars));
   }
   return doCompare(length() - srcLength, srcLength,
                    srcChars, 0, srcLength) == 0;
@@ -4409,7 +4402,7 @@ UnicodeString::endsWith(const char16_t *srcChars,
             int32_t srcStart,
             int32_t srcLength) const {
   if(srcLength < 0) {
-    srcLength = u_strlen(constUCharPtr(srcChars + srcStart));
+    srcLength = u_strlen(toUCharPtr(srcChars + srcStart));
   }
   return doCompare(length() - srcLength, srcLength,
                    srcChars, srcStart, srcLength) == 0;
index 892e0d7d58a4c42ac5a871a39948460666ed83af..424d4e33af5148df9a98b7923c7e9215785a9fec 100644 (file)
@@ -49,13 +49,6 @@ 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)
 {
@@ -113,12 +106,12 @@ static void U_CALLCONV TestStream(void)
 
     inTestStream >> inStr >> inStr2;
     if (inStr.compare(thisMu) != 0) {
-        u_austrncpy(inStrC, constUCharPtr(inStr.getBuffer()), inStr.length());
+        u_austrncpy(inStrC, toUCharPtr(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, constUCharPtr(inStr.getBuffer()), inStr.length());
+        u_austrncpy(inStrC, toUCharPtr(inStr.getBuffer()), inStr.length());
         inStrC[inStr.length()] = 0;
         log_err("Got: \"%s\", Expected: \"mu\"\n", inStrC);
     }
index d85341ac40c06d4a87e934e2cf5dbe537aff1eee..96241a0657bf0ef3f767c3367c7d365496e9be5f 100644 (file)
@@ -10,8 +10,8 @@
 
 #include "unicode/datamap.h"
 #include "unicode/resbund.h"
+#include "unicode/unistr.h"
 #include "hash.h"
-#include "toolutil.h"
 #include <stdlib.h>
 
 DataMap::~DataMap() {}
@@ -21,7 +21,7 @@ int32_t
 DataMap::utoi(const UnicodeString &s) const
 {
   char ch[256];
-  const UChar *u = constUCharPtr(s.getBuffer());
+  const UChar *u = toUCharPtr(s.getBuffer());
   int32_t len = s.length();
   u_UCharsToChars(u, ch, len);
   ch[len] = 0; /* include terminating \0 */
index 5c7c9c0a016601ff1ac54084d41ffae509e70cf3..98b101667431bbc54ecad1aefb7670c5012dcf3f 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, constUCharPtr(s.getBuffer()), s.length(), &errorCode);
+    u_strToUTF8(NULL, 0, NULL, toUCharPtr(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(constUCharPtr(m.getBuffer()), m.length());
+    int32_t numCP=u_countChar32(toUCharPtr(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=constUCharPtr(m.getBuffer());
+        const UChar *s=toUCharPtr(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=constUCharPtr(m.getBuffer());
+    const UChar *s=toUCharPtr(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, constUCharPtr(extraData.getBuffer()), extraData.length());
+    udata_writeUString(pData, toUCharPtr(extraData.getBuffer()), extraData.length());
     udata_writeBlock(pData, smallFCD, sizeof(smallFCD));
     int32_t writtenSize=udata_finish(pData, errorCode);
     if(errorCode.isFailure()) {
index 4ff892cb16dd34656656445240c47b8171619a28..ee1ecf3d05ff00afeaf73b5170e42a524a96664a 100644 (file)
@@ -29,7 +29,6 @@
 #include "unicode/ustring.h"
 #include "cmemory.h"
 #include "cstring.h"
-#include "toolutil.h"
 #include "uhash.h"
 #include "unewdata.h"
 #include "uresdata.h"
@@ -305,7 +304,7 @@ public:
     StringBaseResource(int8_t type, const UChar *value, int32_t len, UErrorCode &errorCode);
     virtual ~StringBaseResource();
 
-    const UChar *getBuffer() const { return icu::constUCharPtr(fString.getBuffer()); }
+    const UChar *getBuffer() const { return icu::toUCharPtr(fString.getBuffer()); }
     int32_t length() const { return fString.length(); }
 
     virtual void handlePreWrite(uint32_t *byteOffset);
index 3208f1d31a354c801802128e3e30f112f1dda376..2bfcfebf9efd70884880a0ab7272aa1e34c4a9d5 100644 (file)
@@ -44,7 +44,6 @@
 #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"
@@ -74,7 +73,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
     u_strToUTF8(NULL,
                 0,
                 &len,
-                constUCharPtr(outString.getBuffer()),
+                toUCharPtr(outString.getBuffer()),
                 outString.length(),
                 &status);
 
@@ -86,7 +85,7 @@ static int32_t write_utf8_file(FileStream* fileStream, UnicodeString outString)
     u_strToUTF8(dest,
                 len,
                 &len,
-                constUCharPtr(outString.getBuffer()),
+                toUCharPtr(outString.getBuffer()),
                 outString.length(),
                 &status);
 
index b214e3785c229edaa7825963668b1f8f948cd466..a5a3f518bb67f1bd505903986f647696c4dd5f2b 100644 (file)
@@ -13,7 +13,6 @@
 
 #include "unicode/unistr.h"
 #include "unicode/ustring.h"
-#include "toolutil.h"
 #include "util.h"
 #include "ucln.h"
 
@@ -118,7 +117,7 @@ U_CAPI int32_t
 udbg_stoi(const UnicodeString &s)
 {
     char ch[256];
-    const UChar *u = constUCharPtr(s.getBuffer());
+    const UChar *u = toUCharPtr(s.getBuffer());
     int32_t len = s.length();
     u_UCharsToChars(u, ch, len);
     ch[len] = 0; /* include terminating \0 */
@@ -130,7 +129,7 @@ U_CAPI double
 udbg_stod(const UnicodeString &s)
 {
     char ch[256];
-    const UChar *u = constUCharPtr(s.getBuffer());
+    const UChar *u = toUCharPtr(s.getBuffer());
     int32_t len = s.length();
     u_UCharsToChars(u, ch, len);
     ch[len] = 0; /* include terminating \0 */
index 9b1d91425e43c3980fbbce6991800fae259f4c41..cccde81c7abfaca0a72eeb261e835cb20560860c 100644 (file)
@@ -19,7 +19,6 @@
 #include "charstr.h"
 #include "cstring.h"
 #include "ppucd.h"
-#include "toolutil.h"
 #include "uassert.h"
 #include "uparse.h"
 
@@ -516,12 +515,12 @@ PreparsedUCD::parseCodePointRange(const char *s, UChar32 &start, UChar32 &end, U
 
 void
 PreparsedUCD::parseString(const char *s, UnicodeString &uni, UErrorCode &errorCode) {
-    UChar *buffer=UCharPtr(uni.getBuffer(-1));
+    UChar *buffer=toUCharPtr(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=UCharPtr(uni.getBuffer(length));
+        buffer=toUCharPtr(uni.getBuffer(length));
         length=u_parseString(s, buffer, uni.getCapacity(), NULL, &errorCode);
     }
     uni.releaseBuffer(length);
index d9c57ef9eb51d60b01210f58417411e0c2e493f0..7ab665cf5068a6e62b22a176d0dbeda17daa157d 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 baf9a73bc3ebf700e907e14adb5f05a9212ccb7d..1ca111c07567b63fecf6a3e572356240be6894f3 100644 (file)
@@ -21,7 +21,6 @@
 #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
@@ -210,7 +209,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) {
             goto exit;
         }
 
-        buffer=UCharPtr(src.getBuffer(bytesLength));
+        buffer=toUCharPtr(src.getBuffer(bytesLength));
         if(buffer==NULL) {
             // unexpected failure to reserve some string capacity
             errorCode=U_MEMORY_ALLOCATION_ERROR;
@@ -279,7 +278,7 @@ UXMLParser::parseFile(const char *filename, UErrorCode &errorCode) {
         pb=bytes;
         for(;;) {
             length=src.length();
-            buffer=UCharPtr(src.getBuffer(capacity));
+            buffer=toUCharPtr(src.getBuffer(capacity));
             if(buffer==NULL) {
                 // unexpected failure to reserve some string capacity
                 errorCode=U_MEMORY_ALLOCATION_ERROR;