]> sourceware.org Git - glibc.git/blame - string/strcoll.c
initial import
[glibc.git] / string / strcoll.c
CommitLineData
28f540f4
RM
1/* Copyright (C) 1991 Free Software Foundation, Inc.
2This file is part of the GNU C Library.
3
4The GNU C Library is free software; you can redistribute it and/or
5modify it under the terms of the GNU Library General Public License as
6published by the Free Software Foundation; either version 2 of the
7License, or (at your option) any later version.
8
9The GNU C Library is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12Library General Public License for more details.
13
14You should have received a copy of the GNU Library General Public
15License along with the GNU C Library; see the file COPYING.LIB. If
16not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17Cambridge, MA 02139, USA. */
18
19#include <ansidecl.h>
20#include <localeinfo.h>
21#include <stddef.h>
22#include <stdlib.h>
23#include <string.h>
24
25
26/* Compare S1 and S2, returning less than, equal to or
27 greater than zero if the collated form of S1 is lexiographically
28 less than, equal to or greater than the collated form of S2. */
29int
30DEFUN(strcoll, (s1, s2), CONST char *s1 AND CONST char *s2)
31{
32 if (_collate_info == NULL || _collate_info->values == NULL)
33 return strcmp(s1, s2);
34 else
35 {
36 CONST unsigned char *CONST values = _collate_info->values;
37 CONST unsigned char *CONST offsets = _collate_info->offsets;
38
39 while (*s1 != '\0' && *s2 != '\0')
40 {
41 CONST unsigned char c1 = *s1++, c2 = *s2++;
42 CONST unsigned char v1 = values[c1], v2 = values[c2];
43 CONST unsigned char o1 = offsets[c1], o2 = offsets[c2];
44
45 if (v1 == UCHAR_MAX && o1 == 0)
46 /* This is a non-collating element. Skip it. */
47 --s2;
48 else if (v2 == UCHAR_MAX && o2 == 0)
49 --s1;
50 else if (v1 == UCHAR_MAX && o1 == CHAR_MAX)
51 {
52 /* This element collates lower than anything else. */
53 if (v2 != UCHAR_MAX || o2 != CHAR_MAX)
54 return -1;
55 }
56 else if (v2 == UCHAR_MAX && o2 == CHAR_MAX)
57 return 1;
58 else if (v1 != v2)
59 return v1 - v2;
60 else if (o1 != o2)
61 return o1 - o2;
62 }
63
64 if (*s1 == '\0')
65 return *s2 == '\0' ? 0 : -1;
66 else if (*s2 == '\0')
67 return 1;
68 return 0;
69 }
70}
This page took 0.029993 seconds and 5 git commands to generate.