summaryrefslogtreecommitdiff
path: root/lib/Support/LocaleXlocale.inc
diff options
context:
space:
mode:
authorSeth Cantrell <seth.cantrell@gmail.com>2012-04-17 20:03:03 +0000
committerSeth Cantrell <seth.cantrell@gmail.com>2012-04-17 20:03:03 +0000
commitfdc97cd2c332e5544bd99cefa4070329cc8bf668 (patch)
tree127008e9d8238c7b7548b9998727366f63afecb3 /lib/Support/LocaleXlocale.inc
parentb90757078f6c270f0b4138dde10a02bd3b64750f (diff)
downloadllvm-fdc97cd2c332e5544bd99cefa4070329cc8bf668.tar.gz
llvm-fdc97cd2c332e5544bd99cefa4070329cc8bf668.tar.bz2
llvm-fdc97cd2c332e5544bd99cefa4070329cc8bf668.tar.xz
platform support for counting column widths and checking isprint
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154944 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/LocaleXlocale.inc')
-rw-r--r--lib/Support/LocaleXlocale.inc61
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/Support/LocaleXlocale.inc b/lib/Support/LocaleXlocale.inc
new file mode 100644
index 0000000000..c3f3e4e092
--- /dev/null
+++ b/lib/Support/LocaleXlocale.inc
@@ -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);
+}
+
+}
+}
+}