platform support for counting column widths and checking isprint
authorSeth Cantrell <seth.cantrell@gmail.com>
Tue, 17 Apr 2012 20:03:03 +0000 (20:03 +0000)
committerSeth Cantrell <seth.cantrell@gmail.com>
Tue, 17 Apr 2012 20:03:03 +0000 (20:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154944 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/Locale.h [new file with mode: 0644]
lib/Support/CMakeLists.txt
lib/Support/Locale.cpp [new file with mode: 0644]
lib/Support/LocaleGeneric.inc [new file with mode: 0644]
lib/Support/LocaleWindows.inc [new file with mode: 0644]
lib/Support/LocaleXlocale.inc [new file with mode: 0644]

diff --git a/include/llvm/Support/Locale.h b/include/llvm/Support/Locale.h
new file mode 100644 (file)
index 0000000..b0f1295
--- /dev/null
@@ -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
index fbbcf78397ee508ba07f6ec04428fefb5e635937..9103327dffad34422e288bbba77204615ea77cfa 100644 (file)
@@ -32,6 +32,7 @@ add_llvm_library(LLVMSupport
   IntrusiveRefCntPtr.cpp
   IsInf.cpp
   IsNAN.cpp
+  Locale.cpp
   LockFileManager.cpp
   ManagedStatic.cpp
   MemoryBuffer.cpp
diff --git a/lib/Support/Locale.cpp b/lib/Support/Locale.cpp
new file mode 100644 (file)
index 0000000..17b9b6c
--- /dev/null
@@ -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
diff --git a/lib/Support/LocaleGeneric.inc b/lib/Support/LocaleGeneric.inc
new file mode 100644 (file)
index 0000000..20df773
--- /dev/null
@@ -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
diff --git a/lib/Support/LocaleWindows.inc b/lib/Support/LocaleWindows.inc
new file mode 100644 (file)
index 0000000..6827ac1
--- /dev/null
@@ -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
diff --git a/lib/Support/LocaleXlocale.inc b/lib/Support/LocaleXlocale.inc
new file mode 100644 (file)
index 0000000..c3f3e4e
--- /dev/null
@@ -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);
+}
+
+}
+}
+}