Eina: Added Eina_Unicode.
authortasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 27 Jul 2010 08:22:20 +0000 (08:22 +0000)
committertasn <tasn@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Tue, 27 Jul 2010 08:22:20 +0000 (08:22 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@50532 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

configure.ac
src/include/Eina.h
src/include/Makefile.am
src/include/eina_config.h.in
src/include/eina_unicode.h [new file with mode: 0644]
src/lib/Makefile.am
src/lib/eina_unicode.c [new file with mode: 0644]

index 73c4dd3..8fa6a58 100644 (file)
@@ -296,7 +296,17 @@ AC_HEADER_TIME
 
 
 ### Checks for types
+AC_CHECK_HEADER([inttypes.h],
+               [EINA_CONFIGURE_HAVE_INTTYPES_H="#define EINA_HAVE_INTTYPES_H"])
+AC_SUBST(EINA_CONFIGURE_HAVE_INTTYPES_H)
 
+AC_CHECK_HEADER([stdint.h],
+               [EINA_CONFIGURE_HAVE_STDINT_H="#define EINA_HAVE_STDINT_H"])
+AC_SUBST(EINA_CONFIGURE_HAVE_STDINT_H)
+
+AC_CHECK_SIZEOF(wchar_t)
+EINA_SIZEOF_WCHAR_T=$ac_cv_sizeof_wchar_t
+AC_SUBST(EINA_SIZEOF_WCHAR_T)
 
 ### Checks for structures
 
index 152bde2..32071aa 100644 (file)
@@ -149,6 +149,7 @@ extern "C" {
 #include "eina_matrixsparse.h"
 #include "eina_str.h"
 #include "eina_strbuf.h"
+#include "eina_unicode.h"
 #include "eina_quadtree.h"
 
 #ifdef __cplusplus
index 6022d43..bb3b4b0 100644 (file)
@@ -46,6 +46,7 @@ eina_inline_tiler.x \
 eina_str.h \
 eina_inline_str.x \
 eina_strbuf.h \
+eina_unicode.h \
 eina_quadtree.h
 
 installed_mainheaderdir = $(includedir)/eina-@VMAJ@
index 4f0b3c3..56331bb 100644 (file)
 #endif
 @EINA_CONFIGURE_SAFETY_CHECKS@
 
+#ifdef EINA_HAVE_INTTYPES_H
+# undef EINA_HAVE_INTTYPES_H
+#endif
+@EINA_CONFIGURE_HAVE_INTTYPES_H@
+
+#ifdef EINA_HAVE_STDINT_H
+# undef EINA_HAVE_STDINT_H
+#endif
+@EINA_CONFIGURE_HAVE_STDINT_H@
+
+#ifdef EINA_SIZEOF_WCHAR_T
+# undef EINA_SIZEOF_WCHAR_T
+#endif
+#define EINA_SIZEOF_WCHAR_T @EINA_SIZEOF_WCHAR_T@
+
 #endif /* EINA_CONFIG_H_ */
diff --git a/src/include/eina_unicode.h b/src/include/eina_unicode.h
new file mode 100644 (file)
index 0000000..08192cd
--- /dev/null
@@ -0,0 +1,73 @@
+#ifndef EINA_UNICODE_H
+#define EINA_UNICODE_H
+
+/**
+ * @addtogroup Eina_Data_Types_Group Data Types
+ *
+ * @{
+ */
+/**
+ * @addtogroup Eina_Unicode_String Unicode String
+ *
+ * @brief These functions provide basic unicode string handling
+ *
+ * Eina_Unicode is a type that holds unicode codepoints.
+ *
+ * @{
+ */
+
+#include "eina_config.h"
+
+/**
+ * @typedef Eina_Unicode
+ * A type that holds Unicode codepoints.
+ */
+#if EINA_SIZEOF_WCHAR_T >= 4
+# include <wchar.h>
+typedef wchar_t            Eina_Unicode; 
+#elif defined(EINA_HAVE_INTTYPES_H)
+# include <inttypes.h>
+typedef uint32_t           Eina_Unicode;
+#elif defined(EINA_HAVE_STDINT_H)
+# include <stdint.h>
+typedef uint32_t           Eina_Unicode;
+#else
+/* Hope that int is big enough */
+typedef unsigned int       Eina_Unicode; 
+#endif
+
+#include <stdlib.h>
+
+#include "eina_types.h"
+
+extern const Eina_Unicode *EINA_UNICODE_EMPTY_STRING;
+
+EAPI size_t
+eina_unicode_strlen(const Eina_Unicode *ustr) EINA_ARG_NONNULL(1);
+
+EAPI Eina_Unicode *
+eina_unicode_strdup(const Eina_Unicode *text) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1) EINA_MALLOC;
+
+EAPI int
+eina_unicode_strcmp(const Eina_Unicode *a, const Eina_Unicode *b) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2);
+
+EAPI Eina_Unicode *
+eina_unicode_strcpy(Eina_Unicode *dest, const Eina_Unicode *source);
+
+EAPI Eina_Unicode *
+eina_unicode_strstr(const Eina_Unicode *haystack, const Eina_Unicode *needle) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1, 2); 
+
+EAPI Eina_Unicode *
+eina_unicode_strncpy(Eina_Unicode *dest, const Eina_Unicode *source, size_t n);
+
+EAPI Eina_Unicode *
+eina_unicode_escape(const Eina_Unicode *str);
+
+/**
+ * @}
+ */
+/**
+ * @}
+ */
+
+#endif
index 000d936..7628e7f 100644 (file)
@@ -39,6 +39,7 @@ eina_hamster.c \
 eina_safety_checks.c \
 eina_str.c \
 eina_strbuf.c \
