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>.
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.
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.
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. */
25 # define STRING_TYPE char
26 # define USTRING_TYPE unsigned char
27 # define STRCOLL strcoll
30 /* Include the shared helper functions. `strxfrm'/`wcsxfrm' also use
32 #include "../locale/weight.h"
35 /* Compare S1 and S2, returning less than, equal to or
36 greater than zero if the collated form of S1 is lexicographically
37 less than, equal to or greater than the collated form of S2. */
40 const STRING_TYPE *s1;
41 const STRING_TYPE *s2;
43 weight_t *s1forw = NULL;
44 weight_t *s1backw = NULL;
45 weight_t *s2forw = NULL;
46 weight_t *s2backw = NULL;
49 /* If the current locale does not specify locale data we use normal
50 8-bit string comparison. */
51 if (collate_nrules == 0)
52 return strcmp (s1, s2);
54 /* Get full information about the strings. This means we get
55 information for all passes in a special data structure. */
56 get_string (s1, s1forw, s1backw);
57 get_string (s2, s2forw, s2backw);
59 /* Now we have all the information. In at most the given number of
60 passes we can finally decide about the order. */
61 for (pass = 0; pass < collate_nrules; ++pass)
63 int forward = (collate_rules[pass] & sort_forward) != 0;
64 const weight_t *s1run = forward ? s1forw : s1backw;
65 const weight_t *s2run = forward ? s2forw : s2backw;
66 int s1idx = forward ? 0 : s1run->data[pass].number - 1;
67 int s2idx = forward ? 0 : s2run->data[pass].number - 1;
75 /* Here we have to check for IGNORE entries. If these are
76 found we count them and go on witht he next value. */
77 while ((w1 = s1run->data[pass].value[s1idx]) == IGNORE_CHAR)
80 if ((forward && ++s1idx >= s1run->data[pass].number)
81 || (!forward && --s1idx < 0))
83 weight_t *nextp = forward ? s1run->next : s1run->prev;
90 s1idx = forward ? 0 : s1run->data[pass].number - 1;
94 while ((w2 = s2run->data[pass].value[s2idx]) == IGNORE_CHAR)
97 if ((forward && ++s2idx >= s2run->data[pass].number)
98 || (!forward && --s2idx < 0))
100 weight_t *nextp = forward ? s2run->next : s2run->prev;
107 s2idx = forward ? 0 : s2run->data[pass].number - 1;
111 /* Now we have information of the number of ignored
112 weights and the value of the next weight. */
113 if ((collate_rules[pass] & sort_position) != 0
114 && s1ignore != s2ignore && (w1 != 0 || w2 != 0))
115 return s1ignore < s2ignore ? -1 : 1;
118 return w1 < w2 ? -1 : 1;
120 /* We have to increment the index counters. */
121 if ((forward && ++s1idx >= s1run->data[pass].number)
122 || (!forward && --s1idx < 0))
132 s1idx = s1run->data[pass].number - 1;
135 if ((forward && ++s2idx >= s2run->data[pass].number)
136 || (!forward && --s2idx < 0))
146 s2idx = s2run->data[pass].number - 1;
150 while (s1run != NULL && s2run != NULL);
153 return s1run != NULL ? 1 : -1;