This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC] collate_members_wchar_t.cc


On Sat, 2002-03-09 at 10:46, Paolo Carlini wrote:

> #include <string.h>
> #include <wchar.h>
> 
> int main()
> {
>   int cmp, wcmp;
> 
>   char* one = "Paolo";
>   char* two = "Carlini";
> 
>   wchar_t* three = L"Paolo";
>   wchar_t* four = L"Carlini";
> 
>   cmp = strcmp(one, two);
>   /* cmp = 1 */
> 
>   wcmp = wcscmp(three, four);
>   /* wcmp = 13 */
> 
>   return 0
> }

None of the functions strcmp, wcscmp, strcoll, wcscoll is required to
return a normalized value (i.e., either 1, 0, -1).  Instead only <0,
==0, >0 must be tested for.


>   template<>
>     int
>     collate<wchar_t>::_M_compare_helper(const wchar_t* __one,
>      const wchar_t* __two) const
>     {
>       int __cmp = wcscoll(__one, __two);
>       if (__cmp > 0)
>         return 1;
>       else if (__cmp < 0)
>         return -1;
>       else return 0;
>     }

This is definitely needed.  The C++ standard indeed requires
normalization of the return value for do_compare.  ISO C does not (with
good reasons) for strcoll and wcscoll.  But the conditionals are kind of
bad.  I'd suggest

template<>
  int
  collate<wchar_t>::_M_compare_helper(const wchar_t* __one,
   const wchar_t* __two) const
  {
    int __cmp = wcscoll(__one, __two);
    return (__cmp >> (8 * sizeof (int) - 2)) | (__cmp != 0);
  }

Compiled for the right platform it won't require any jumps.  Modern gcc
versions will optimize your code pretty well but it's not as good as
this code.

You have to use the same sequence for the strcoll call.


> would fix only the assert at line 129, /not/ that at 107, where i1 == 20 (vs i1
> == -1 for the corresponding char test).

On what system do you run this?  I get -1 for the C equivalent

  wcscoll (L"Äuglein", L"Augment")

It might well be the case that you're stumbling accross a problem with
the libc code.  There were until recently some changes in the locale
file generation.  I'm running the current RH libc (as you'd get it from
up2date) and I guess the test case would work.

Anyway, this test assumes correct collation information according to
German collation rules.  Not every system which has locales and has a
de_DE locale will have this correctly implemented.

-- 
---------------.                          ,-.   1325 Chesapeake Terrace
Ulrich Drepper  \    ,-------------------'   \  Sunnyvale, CA 94089 USA
Red Hat          `--' drepper at redhat.com   `------------------------

Attachment: signature.asc
Description: This is a digitally signed message part


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]