From: Bruno Haible Date: Sun, 8 Mar 2009 15:49:20 +0000 (+0100) Subject: New module 'unicase/u8-is-invariant'. X-Git-Tag: v0.1~6137 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=66a28fc207d465abfca296380fb6c2b6f1fb203a;p=gnulib.git New module 'unicase/u8-is-invariant'. --- diff --git a/ChangeLog b/ChangeLog index 673a0df6d..70807e038 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2009-03-08 Bruno Haible + 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 index 000000000..201514333 --- /dev/null +++ b/lib/unicase/invariant.h @@ -0,0 +1,45 @@ +/* Internal functions for Unicode character case mappings. + Copyright (C) 2009 Free Software Foundation, Inc. + Written by Bruno Haible , 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 . */ + +#include +#include + +#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 index 000000000..e6b7fbb41 --- /dev/null +++ b/lib/unicase/u-is-invariant.h @@ -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 , 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 . */ + +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 index 000000000..2c4913bfb --- /dev/null +++ b/lib/unicase/u8-is-invariant.c @@ -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 , 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 . */ + +#include + +/* Specification. */ +#include "unicase/invariant.h" + +#include +#include + +#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 index 000000000..40cacdd4e --- /dev/null +++ b/modules/unicase/u8-is-invariant @@ -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 +