Add a check for wchar support
authorMichael Lee <mzlee@fb.com>
Fri, 15 Jul 2016 00:37:31 +0000 (17:37 -0700)
committerFacebook Github Bot 9 <facebook-github-bot-9-bot@fb.com>
Fri, 15 Jul 2016 00:38:25 +0000 (17:38 -0700)
Summary:
Not all libc's are the same. Some support wchar_t and some
don't do a great job...

Reviewed By: Orvid

Differential Revision: D3565016

fbshipit-source-id: 91da4f1332e30bdb20a93d0a26a0445d5eadd1b7

folly/configure.ac
folly/test/FBStringTest.cpp

index 48a99b5b69450adcb7c44c483c2eb15c1f3a08e4..51ec0dd67a2833a49bc5833b58ef77e35bfc1fa4 100644 (file)
@@ -340,6 +340,27 @@ if test "$folly_cv_prog_cc_weak_symbols" = yes; then
             [Define to 1 if the linker supports weak symbols.])
 fi
 
+
+# Figure out if we support wchar well
+AC_CACHE_CHECK(
+  [for wchar support],
+  [folly_cv_prog_cc_wchar_support],
+  [AC_RUN_IFELSE(
+    [AC_LANG_SOURCE[
+      #include <cstddef>
+      #include <cwchar>
+
+      int main(int argc, char** argv) {
+        return wcstol(L"01", nullptr, 10) == 1 ? 0 : 1;
+      }
+    ]],
+    [folly_cv_prog_cc_wchar_support=yes],
+    [folly_cv_prog_cc_wchar_support=no])])
+
+if test "$folly_cv_prog_cc_wchar_support" = "yes"; then
+  AC_DEFINE([HAVE_WCHAR_SUPPORT], [1], [Define to 1 if the libc supports wchar well])
+fi
+
 # Figure out whether the architecture supports unaligned accesses
 AC_CACHE_CHECK(
   [for unaligned access support],
index 1bc36391f88eb7a2afde2eabe9095c9f94407aa0..8767b28ff61831cbfa77774901cadd818e70edab 100644 (file)
@@ -984,9 +984,7 @@ TEST(FBString, testAllClauses) {
   EXPECT_TRUE(1) << "Starting with seed: " << seed;
   std::string r;
   folly::fbstring c;
-#ifndef __ANDROID__
-  // Disabled on Android: wchar support is not recommended and does not
-  // always behave as expected
+#if FOLLY_HAVE_WCHAR_SUPPORT
   std::wstring wr;
   folly::basic_fbstring<wchar_t> wc;
 #endif
@@ -1001,7 +999,7 @@ TEST(FBString, testAllClauses) {
       randomString(&r);
       c = r;
       EXPECT_EQ(c, r);
-#ifndef __ANDROID__
+#if FOLLY_HAVE_WCHAR_SUPPORT
       wr = std::wstring(r.begin(), r.end());
       wc = folly::basic_fbstring<wchar_t>(wr.c_str());
 #endif
@@ -1014,7 +1012,7 @@ TEST(FBString, testAllClauses) {
         << "Lengths: " << r.size() << " vs. " << c.size()
         << "\nReference: '" << r << "'"
         << "\nActual:    '" << c.data()[0] << "'";
-#ifndef __ANDROID__
+#if FOLLY_HAVE_WCHAR_SUPPORT
       rng = RandomT(localSeed);
       f_wfbstring(wc);
       int wret = wcslen(wc.c_str());
@@ -1287,6 +1285,7 @@ TEST(FBString, testHash) {
   EXPECT_NE(hashfunc(a), hashfunc(b));
 }
 
+#if FOLLY_HAVE_WCHAR_SUPPORT
 TEST(FBString, testHashChar16) {
   using u16fbstring = basic_fbstring<char16_t>;
   u16fbstring a;
@@ -1298,6 +1297,7 @@ TEST(FBString, testHashChar16) {
   std::hash<u16fbstring> hashfunc;
   EXPECT_NE(hashfunc(a), hashfunc(b));
 }
+#endif
 
 TEST(FBString, testFrontBack) {
   fbstring str("hello");