New module 'striconv'.
[gnulib.git] / lib / mbchar.h
index 6bb531e..bf31027 100644 (file)
@@ -1,5 +1,5 @@
 /* Multibyte character data type.
-   Copyright (C) 2001, 2005 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2005-2006 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #include <time.h>
 #include <wchar.h>
 
-#include <wctype.h>
+/* BeOS 5 has the functions but no <wctype.h>.  */
+#if HAVE_WCTYPE_H
+# include <wctype.h>
+#endif
+/* FreeBSD 4.4 to 4.11 has <wctype.h> but lacks the functions.
+   Assume all 12 functions are implemented the same way, or not at all.  */
+#if !defined iswalnum && !HAVE_ISWCNTRL
+static inline int
+iswalnum (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? (wc >= '0' && wc <= '9') || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z')
+         : 0);
+}
+# define iswalnum iswalnum
+#endif
+#if !defined iswalpha && !HAVE_ISWCNTRL
+static inline int
+iswalpha (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? (wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'
+         : 0);
+}
+# define iswalpha iswalpha
+#endif
+#if !defined iswblank && !HAVE_ISWCNTRL
+static inline int
+iswblank (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc == ' ' || wc == '\t'
+         : 0);
+}
+# define iswblank iswblank
+#endif
+#if !defined iswcntrl && !HAVE_ISWCNTRL
+static inline int
+iswcntrl (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? (wc & ~0x1f) == 0 || wc == 0x7f
+         : 0);
+}
+# define iswcntrl iswcntrl
+#endif
+#if !defined iswdigit && !HAVE_ISWCNTRL
+static inline int
+iswdigit (wint_t wc)
+{
+  return (wc >= '0' && wc <= '9');
+}
+# define iswdigit iswdigit
+#endif
+#if !defined iswgraph && !HAVE_ISWCNTRL
+static inline int
+iswgraph (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc >= '!' && wc <= '~'
+         : 1);
+}
+# define iswgraph iswgraph
+#endif
+#if !defined iswlower && !HAVE_ISWCNTRL
+static inline int
+iswlower (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc >= 'a' && wc <= 'z'
+         : 0);
+}
+# define iswlower iswlower
+#endif
+#if !defined iswprint && !HAVE_ISWCNTRL
+static inline int
+iswprint (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc >= ' ' && wc <= '~'
+         : 1);
+}
+# define iswprint iswprint
+#endif
+#if !defined iswpunct && !HAVE_ISWCNTRL
+static inline int
+iswpunct (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc >= '!' && wc <= '~'
+           && !((wc >= '0' && wc <= '9')
+                || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'Z'))
+         : 1);
+}
+# define iswpunct iswpunct
+#endif
+#if !defined iswspace && !HAVE_ISWCNTRL
+static inline int
+iswspace (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc == ' ' || wc == '\t'
+           || wc == '\n' || wc == '\v' || wc == '\f' || wc == '\r'
+         : 0);
+}
+# define iswspace iswspace
+#endif
+#if !defined iswupper && !HAVE_ISWCNTRL
+static inline int
+iswupper (wint_t wc)
+{
+  return (wc >= 0 && wc < 128
+         ? wc >= 'A' && wc <= 'Z'
+         : 0);
+}
+# define iswupper iswupper
+#endif
+#if !defined iswxdigit && !HAVE_ISWCNTRL
+static inline int
+iswxdigit (wint_t wc)
+{
+  return (wc >= '0' && wc <= '9') || ((wc & ~0x20) >= 'A' && (wc & ~0x20) <= 'F');
+}
+# define iswxdigit iswxdigit
+#endif
+
+#include "wcwidth.h"
 
 #define MBCHAR_BUF_SIZE 24
 
@@ -182,21 +308,29 @@ typedef struct mbchar mbchar_t;
 #define mb_iseq(mbc, sc) ((mbc).wc_valid && (mbc).wc == (sc))
 #define mb_isnul(mbc) ((mbc).wc_valid && (mbc).wc == 0)
 #define mb_cmp(mbc1, mbc2) \
-  ((mbc1).wc_valid && (mbc2).wc_valid                                  \
-   ? (int) (mbc1).wc - (int) (mbc2).wc                                 \
-   : (mbc1).bytes == (mbc2).bytes                                      \
-     ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                   \
-     : (mbc1).bytes < (mbc2).bytes                                     \
-       ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1)  \
-       : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1))
+  ((mbc1).wc_valid                                                     \
+   ? ((mbc2).wc_valid                                                  \
+      ? (int) (mbc1).wc - (int) (mbc2).wc                              \
+      : -1)                                                            \
+   : ((mbc2).wc_valid                                                  \
+      ? 1                                                              \
+      : (mbc1).bytes == (mbc2).bytes                                   \
+        ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                        \
+        : (mbc1).bytes < (mbc2).bytes                                  \
+          ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
+          : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
 #define mb_casecmp(mbc1, mbc2) \
-  ((mbc1).wc_valid && (mbc2).wc_valid                                  \
-   ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc)           \
-   : (mbc1).bytes == (mbc2).bytes                                      \
-     ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                   \
-     : (mbc1).bytes < (mbc2).bytes                                     \
-       ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1)  \
-       : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1))
+  ((mbc1).wc_valid                                                     \
+   ? ((mbc2).wc_valid                                                  \
+      ? (int) towlower ((mbc1).wc) - (int) towlower ((mbc2).wc)                \
+      : -1)                                                            \
+   : ((mbc2).wc_valid                                                  \
+      ? 1                                                              \
+      : (mbc1).bytes == (mbc2).bytes                                   \
+        ? memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes)                        \
+        : (mbc1).bytes < (mbc2).bytes                                  \
+          ? (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc1).bytes) > 0 ? 1 : -1) \
+          : (memcmp ((mbc1).ptr, (mbc2).ptr, (mbc2).bytes) >= 0 ? 1 : -1)))
 #define mb_equal(mbc1, mbc2) \
   ((mbc1).wc_valid && (mbc2).wc_valid                                  \
    ? (mbc1).wc == (mbc2).wc                                            \