New module 'unicase/u8-is-invariant'.
authorBruno Haible <bruno@clisp.org>
Sun, 8 Mar 2009 15:49:20 +0000 (16:49 +0100)
committerBruno Haible <bruno@clisp.org>
Sun, 8 Mar 2009 15:49:20 +0000 (16:49 +0100)
ChangeLog
lib/unicase/invariant.h [new file with mode: 0644]
lib/unicase/u-is-invariant.h [new file with mode: 0644]
lib/unicase/u8-is-invariant.c [new file with mode: 0644]
modules/unicase/u8-is-invariant [new file with mode: 0644]

index 673a0df..70807e0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2009-03-08  Bruno Haible  <bruno@clisp.org>
 
+       New module 'unicase/u8-is-invariant'.
+       * lib/unicase/u8-is-invariant.c: New file.
+       * lib/unicase/invariant.h: New file.
+       * lib/unicase/u-is-invariant.h: New file.
+       * modules/unicase/u8-is-invariant: New file.
+
        Tests for module 'unicase/u32-casecoll'.
        * modules/unicase/u32-casecoll-tests: New file.
        * tests/unicase/test-u32-casecoll.c: New file.
diff --git a/lib/unicase/invariant.h b/lib/unicase/invariant.h
new file mode 100644 (file)
index 0000000..2015143
--- /dev/null
@@ -0,0 +1,45 @@
+/* Internal functions for Unicode character case mappings.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2009.
+
+   This program 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 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <stdbool.h>
+#include <stddef.h>
+
+#include "unitypes.h"
+#include "uninorm.h"
+
+/* Return true if mapping NFD(S) to via MAPPING is a no-op.  */
+extern int
+       u8_is_invariant (const uint8_t *s, size_t n,
+                       uint8_t * (*mapping) (const uint8_t *s, size_t n, const char *iso639_language,
+                                             uninorm_t nf,
+                                             uint8_t *resultbuf, size_t *lengthp),
+                       const char *iso639_language,
+                       bool *resultp);
+extern int
+       u16_is_invariant (const uint16_t *s, size_t n,
+                        uint16_t * (*mapping) (const uint16_t *s, size_t n, const char *iso639_language,
+                                               uninorm_t nf,
+                                               uint16_t *resultbuf, size_t *lengthp),
+                        const char *iso639_language,
+                        bool *resultp);
+extern int
+       u32_is_invariant (const uint32_t *s, size_t n,
+                        uint32_t * (*mapping) (const uint32_t *s, size_t n, const char *iso639_language,
+                                               uninorm_t nf,
+                                               uint32_t *resultbuf, size_t *lengthp),
+                        const char *iso639_language,
+                        bool *resultp);
diff --git a/lib/unicase/u-is-invariant.h b/lib/unicase/u-is-invariant.h
new file mode 100644 (file)
index 0000000..e6b7fbb
--- /dev/null
@@ -0,0 +1,64 @@
+/* Test whether a Unicode string is invariant under a given case mapping.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2009.
+
+   This program 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 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int
+FUNC (const UNIT *s, size_t n,
+      UNIT * (*mapping) (const UNIT *s, size_t n, const char *iso639_language,
+                        uninorm_t nf,
+                        UNIT *resultbuf, size_t *lengthp),
+      const char *iso639_language,
+      bool *resultp)
+{
+  UNIT normsbuf[2048 / sizeof (UNIT)];
+  UNIT *norms;
+  size_t norms_length;
+  UNIT mappedbuf[2048 / sizeof (UNIT)];
+  UNIT *mapped;
+  size_t mapped_length;
+
+  /* Apply canonical decomposition to S.  */
+  norms_length = sizeof (normsbuf) / sizeof (UNIT);
+  norms = U_NORMALIZE (UNINORM_NFD, s, n, normsbuf, &norms_length);
+  if (norms == NULL)
+    /* errno is set here.  */
+    return -1;
+
+  /* Apply mapping.  */
+  mapped_length = sizeof (mappedbuf) / sizeof (UNIT);
+  mapped = mapping (norms, norms_length, iso639_language, NULL,
+                   mappedbuf, &mapped_length);
+  if (mapped == NULL)
+    {
+      if (norms != normsbuf)
+       {
+         int saved_errno = errno;
+         free (norms);
+         errno = saved_errno;
+       }
+      return -1;
+    }
+
+  /* Compare.  */
+  *resultp = (mapped_length == norms_length
+             && U_CMP (mapped, norms, norms_length) == 0);
+
+  if (mapped != mappedbuf)
+    free (mapped);
+  if (norms != normsbuf)
+    free (norms);
+  return 0;
+}
diff --git a/lib/unicase/u8-is-invariant.c b/lib/unicase/u8-is-invariant.c
new file mode 100644 (file)
index 0000000..2c4913b
--- /dev/null
@@ -0,0 +1,33 @@
+/* Test whether an UTF-8 string is invariant under a given case mapping.
+   Copyright (C) 2009 Free Software Foundation, Inc.
+   Written by Bruno Haible <bruno@clisp.org>, 2009.
+
+   This program 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 3 of the License, or
+   (at your option) any later version.
+
+   This program 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 program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include "unicase/invariant.h"
+
+#include <errno.h>
+#include <stdlib.h>
+
+#include "uninorm.h"
+#include "unistr.h"
+
+#define FUNC u8_is_invariant
+#define UNIT uint8_t
+#define U_NORMALIZE u8_normalize
+#define U_CMP u8_cmp
+#include "u-is-invariant.h"
diff --git a/modules/unicase/u8-is-invariant b/modules/unicase/u8-is-invariant
new file mode 100644 (file)
index 0000000..40cacdd
--- /dev/null
@@ -0,0 +1,30 @@
+Description:
+Test whether an UTF-8 string is invariant under a given case mapping.
+
+Files:
+lib/unicase/u8-is-invariant.c
+lib/unicase/invariant.h
+lib/unicase/u-is-invariant.h
+
+Depends-on:
+unitypes
+uninorm/base
+uninorm/u8-normalize
+uninorm/nfd
+unistr/u8-cmp
+stdbool
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += unicase/u8-is-invariant.c
+
+Include:
+"unicase/invariant.h"
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+