src/foundation/ZUnicodePriv.h

00001 /*  @(#) $Id: ZUnicodePriv.h,v 1.7 2006/07/23 22:00:42 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2004 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZUnicodePriv__
00026 #define __ZUnicodePriv__ 1
00027 #include "zconfig.h"
00028 
00029 namespace ZUnicode {
00030 
00031 // =================================================================================================
00032 #pragma mark -
00033 #pragma mark * ZUnicode::Functions_CountCU
00034 
00035 // Functions_CountCU::sCountCU will call through to the standard
00036 // string length functions for pointers, and for other iterator
00037 // types there's a definition in ZUnicodePrivB.h and explicit
00038 // instantiations in ZUnicode.cpp
00039 
00040 template <class I>
00041 struct Functions_CountCU_T
00042         {
00043         static size_t sCountCU(I iSource);
00044         };
00045 
00046 template <>
00047 struct Functions_CountCU_T<const UTF32*>
00048         {
00049         static size_t sCountCU(const UTF32* iString)
00050                 {
00051                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00052                         return std::string_char_traits<UTF32>::length(iString);
00053                 #else
00054                         return std::char_traits<UTF32>::length(iString);
00055                 #endif
00056                 }
00057         };
00058 
00059 template <>
00060 struct Functions_CountCU_T<const UTF16*>
00061         {
00062         static size_t sCountCU(const UTF16* iString)
00063                 {
00064                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00065                         return std::string_char_traits<UTF16>::length(iString);
00066                 #else
00067                         return std::char_traits<UTF16>::length(iString);
00068                 #endif
00069                 }
00070         };
00071 
00072 template <>
00073 struct Functions_CountCU_T<const UTF8*>
00074         {
00075         static size_t sCountCU(const UTF8* iString)
00076                 {
00077                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00078                         return std::string_char_traits<UTF8>::length(iString);
00079                 #else
00080                         return std::char_traits<UTF8>::length(iString);
00081                 #endif
00082                 }
00083         };
00084 
00085 template <>
00086 struct Functions_CountCU_T<UTF32*>
00087         {
00088         static size_t sCountCU(UTF32* iString)
00089                 {
00090                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00091                         return std::string_char_traits<UTF32>::length(iString);
00092                 #else
00093                         return std::char_traits<UTF32>::length(iString);
00094                 #endif
00095                 }
00096         };
00097 
00098 template <>
00099 struct Functions_CountCU_T<UTF16*>
00100         {
00101         static size_t sCountCU(UTF16* iString)
00102                 {
00103                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00104                         return std::string_char_traits<UTF16>::length(iString);
00105                 #else
00106                         return std::char_traits<UTF16>::length(iString);
00107                 #endif
00108                 }
00109         };
00110 
00111 template <>
00112 struct Functions_CountCU_T<UTF8*>
00113         {
00114         static size_t sCountCU(UTF8* iString)
00115                 {
00116                 #if defined(__GNUC__) && __GNUC__ == 2 && __GNUC_MINOR__ <= 9
00117                         return std::string_char_traits<UTF8>::length(iString);
00118                 #else
00119                         return std::char_traits<UTF8>::length(iString);
00120                 #endif
00121                 }
00122         };
00123 
00124 // =================================================================================================
00125 #pragma mark -
00126 #pragma mark * ZUnicode::Functions_Count
00127 
00128 template <class I>
00129 struct Functions_Count_T
00130         {
00131         static size_t sCountCP(I iSource);
00132         static void sCount(I iSource, size_t* oCountCU, size_t* oCountCP);
00133 
00134         static size_t sCPToCU(I iSource, size_t iCountCP);
00135         static size_t sCPToCU(I iSource, size_t iCountCU, size_t iCountCP, size_t* oCountCP);
00136         static size_t sCPToCU(I iSource, I iEnd, size_t iCountCP, size_t* oCountCP);
00137 
00138         static size_t sCUToCP(I iSource, size_t iCountCU);
00139         static size_t sCUToCP(I iSource, I iEnd);
00140         };
00141 
00142 // =================================================================================================
00143 #pragma mark -
00144 #pragma mark * ZUnicode::Functions_Read
00145 
00146 template <class I, typename C = typename std::iterator_traits<I>::value_type>
00147 struct Functions_Read_T
00148         {
00149         static void sAlign(I& ioCurrent);
00150         static void sAlign(I& ioCurrent, I iEnd);
00151 
00152         static void sInc(I& ioCurrent);
00153         static bool sInc(I& ioCurrent, I iEnd);
00154 
00155         static void sDec(I& ioCurrent);
00156         static bool sDec(I iStart, I& ioCurrent, I iEnd);
00157 
00158         static UTF32 sRead(I iCurrent);
00159         static bool sRead(I iCurrent, I iEnd, UTF32& oCP);
00160 
00161         static UTF32 sReadInc(I& ioCurrent);
00162         static bool sReadInc(I& ioCurrent, I iEnd, UTF32& oCP);
00163         static bool sReadInc(I& ioCurrent, I iEnd, UTF32& oCP, size_t& ioCountSkipped);
00164 
00165         static UTF32 sDecRead(I& ioCurrent);
00166         static bool sDecRead(I iStart, I& ioCurrent, I iEnd, UTF32& oCP);
00167         };
00168 
00169 // =================================================================================================
00170 #pragma mark -
00171 #pragma mark * ZUnicode::Functions_Write
00172 
00173 template <class I, typename C = typename std::iterator_traits<I>::value_type>
00174 struct Functions_Write_T
00175         {
00176         static bool sWrite(I iDest, I iEnd, UTF32 iCP);
00177         static bool sWriteInc(I& ioDest, I iEnd, UTF32 iCP);
00178         };
00179 
00180 // =================================================================================================
00181 #pragma mark -
00182 #pragma mark * ZUnicode::Functions_Convert
00183 
00184 template <class I>
00185 struct Functions_Convert_T
00186         {
00187         static string32 sAsUTF32(I iSource);
00188         static string32 sAsUTF32(I iSource, size_t iCountCU);
00189         static string32 sAsUTF32(I iSource, I iEnd);
00190 
00191         static string16 sAsUTF16(I iSource);
00192         static string16 sAsUTF16(I iSource, size_t iCountCU);
00193         static string16 sAsUTF16(I iSource, I iEnd);
00194 
00195         static string8 sAsUTF8(I iSource);
00196         static string8 sAsUTF8(I iSource, size_t iCountCU);
00197         static string8 sAsUTF8(I iSource, I iEnd);
00198         };
00199 
00200 } // namespace ZUnicode
00201 
00202 // =================================================================================================
00203 #pragma mark -
00204 #pragma mark * GCC workaround
00205 
00206 #if defined(__GNUC__) && __GNUC__ == 3 && __GNUC_MINOR__ >= 2
00207 // AG 2002-10-07. Workaround for the gcc 3.2+ STL's lack of a definition
00208 // for char_traits<X> where X is neither char nor wchar_t.
00209 
00210 template <class charT>
00211 inline void std::char_traits<charT>::assign(char_type& c1, const char_type& c2)
00212         {
00213         c1 = c2;
00214         }
00215 
00216 template <class charT>
00217 inline bool std::char_traits<charT>::eq(const char_type& c1, const char_type& c2)
00218         {
00219         return c1 == c2;
00220         }
00221 
00222 template <class charT>
00223 inline bool std::char_traits<charT>::lt(const char_type& c1, const char_type& c2)
00224         { 
00225         return c1 < c2;
00226         }
00227 
00228 template <class charT>
00229 int std::char_traits<charT>::compare(const char_type* s1, const char_type* s2, size_t n)
00230         {
00231         for (size_t i = 0; i < n; ++i, ++s1, ++s2)
00232                 {
00233                 if (lt(*s1, *s2))
00234                         return -1;
00235                 if (lt(*s2, *s1))
00236                         return 1;
00237                 }
00238         return 0;
00239         }
00240 
00241 template <class charT>
00242 size_t std::char_traits<charT>::length(const char_type* s)
00243         {
00244         size_t len = 0;
00245         while (!eq(*s, charT()))
00246                 {
00247                 ++s;
00248                 ++len;
00249                 }
00250         return len;
00251         }
00252 
00253 template <class charT>
00254 const typename std::char_traits<charT>::char_type*
00255 std::char_traits<charT>::find(const char_type* s, size_t n, const char_type& a)
00256         {
00257         if (n == 0)
00258                 return 0;
00259         const char_type* p = s;
00260         size_t i = 0;
00261         while (!eq(*p, a))
00262                 {
00263                 ++i;
00264                 if (i == n)
00265                         return 0;
00266                 ++p;
00267                 }
00268         return p;
00269         }
00270 
00271 template <class charT>
00272 typename std::char_traits<charT>::char_type*
00273 std::char_traits<charT>::move(char_type* s1, const char_type* s2, size_t n)
00274         {
00275         char_type* r = s1;
00276         if (s1 < s2)
00277                 {
00278                 for (size_t i = 0; i < n; ++i)
00279                         assign(*s1++, *s2++);
00280                 }
00281         else if (s2 < s1)
00282                 {
00283                 s1 += n;
00284                 s2 += n;
00285                 for (; n > 0; --n)
00286                         assign(*--s1, *--s2);
00287                 }
00288         return r;
00289         }
00290 
00291 template <class charT>
00292 inline typename std::char_traits<charT>::char_type*
00293 std::char_traits<charT>::copy(char_type* s1, const char_type* s2, size_t n)
00294         {
00295         char_type* r = s1;
00296         for (size_t i = 0; i < n; ++i)
00297                 assign(*s1++, *s2++);
00298         return r;
00299         }
00300 
00301 template <class charT>
00302 inline typename std::char_traits<charT>::char_type*
00303 std::char_traits<charT>::assign(char_type* s, size_t n, char_type a)
00304         {
00305         char_type* r = s;
00306         for (size_t i = 0; i < n; ++i)
00307                 assign(*s++, a);
00308         return r;
00309         }
00310 
00311 template <class charT>
00312 inline typename std::char_traits<charT>::int_type
00313 std::char_traits<charT>::not_eof(const int_type& c)
00314         {
00315         return eq_int_type(c, eof()) ? ~eof() : c;
00316         }
00317 
00318 template <class charT>
00319 inline typename std::char_traits<charT>::char_type
00320 std::char_traits<charT>::to_char_type(const int_type& c)
00321         {
00322         return charT(c);
00323         }
00324 
00325 template <class charT>
00326 inline typename std::char_traits<charT>::int_type
00327 std::char_traits<charT>::to_int_type(const char_type& c)
00328         {
00329         return int_type(c);
00330         }
00331 
00332 template <class charT>
00333 inline bool std::char_traits<charT>::eq_int_type(const int_type& c1, const int_type& c2)
00334         {
00335         return c1 == c2;
00336         }
00337 
00338 template <class charT>
00339 inline typename std::char_traits<charT>::int_type std::char_traits<charT>::eof()
00340         {
00341         return EOF;
00342         }
00343 
00344 #endif // defined(__GNUC__) && __GNUC__ >= 3 && __GNUC_MINOR__ >= 2
00345 
00346 #endif// __ZUnicodePriv__

Generated on Thu Jul 26 11:21:52 2007 for ZooLib by  doxygen 1.4.7