bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

new modules unistr/u{8,16,32}-strcoll


From: Bruno Haible
Subject: new modules unistr/u{8,16,32}-strcoll
Date: Sat, 7 Feb 2009 23:33:21 +0100
User-agent: KMail/1.9.9

The unistr/* modules so far don't have a locale aware comparison of Unicode
strings. This commit adds such functions.


2009-02-07  Bruno Haible  <address@hidden>

        * lib/unistr.h (u8_strcoll, u16_strcoll, u32_strcoll): New declations.

        New module 'unistr/u32-strcoll'.
        * modules/unistr/u32-strcoll: New file.
        * lib/unistr/u32-strcoll.c: New file.

        New module 'unistr/u16-strcoll'.
        * modules/unistr/u16-strcoll: New file.
        * lib/unistr/u16-strcoll.c: New file.

        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.

--- lib/unistr.h.orig   2009-02-07 23:31:41.000000000 +0100
+++ lib/unistr.h        2009-02-07 22:53:33.000000000 +0100
@@ -1,5 +1,5 @@
 /* Elementary Unicode string functions.
-   Copyright (C) 2001-2002, 2005-2008 Free Software Foundation, Inc.
+   Copyright (C) 2001-2002, 2005-2009 Free Software Foundation, Inc.
 
    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
@@ -553,6 +553,17 @@
 extern int
        u32_strcmp (const uint32_t *s1, const uint32_t *s2);
 
+/* Compare S1 and S2 using the collation rules of the current locale.
+   Return -1 if S1 < S2, 0 if S1 = S2, 1 if S1 > S2.
+   Upon failure, set errno and return any value.  */
+/* Similar to strcoll(), wcscoll().  */
+extern int
+       u8_strcoll (const uint8_t *s1, const uint8_t *s2);
+extern int
+       u16_strcoll (const uint16_t *s1, const uint16_t *s2);
+extern int
+       u32_strcoll (const uint32_t *s1, const uint32_t *s2);
+
 /* Compare no more than N units of S1 and S2.  */
 /* Similar to strncmp(), wcsncmp().  */
 extern int

=========================== lib/unistr/u-strcoll.h ===========================
/* 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 <address@hidden>, 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 *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;
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]