update from main archive 961201
[platform/upstream/glibc.git] / string / strcoll.c
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB.  If
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.  */
19
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #ifndef STRING_TYPE
25 # define STRING_TYPE char
26 # define USTRING_TYPE unsigned char
27 # define STRCOLL strcoll
28 # define STRCMP strcmp
29 #endif
30
31 /* Include the shared helper functions.  `strxfrm'/`wcsxfrm' also use
32    these functions.  */
33 #include "../locale/weight.h"
34
35
36 /* Compare S1 and S2, returning less than, equal to or
37    greater than zero if the collated form of S1 is lexicographically
38    less than, equal to or greater than the collated form of S2.  */
39 int
40 STRCOLL (s1, s2)
41      const STRING_TYPE *s1;
42      const STRING_TYPE *s2;
43 {
44   weight_t *s1forw = NULL;
45   weight_t *s1backw = NULL;
46   weight_t *s2forw = NULL;
47   weight_t *s2backw = NULL;
48   size_t pass;
49
50   /* If the current locale does not specify locale data we use normal
51      8-bit string comparison.  */
52   if (collate_nrules == 0)
53     return STRCMP (s1, s2);
54
55   /* Get full information about the strings.  This means we get
56      information for all passes in a special data structure.  */
57   get_string (s1, s1forw, s1backw);
58   get_string (s2, s2forw, s2backw);
59
60   /* Now we have all the information.  In at most the given number of
61      passes we can finally decide about the order.  */
62   for (pass = 0; pass < collate_nrules; ++pass)
63     {
64       int forward = (collate_rules[pass] & sort_forward) != 0;
65       const weight_t *s1run = forward ? s1forw : s1backw;
66       const weight_t *s2run = forward ? s2forw : s2backw;
67       int s1idx = forward ? 0 : s1run->data[pass].number - 1;
68       int s2idx = forward ? 0 : s2run->data[pass].number - 1;
69
70       do
71         {
72           int s1ignore = 0;
73           int s2ignore = 0;
74           u_int32_t w1, w2;
75
76           /* Here we have to check for IGNORE entries.  If these are
77              found we count them and go on with the next value.  */
78           while ((w1 = s1run->data[pass].value[s1idx])
79                  == (u_int32_t) IGNORE_CHAR)
80             {
81               ++s1ignore;
82               if ((forward && ++s1idx >= s1run->data[pass].number)
83                   || (!forward && --s1idx < 0))
84                 {
85                   weight_t *nextp = forward ? s1run->next : s1run->prev;
86                   if (nextp == NULL)
87                     {
88                       w1 = 0;
89                       break;
90                     }
91                   s1run = nextp;
92                   s1idx = forward ? 0 : s1run->data[pass].number - 1;
93                 }
94             }
95
96           while ((w2 = s2run->data[pass].value[s2idx])
97                  == (u_int32_t) IGNORE_CHAR)
98             {
99               ++s2ignore;
100               if ((forward && ++s2idx >= s2run->data[pass].number)
101                   || (!forward && --s2idx < 0))
102                 {
103                   weight_t *nextp = forward ? s2run->next : s2run->prev;
104                   if (nextp == NULL)
105                     {
106                       w2 = 0;
107                       break;
108                     }
109                   s2run = nextp;
110                   s2idx = forward ? 0 : s2run->data[pass].number - 1;
111                 }
112             }
113
114           /* Now we have information of the number of ignored
115              weights and the value of the next weight.  */
116           if ((collate_rules[pass] & sort_position) != 0
117               && s1ignore != s2ignore && (w1 != 0 || w2 != 0))
118             return s1ignore < s2ignore ? -1 : 1;
119
120           if (w1 != w2)
121             return w1 < w2 ? -1 : 1;
122
123           /* We have to increment the index counters.  */
124           if ((forward && ++s1idx >= s1run->data[pass].number)
125               || (!forward && --s1idx < 0))
126             if (forward)
127               {
128                 s1run = s1run->next;
129                 s1idx = 0;
130               }
131             else
132               {
133                 s1run = s1run->prev;
134                 if (s1run != NULL)
135                   s1idx = s1run->data[pass].number - 1;
136               }
137
138           if ((forward && ++s2idx >= s2run->data[pass].number)
139               || (!forward && --s2idx < 0))
140             if (forward)
141               {
142                 s2run = s2run->next;
143                 s2idx = 0;
144               }
145             else
146               {
147                 s2run = s2run->prev;
148                 if (s2run != NULL)
149                   s2idx = s2run->data[pass].number - 1;
150               }
151
152         }
153       while (s1run != NULL && s2run != NULL);
154
155       if (s1run != s2run)
156         return s1run != NULL ? 1 : -1;
157     }
158
159   return 0;
160 }