[llvm-commits] [llvm] r154944 - in /llvm/trunk: include/llvm/Support/Locale.h lib/Support/CMakeLists.txt lib/Support/Locale.cpp lib/Support/LocaleGeneric.inc lib/Support/LocaleWindows.inc lib/Support/LocaleXlocale.inc

Seth Cantrell seth.cantrell at gmail.com
Tue Apr 17 13:03:03 PDT 2012


Author: socantre
Date: Tue Apr 17 15:03:03 2012
New Revision: 154944

URL: http://llvm.org/viewvc/llvm-project?rev=154944&view=rev
Log:
platform support for counting column widths and checking isprint

Added:
    llvm/trunk/include/llvm/Support/Locale.h
    llvm/trunk/lib/Support/Locale.cpp
    llvm/trunk/lib/Support/LocaleGeneric.inc
    llvm/trunk/lib/Support/LocaleWindows.inc
    llvm/trunk/lib/Support/LocaleXlocale.inc
Modified:
    llvm/trunk/lib/Support/CMakeLists.txt

Added: llvm/trunk/include/llvm/Support/Locale.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Locale.h?rev=154944&view=auto
==============================================================================
--- llvm/trunk/include/llvm/Support/Locale.h (added)
+++ llvm/trunk/include/llvm/Support/Locale.h Tue Apr 17 15:03:03 2012
@@ -0,0 +1,17 @@
+#ifndef LLVM_SUPPORT_LOCALE
+#define LLVM_SUPPORT_LOCALE
+
+#include "llvm/ADT/StringRef.h"
+
+namespace llvm {
+namespace sys {
+namespace locale {
+
+int columnWidth(StringRef s);
+bool isPrint(int c);
+
+}
+}
+}
+
+#endif // LLVM_SUPPORT_LOCALE

Modified: llvm/trunk/lib/Support/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/CMakeLists.txt?rev=154944&r1=154943&r2=154944&view=diff
==============================================================================
--- llvm/trunk/lib/Support/CMakeLists.txt (original)
+++ llvm/trunk/lib/Support/CMakeLists.txt Tue Apr 17 15:03:03 2012
@@ -32,6 +32,7 @@
   IntrusiveRefCntPtr.cpp
   IsInf.cpp
   IsNAN.cpp
+  Locale.cpp
   LockFileManager.cpp
   ManagedStatic.cpp
   MemoryBuffer.cpp

Added: llvm/trunk/lib/Support/Locale.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Locale.cpp?rev=154944&view=auto
==============================================================================
--- llvm/trunk/lib/Support/Locale.cpp (added)
+++ llvm/trunk/lib/Support/Locale.cpp Tue Apr 17 15:03:03 2012
@@ -0,0 +1,10 @@
+#include "llvm/Support/Locale.h"
+#include "llvm/Config/config.h"
+
+#ifdef __APPLE__
+#include "LocaleXlocale.inc"
+#elif LLVM_ON_WIN32
+#include "LocaleWindows.inc"
+#else
+#include "LocaleGeneric.inc"
+#endif

Added: llvm/trunk/lib/Support/LocaleGeneric.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LocaleGeneric.inc?rev=154944&view=auto
==============================================================================
--- llvm/trunk/lib/Support/LocaleGeneric.inc (added)
+++ llvm/trunk/lib/Support/LocaleGeneric.inc Tue Apr 17 15:03:03 2012
@@ -0,0 +1,17 @@
+#include <cwctype>
+
+namespace llvm {
+namespace sys {
+namespace locale {
+
+int columnWidth(StringRef s) {
+    return s.size();
+}
+
+bool isPrint(int c) {
+    return iswprint(c);
+}
+
+}
+}
+}
\ No newline at end of file

Added: llvm/trunk/lib/Support/LocaleWindows.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LocaleWindows.inc?rev=154944&view=auto
==============================================================================
--- llvm/trunk/lib/Support/LocaleWindows.inc (added)
+++ llvm/trunk/lib/Support/LocaleWindows.inc Tue Apr 17 15:03:03 2012
@@ -0,0 +1,15 @@
+namespace llvm {
+namespace sys {
+namespace locale {
+
+int columnWidth(StringRef s) {
+    return s.size();
+}
+
+bool isPrint(int c) {
+    return ' ' <= c && c <= '~';
+}
+
+}
+}
+}
\ No newline at end of file

Added: llvm/trunk/lib/Support/LocaleXlocale.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LocaleXlocale.inc?rev=154944&view=auto
==============================================================================
--- llvm/trunk/lib/Support/LocaleXlocale.inc (added)
+++ llvm/trunk/lib/Support/LocaleXlocale.inc Tue Apr 17 15:03:03 2012
@@ -0,0 +1,61 @@
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/ManagedStatic.h"
+#include <cassert>
+#include <xlocale.h>
+
+
+namespace {
+  struct locale_holder {
+    locale_holder()
+    : l(newlocale(LC_CTYPE_MASK,"en_US.UTF-8",LC_GLOBAL_LOCALE))
+    {
+      assert(NULL!=l);
+    }
+    ~locale_holder() {
+      freelocale(l);
+    }
+
+    int mbswidth(llvm::SmallString<16> s) const {
+       // this implementation assumes no '\0' in s
+      assert(s.size()==strlen(s.c_str()));
+
+      size_t size = mbstowcs_l(NULL,s.c_str(),0,l);
+      assert(size>=0);
+      if (size==0)
+        return 0;
+      llvm::SmallVector<wchar_t,200> ws(size);
+      size = mbstowcs_l(&ws[0],s.c_str(),ws.size(),l);
+      assert(ws.size()==size);
+      return wcswidth_l(&ws[0],ws.size(),l);
+    }
+
+    int isprint(int c) const {
+      return iswprint_l(c,l);
+    }
+
+  private:
+
+    locale_t l;
+  };
+
+  llvm::ManagedStatic<locale_holder> l;
+}
+
+namespace llvm {
+namespace sys {
+namespace locale {
+
+int columnWidth(StringRef s) {
+  int width = l->mbswidth(s);
+  assert(width>=0);
+  return width;
+}
+
+bool isPrint(int c) {
+  return l->isprint(c);
+}
+
+}
+}
+}





More information about the llvm-commits mailing list