# HG changeset patch # User Bruno Haible # Date 1234045640 -3600 # Node ID 5d0199ab0adb97dca71a5d0e46cee39dd8249f98 # Parent d39a9bdacb69cd9243314c1109cb2a4285ecf98e New module 'unistr/u8-strcoll'. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2009-02-07 Bruno Haible + + New module 'unistr/u8-strcoll'. + * modules/unistr/u8-strcoll: New file. + * lib/unistr/u8-strcoll.c: New file. + * lib/unistr/u-strcoll.h: New file. + 2009-02-07 Bruno Haible * test-mbrtowc4.sh (LOCALE_ZH_CN): Fix default value. diff --git a/lib/unistr/u-strcoll.h b/lib/unistr/u-strcoll.h new file mode 100644 --- /dev/null +++ b/lib/unistr/u-strcoll.h @@ -0,0 +1,81 @@ +/* Compare UTF-8/UTF-16/UTF-32 strings using the collation rules of the current + locale. + 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 *s1, const UNIT *s2) +{ + /* When this function succeeds, it sets errno back to its original value. + When it fails, it sets errno, but also returns a meaningful return value, + for the sake of callers which ignore errno. */ + int final_errno = errno; + char *sl1; + char *sl2; + int result; + + sl1 = U_STRCONV_TO_LOCALE (s1); + if (sl1 != NULL) + { + sl2 = U_STRCONV_TO_LOCALE (s2); + if (sl2 != NULL) + { + /* Compare sl1 and sl2. */ + errno = 0; + result = strcoll (sl1, sl2); + if (errno == 0) + { + /* strcoll succeeded. */ + free (sl1); + free (sl2); + } + else + { + /* strcoll failed. */ + final_errno = errno; + free (sl1); + free (sl2); + result = U_STRCMP (s1, s2); + } + } + else + { + /* s1 could be converted to locale encoding, s2 not. */ + final_errno = errno; + free (sl1); + result = -1; + } + } + else + { + final_errno = errno; + sl2 = U_STRCONV_TO_LOCALE (s2); + if (sl2 != NULL) + { + /* s2 could be converted to locale encoding, s1 not. */ + free (sl2); + result = 1; + } + else + { + /* Neither s1 nor s2 could be converted to locale encoding. */ + result = U_STRCMP (s1, s2); + } + } + + errno = final_errno; + return result; +} diff --git a/lib/unistr/u8-strcoll.c b/lib/unistr/u8-strcoll.c new file mode 100644 --- /dev/null +++ b/lib/unistr/u8-strcoll.c @@ -0,0 +1,33 @@ +/* Compare UTF-8 strings using the collation rules of the current locale. + 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 "unistr.h" + +#include +#include +#include + +#include "uniconv.h" + +#define FUNC u8_strcoll +#define UNIT uint8_t +#define U_STRCMP u8_strcmp +#define U_STRCONV_TO_LOCALE u8_strconv_to_locale +#include "u-strcoll.h" diff --git a/modules/unistr/u8-strcoll b/modules/unistr/u8-strcoll new file mode 100644 --- /dev/null +++ b/modules/unistr/u8-strcoll @@ -0,0 +1,26 @@ +Description: +Compare UTF-8 strings using the collation rules of the current locale. + +Files: +lib/unistr/u8-strcoll.c +lib/unistr/u-strcoll.h + +Depends-on: +unistr/base +unistr/u8-strcmp +uniconv/u8-strconv-to-locale + +configure.ac: + +Makefile.am: +lib_SOURCES += unistr/u8-strcoll.c + +Include: +"unistr.h" + +License: +LGPL + +Maintainer: +Bruno Haible +