[cfe-commits] [libcxx] r134781 - in /libcxx/trunk: include/__locale include/locale src/locale.cpp

Sean Hunt scshunt at csclub.uwaterloo.ca
Fri Jul 8 17:56:23 PDT 2011


Author: coppro
Date: Fri Jul  8 19:56:23 2011
New Revision: 134781

URL: http://llvm.org/viewvc/llvm-project?rev=134781&view=rev
Log:
Implement generalized table lookups for upper, lower, and character
traits.

To the best of my knowledge, this will not break the ABI for Apple.
However, it does introduce three publicly visible (although with
reserved name) functions that will fail to link against the just-shipped
Apple version of libc++. Since they are not used in any inline
functions, no actual breakage should occur.

If Howard doesn't want to put undefined functions (even internal ones)
into a header, they could be surrounded by additional conditional
compilation.

Modified:
    libcxx/trunk/include/__locale
    libcxx/trunk/include/locale
    libcxx/trunk/src/locale.cpp

Modified: libcxx/trunk/include/__locale
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__locale?rev=134781&r1=134780&r2=134781&view=diff
==============================================================================
--- libcxx/trunk/include/__locale (original)
+++ libcxx/trunk/include/__locale Fri Jul  8 19:56:23 2011
@@ -302,7 +302,11 @@
 class _LIBCPP_VISIBLE ctype_base
 {
 public:
+#ifdef __GLIBC__
+    typedef unsigned short mask;
+#else
     typedef __uint32_t mask;
+#endif
 
 #if __APPLE__
     static const mask space  = _CTYPE_S;
@@ -537,6 +541,8 @@
 #endif
     _LIBCPP_ALWAYS_INLINE const mask* table() const  _NOEXCEPT {return __tab_;}
     static const mask* classic_table()  _NOEXCEPT;
+    static const int* __classic_upper_table() _NOEXCEPT;
+    static const int* __classic_lower_table() _NOEXCEPT;
 
 protected:
     ~ctype();

Modified: libcxx/trunk/include/locale
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/locale?rev=134781&r1=134780&r2=134781&view=diff
==============================================================================
--- libcxx/trunk/include/locale (original)
+++ libcxx/trunk/include/locale Fri Jul  8 19:56:23 2011
@@ -192,6 +192,9 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+// Get the C locale object
+locale_t __cloc();
+
 // OSX has nice foo_l() functions that let you turn off use of the global
 // locale.  Linux, not so much.  The following functions avoid the locale when
 // that's possible and otherwise do the wrong thing.  FIXME.

Modified: libcxx/trunk/src/locale.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/src/locale.cpp?rev=134781&r1=134780&r2=134781&view=diff
==============================================================================
--- libcxx/trunk/src/locale.cpp (original)
+++ libcxx/trunk/src/locale.cpp Fri Jul  8 19:56:23 2011
@@ -116,6 +116,18 @@
 
 _LIBCPP_BEGIN_NAMESPACE_STD
 
+locale_t __cloc() {
+  // In theory this could create a race condition. In practice
+  // the race condition is non-fatal since it will just create
+  // a little resource leak. Better approach would be appreciated.
+#ifdef __APPLE__
+  return 0;
+#else
+  static locale_t result = newlocale(LC_ALL_MASK, "C", 0);
+  return result;
+#endif
+}
+
 namespace {
 
 struct release
@@ -767,93 +779,64 @@
 bool
 ctype<wchar_t>::do_is(mask m, char_type c) const
 {
-#ifdef __APPLE__
-    return isascii(c) ? _DefaultRuneLocale.__runetype[c] & m : false;
-#else
-    return false;
-#endif
+    return isascii(c) ? ctype<char>::classic_table()[c] & m : false;
 }
 
 const wchar_t*
 ctype<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low, ++vec)
-        *vec = static_cast<mask>(isascii(*low) ? _DefaultRuneLocale.__runetype[*low] : 0);
+        *vec = static_cast<mask>(isascii(*low) ?
+                                   ctype<char>::classic_table()[*low] : 0);
     return low;
-#else
-    return NULL;
-#endif
 }
 
 const wchar_t*
 ctype<wchar_t>::do_scan_is(mask m, const char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        if (isascii(*low) && (_DefaultRuneLocale.__runetype[*low] & m))
+        if (isascii(*low) && (ctype<char>::classic_table()[*low] & m))
             break;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 const wchar_t*
 ctype<wchar_t>::do_scan_not(mask m, const char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        if (!(isascii(*low) && (_DefaultRuneLocale.__runetype[*low] & m)))
+        if (!(isascii(*low) && (ctype<char>::classic_table()[*low] & m)))
             break;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 wchar_t
 ctype<wchar_t>::do_toupper(char_type c) const
 {
-#ifdef __APPLE__
-    return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
-#else
-    return 0;
-#endif
+    return isascii(c) ? ctype<char>::__classic_upper_table()[c] : c;
 }
 
 const wchar_t*
 ctype<wchar_t>::do_toupper(char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
+        *low = isascii(*low) ? ctype<char>::__classic_upper_table()[*low]
+                             : *low;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 wchar_t
 ctype<wchar_t>::do_tolower(char_type c) const
 {
-#ifdef __APPLE__
-    return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
-#else
-    return 0;
-#endif
+    return isascii(c) ? ctype<char>::__classic_lower_table()[c] : c;
 }
 
 const wchar_t*
 ctype<wchar_t>::do_tolower(char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
+        *low = isascii(*low) ? ctype<char>::__classic_lower_table()[*low]
+                             : *low;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 wchar_t
@@ -898,10 +881,8 @@
       __tab_(tab),
       __del_(del)
 {
-#ifdef __APPLE__
-    if (__tab_ == 0)
-        __tab_ = _DefaultRuneLocale.__runetype;
-#endif
+  if (__tab_ == 0)
+      __tab_ = classic_table();
 }
 
 ctype<char>::~ctype()
@@ -913,45 +894,29 @@
 char
 ctype<char>::do_toupper(char_type c) const
 {
-#ifdef __APPLE__
-    return isascii(c) ? _DefaultRuneLocale.__mapupper[c] : c;
-#else
-    return 0;
-#endif
+    return isascii(c) ? __classic_upper_table()[c] : c;
 }
 
 const char*
 ctype<char>::do_toupper(char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        *low = isascii(*low) ? _DefaultRuneLocale.__mapupper[*low] : *low;
+        *low = isascii(*low) ? __classic_upper_table()[*low] : *low;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 char
 ctype<char>::do_tolower(char_type c) const
 {
-#ifdef __APPLE__
-    return isascii(c) ? _DefaultRuneLocale.__maplower[c] : c;
-#else
-    return 0;
-#endif
+    return isascii(c) ? __classic_lower_table()[c] : c;
 }
 
 const char*
 ctype<char>::do_tolower(char_type* low, const char_type* high) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low)
-        *low = isascii(*low) ? _DefaultRuneLocale.__maplower[*low] : *low;
+        *low = isascii(*low) ? __classic_lower_table()[*low] : *low;
     return low;
-#else
-    return NULL;
-#endif
 }
 
 char
@@ -992,6 +957,33 @@
 {
 #ifdef __APPLE__
     return _DefaultRuneLocale.__runetype;
+#elif defined(__GLIBC__)
+    return __cloc()->__ctype_b;
+// This is assumed to be safe.
+#else
+    return NULL;
+#endif
+}
+
+const int*
+ctype<char>::__classic_lower_table() _NOEXCEPT
+{
+#ifdef __APPLE__
+    return _DefaultRuneLocale.__maplower;
+#elif defined(__GLIBC__)
+    return __cloc()->__ctype_tolower;
+#else
+    return NULL;
+#endif
+}
+
+const int*
+ctype<char>::__classic_upper_table() _NOEXCEPT
+{
+#ifdef __APPLE__
+    return _DefaultRuneLocale.__mapupper;
+#elif defined(__GLIBC__)
+    return __cloc()->__ctype_toupper;
 #else
     return NULL;
 #endif
@@ -1092,11 +1084,10 @@
 const wchar_t*
 ctype_byname<wchar_t>::do_is(const char_type* low, const char_type* high, mask* vec) const
 {
-#ifdef __APPLE__
     for (; low != high; ++low, ++vec)
     {
         if (isascii(*low))
-            *vec = static_cast<mask>(_DefaultRuneLocale.__runetype[*low]);
+            *vec = static_cast<mask>(ctype<char>::classic_table()[*low]);
         else
         {
             *vec = 0;
@@ -1121,9 +1112,6 @@
         }
     }
     return low;
-#else
-    return NULL;
-#endif
 }
 
 const wchar_t*





More information about the cfe-commits mailing list