+eina_unicode.c \
 eina_quadtree.c
 
 if EINA_STATIC_BUILD_CHAINED_POOL
diff --git a/src/lib/eina_unicode.c b/src/lib/eina_unicode.c
new file mode 100644 (file)
index 0000000..72234b4
--- /dev/null
@@ -0,0 +1,145 @@
+/* EINA - EFL data type library
+ * Copyright (C) 2010-2010 Tom Hacohen
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library;
+ * if not, see <http://www.gnu.org/licenses/>.
+
+ */
+
+#include <Eina.h>
+#include "eina_unicode.h"
+
+/* FIXME: check if sizeof(wchar_t) == sizeof(Eina_Unicode) if so,
+ * probably better to use the standard functions */
+
+const Eina_Unicode *EINA_UNICODE_EMPTY_STRING = {0};
+/**
+ * @brief Same as the standard strcmp just with Eina_Unicode instead of char.
+ */
+EAPI int
+eina_unicode_strcmp(const Eina_Unicode *a, const Eina_Unicode *b)
+{
+   for ( ; *a && *a == *b ; a++, b++)
+      ;
+   if (*a == *b)
+      return 0;
+   else if (*a < *b)
+      return -1;
+   else 
+      return 1;
+}
+
+/**
+ * @brief Same as the standard strcpy just with Eina_Unicode instead of char.
+ */
+EAPI Eina_Unicode *
+eina_unicode_strcpy(Eina_Unicode *dest, const Eina_Unicode *source)
+{
+   Eina_Unicode *ret = dest;
+   
+   while (*source)
+      *dest++ = *source++;
+   return ret;
+}
+
+/**
+ * @brief Same as the standard strncpy just with Eina_Unicode instead of char.
+ */
+EAPI Eina_Unicode *
+eina_unicode_strncpy(Eina_Unicode *dest, const Eina_Unicode *source, size_t n)
+{
+   Eina_Unicode *ret = dest;
+   
+   for (n = 0 ; *source && n ; n--)
+      *dest++ = *source++;
+   for ( ; n ; n--)
+      *dest++ = 0;
+   return ret;
+}
+
+/**
+ * @brief Same as the standard strlen just with Eina_Unicode instead of char.
+ */
+EAPI size_t
+eina_unicode_strlen(const Eina_Unicode *ustr)
+{
+   const Eina_Unicode *end;
+   for (end = ustr ; *end ; end++)
+      ;
+   return end - ustr;
+}
+
+/**
+ * @brief Same as the standard strdup just with Eina_Unicode instead of char.
+ */
+EAPI Eina_Unicode *
+eina_unicode_strdup(const Eina_Unicode *text)
+{
+   Eina_Unicode *ustr;
+   int len;
+
+   len = eina_unicode_strlen(text);
+   ustr = (Eina_Unicode *) calloc(sizeof(Eina_Unicode), len + 1);
+   memcpy(ustr, text, len * sizeof(Eina_Unicode));
+
+   return ustr;
+}
+
+/**
+ * @brief Same as the standard strdup just with Eina_Unicode instead of char.
+ */
+EAPI Eina_Unicode *
+eina_unicode_strstr(const Eina_Unicode *haystack, const Eina_Unicode *needle)
+{
+   const Eina_Unicode *i, *j;
+
+   for (i = haystack ; *i ; i++)
+     {
+        haystack = i; /* set this location as the base position */
+        for (j = needle ; *j && *i && *j == *i ; j++, i++)
+           ;
+
+        if (!*j) /*if we got to the end of j this means we got a full match */
+          {
+             return (Eina_Unicode *) haystack; /* return the new base position */
+          }
+     }
+
+   return NULL;
+}
+
+/**
+ * @see eina_str_escape()
+ */
+EAPI Eina_Unicode *
+eina_unicode_escape(const Eina_Unicode *str)
+{
+   Eina_Unicode *s2, *d;
+   const Eina_Unicode *s;
+
+   s2 = malloc((eina_unicode_strlen(str) * 2) + 1);
+   if (!s2) return NULL;
+   for (s = str, d = s2; *s != 0; s++, d++)
+     {
+       if ((*s == ' ') || (*s == '\\') || (*s == '\''))
+         {
+            *d = '\\';
+            d++;
+         }
+       *d = *s;
+     }
+   *d = 0;
+   return s2;
+}
+