Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/unicase/u8-tolower.c @ 14079:97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Run the new "make update-copyright" rule.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Sat, 01 Jan 2011 20:17:23 +0100 |
parents | c2cbabec01dd |
children | 8250f2777afc |
rev | line source |
---|---|
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Lowercase mapping for UTF-8 strings (locale dependent). |
14079
97fc9a21a8fb
maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents:
12559
diff
changeset
|
2 Copyright (C) 2009-2011 Free Software Foundation, Inc. |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 Written by Bruno Haible <bruno@clisp.org>, 2009. |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify it |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 under the terms of the GNU Lesser General Public License as published |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
7 by the Free Software Foundation; either version 3 of the License, or |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
8 (at your option) any later version. |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 Lesser General Public License for more details. |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public License |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
18 #include <config.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 /* Specification. */ |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include "unicase.h" |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #include <stddef.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 #include "unicasemap.h" |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 #include "special-casing.h" |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 uint8_t * |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 u8_tolower (const uint8_t *s, size_t n, const char *iso639_language, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
30 uninorm_t nf, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
31 uint8_t *resultbuf, size_t *lengthp) |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 { |
11689
189b3ad9d096
Update u8_casemap invocation.
Bruno Haible <bruno@clisp.org>
parents:
11316
diff
changeset
|
33 return u8_casemap (s, n, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
34 unicase_empty_prefix_context, unicase_empty_suffix_context, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
35 iso639_language, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
36 uc_tolower, offsetof (struct special_casing_rule, lower[0]), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
37 nf, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
38 resultbuf, lengthp); |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
42 #ifdef TEST |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
43 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
44 #include <locale.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 #include <stdio.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
46 #include <stdlib.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
47 #include <string.h> |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
48 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
49 /* Read the contents of an input stream, and return it, terminated with a NUL |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
50 byte. */ |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 char * |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 read_file (FILE *stream) |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 { |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
54 #define BUFSIZE 4096 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 char *buf = NULL; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 int alloc = 0; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 int size = 0; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 int count; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 while (! feof (stream)) |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 { |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 if (size + BUFSIZE > alloc) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
63 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
64 alloc = alloc + alloc / 2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
65 if (alloc < size + BUFSIZE) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
66 alloc = size + BUFSIZE; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
67 buf = realloc (buf, alloc); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
68 if (buf == NULL) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
69 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
70 fprintf (stderr, "out of memory\n"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
71 exit (1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
72 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
73 } |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 count = fread (buf + size, 1, BUFSIZE, stream); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 if (count == 0) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
76 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
77 if (ferror (stream)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
78 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
79 perror ("fread"); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
80 exit (1); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
81 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
82 } |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 else |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
84 size += count; |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
85 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
86 buf = realloc (buf, size + 1); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
87 if (buf == NULL) |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
88 { |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
89 fprintf (stderr, "out of memory\n"); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 exit (1); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
91 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
92 buf[size] = '\0'; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
93 return buf; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
94 #undef BUFSIZE |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
95 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
96 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
97 int |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
98 main (int argc, char * argv[]) |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 { |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 setlocale (LC_ALL, ""); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 if (argc == 1) |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 { |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 /* Display the lower case of the input string. */ |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 char *input = read_file (stdin); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 int length = strlen (input); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
106 size_t output_length; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 uint8_t *output = |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
108 u8_tolower ((uint8_t *) input, length, uc_locale_language (), |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
109 NULL, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
11689
diff
changeset
|
110 NULL, &output_length); |
11316
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
111 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
112 fwrite (output, 1, output_length, stdout); |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
113 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
114 return 0; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
115 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
116 else |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
117 return 1; |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
118 } |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
119 |
614fa8bd908c
New module 'unicase/u8-tolower'.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
120 #endif /* TEST */ |