- Moved unused argc, temp variable into small scope.
[dragonfly.git] / contrib / perl5 / lib / I18N / Collate.pm
1 package I18N::Collate;
2
3 =head1 NAME
4
5 I18N::Collate - compare 8-bit scalar data according to the current locale
6
7   ***
8
9   WARNING: starting from the Perl version 5.003_06
10   the I18N::Collate interface for comparing 8-bit scalar data
11   according to the current locale
12
13         HAS BEEN DEPRECATED
14
15   That is, please do not use it anymore for any new applications
16   and please migrate the old applications away from it because its
17   functionality was integrated into the Perl core language in the
18   release 5.003_06.
19
20   See the perllocale manual page for further information.
21
22   ***
23
24 =head1 SYNOPSIS
25
26     use I18N::Collate;
27     setlocale(LC_COLLATE, 'locale-of-your-choice'); 
28     $s1 = new I18N::Collate "scalar_data_1";
29     $s2 = new I18N::Collate "scalar_data_2";
30
31 =head1 DESCRIPTION
32
33 This module provides you with objects that will collate 
34 according to your national character set, provided that the 
35 POSIX setlocale() function is supported on your system.
36
37 You can compare $s1 and $s2 above with
38
39     $s1 le $s2
40
41 to extract the data itself, you'll need a dereference: $$s1
42
43 This module uses POSIX::setlocale(). The basic collation conversion is
44 done by strxfrm() which terminates at NUL characters being a decent C
45 routine.  collate_xfrm() handles embedded NUL characters gracefully.
46
47 The available locales depend on your operating system; try whether
48 C<locale -a> shows them or man pages for "locale" or "nlsinfo" or the
49 direct approach C<ls /usr/lib/nls/loc> or C<ls /usr/lib/nls> or
50 C<ls /usr/lib/locale>.  Not all the locales that your vendor supports
51 are necessarily installed: please consult your operating system's
52 documentation and possibly your local system administration.  The
53 locale names are probably something like C<xx_XX.(ISO)?8859-N> or
54 C<xx_XX.(ISO)?8859N>, for example C<fr_CH.ISO8859-1> is the Swiss (CH)
55 variant of French (fr), ISO Latin (8859) 1 (-1) which is the Western
56 European character set.
57
58 =cut
59
60 # I18N::Collate.pm
61 #
62 # Author:       Jarkko Hietaniemi <F<jhi@iki.fi>>
63 #               Helsinki University of Technology, Finland
64 #
65 # Acks:         Guy Decoux <F<decoux@moulon.inra.fr>> understood
66 #               overloading magic much deeper than I and told
67 #               how to cut the size of this code by more than half.
68 #               (my first version did overload all of lt gt eq le ge cmp)
69 #
70 # Purpose:      compare 8-bit scalar data according to the current locale
71 #
72 # Requirements: Perl5 POSIX::setlocale() and POSIX::strxfrm()
73 #
74 # Exports:      setlocale 1)
75 #               collate_xfrm 2)
76 #
77 # Overloads:    cmp # 3)
78 #
79 # Usage:        use I18N::Collate;
80 #               setlocale(LC_COLLATE, 'locale-of-your-choice'); # 4)
81 #               $s1 = new I18N::Collate "scalar_data_1";
82 #               $s2 = new I18N::Collate "scalar_data_2";
83 #               
84 #               now you can compare $s1 and $s2: $s1 le $s2
85 #               to extract the data itself, you need to deref: $$s1
86 #               
87 # Notes:        
88 #               1) this uses POSIX::setlocale
89 #               2) the basic collation conversion is done by strxfrm() which
90 #                  terminates at NUL characters being a decent C routine.
91 #                  collate_xfrm handles embedded NUL characters gracefully.
92 #               3) due to cmp and overload magic, lt le eq ge gt work also
93 #               4) the available locales depend on your operating system;
94 #                  try whether "locale -a" shows them or man pages for
95 #                  "locale" or "nlsinfo" work or the more direct
96 #                  approach "ls /usr/lib/nls/loc" or "ls /usr/lib/nls".
97 #                  Not all the locales that your vendor supports
98 #                  are necessarily installed: please consult your
99 #                  operating system's documentation.
100 #                  The locale names are probably something like
101 #                  'xx_XX.(ISO)?8859-N' or 'xx_XX.(ISO)?8859N',
102 #                  for example 'fr_CH.ISO8859-1' is the Swiss (CH)
103 #                  variant of French (fr), ISO Latin (8859) 1 (-1)
104 #                  which is the Western European character set.
105 #
106 # Updated:      19961005
107 #
108 # ---
109
110 use POSIX qw(strxfrm LC_COLLATE);
111
112 require Exporter;
113
114 @ISA = qw(Exporter);
115 @EXPORT = qw(collate_xfrm setlocale LC_COLLATE);
116 @EXPORT_OK = qw();
117
118 use overload qw(
119 fallback        1
120 cmp             collate_cmp
121 );
122
123 sub new {
124   my $new = $_[1];
125
126   if ($^W && $] >= 5.003_06) {
127     unless ($please_use_I18N_Collate_even_if_deprecated) {
128       warn <<___EOD___;
129 ***
130
131   WARNING: starting from the Perl version 5.003_06
132   the I18N::Collate interface for comparing 8-bit scalar data
133   according to the current locale
134
135         HAS BEEN DEPRECATED
136
137   That is, please do not use it anymore for any new applications
138   and please migrate the old applications away from it because its
139   functionality was integrated into the Perl core language in the
140   release 5.003_06.
141
142   See the perllocale manual page for further information.
143
144 ***
145 ___EOD___
146       $please_use_I18N_Collate_even_if_deprecated++;
147     }
148   }
149
150   bless \$new;
151 }
152
153 sub setlocale {
154  my ($category, $locale) = @_[0,1];
155
156  POSIX::setlocale($category, $locale) if (defined $category);
157  # the current $LOCALE 
158  $LOCALE = $locale || $ENV{'LC_COLLATE'} || $ENV{'LC_ALL'} || '';
159 }
160
161 sub C {
162   my $s = ${$_[0]};
163
164   $C->{$LOCALE}->{$s} = collate_xfrm($s)
165     unless (defined $C->{$LOCALE}->{$s}); # cache when met
166
167   $C->{$LOCALE}->{$s};
168 }
169
170 sub collate_xfrm {
171   my $s = $_[0];
172   my $x = '';
173   
174   for (split(/(\000+)/, $s)) {
175     $x .= (/^\000/) ? $_ : strxfrm("$_\000");
176   }
177
178   $x;
179 }
180
181 sub collate_cmp {
182   &C($_[0]) cmp &C($_[1]);
183 }
184
185 # init $LOCALE
186
187 &I18N::Collate::setlocale();
188
189 1; # keep require happy