]> sourceware.org Git - glibc.git/blame - time/alt_digit.c
Update copyright dates with scripts/update-copyrights
[glibc.git] / time / alt_digit.c
CommitLineData
df9f41c9 1/* Helper functions used by strftime/strptime to handle alternate digits.
dff8da6b 2 Copyright (C) 1995-2024 Free Software Foundation, Inc.
df9f41c9
RM
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
59ba27a6 16 License along with the GNU C Library; if not, see
5a82c748 17 <https://www.gnu.org/licenses/>. */
df9f41c9
RM
18
19#include "../locale/localeinfo.h"
ec999b8e 20#include <libc-lock.h>
df9f41c9
RM
21#include <stdlib.h>
22#include <wchar.h>
23#include <string.h>
e054f494 24#include <stdint.h>
df9f41c9
RM
25
26/* Some of the functions here must not be used while setlocale is called. */
91e32540 27__libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
df9f41c9
RM
28
29#define CURRENT(item) (current->values[_NL_ITEM_INDEX (item)].string)
30#define CURRENT_WSTR(item) \
31 ((wchar_t *) current->values[_NL_ITEM_INDEX (item)].wstr)
32
7ee41feb 33static struct lc_time_data *
f095bb72 34_nl_init_alt_digit (struct __locale_data *current)
df9f41c9 35{
7ee41feb 36 struct lc_time_data *data = current->private;
df9f41c9 37
7ee41feb 38 if (data == NULL)
df9f41c9 39 {
7ee41feb
FW
40 data = calloc (sizeof *data, 1);
41 if (data == NULL)
42 return NULL;
43 current->private = data;
df9f41c9 44 }
df9f41c9
RM
45
46 if (! data->alt_digits_initialized)
47 {
48 const char *ptr = CURRENT (ALT_DIGITS);
49 size_t cnt;
50
51 data->alt_digits_initialized = 1;
52
53 if (ptr != NULL)
54 {
55 data->alt_digits = malloc (100 * sizeof (const char *));
56 if (data->alt_digits != NULL)
57 for (cnt = 0; cnt < 100; ++cnt)
58 {
59 data->alt_digits[cnt] = ptr;
60
61 /* Skip digit format. */
62 ptr = strchr (ptr, '\0') + 1;
63 }
64 }
65 }
66
7ee41feb 67 return data;
df9f41c9
RM
68}
69
70const char *
f095bb72 71_nl_get_alt_digit (unsigned int number, struct __locale_data *current)
df9f41c9
RM
72{
73 const char *result;
74
75 if (number >= 100 || CURRENT (ALT_DIGITS)[0] == '\0')
76 return NULL;
77
91e32540 78 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9 79
7ee41feb 80 struct lc_time_data *data = _nl_init_alt_digit (current);
df9f41c9 81
7ee41feb
FW
82 result = ((data != NULL
83 && data->alt_digits != NULL)
84 ? data->alt_digits[number]
df9f41c9
RM
85 : NULL);
86
91e32540 87 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
88
89 return result;
90}
91
92
93const wchar_t *
f095bb72 94_nl_get_walt_digit (unsigned int number, struct __locale_data *current)
df9f41c9
RM
95{
96 const wchar_t *result = NULL;
df9f41c9
RM
97
98 if (number >= 100 || CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
99 return NULL;
100
91e32540 101 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9 102
7ee41feb
FW
103 struct lc_time_data *data = current->private;
104 if (data == NULL)
df9f41c9 105 {
7ee41feb
FW
106 data = calloc (sizeof *data, 1);
107 if (data == NULL)
df9f41c9 108 goto out;
7ee41feb 109 current->private = data;
df9f41c9 110 }
df9f41c9
RM
111
112 if (! data->walt_digits_initialized)
113 {
114 const wchar_t *ptr = CURRENT_WSTR (_NL_WALT_DIGITS);
115 size_t cnt;
116
117 data->walt_digits_initialized = 1;
118
119 if (ptr != NULL)
120 {
121 data->walt_digits = malloc (100 * sizeof (const uint32_t *));
122 if (data->walt_digits != NULL)
123 for (cnt = 0; cnt < 100; ++cnt)
124 {
125 data->walt_digits[cnt] = ptr;
126
127 /* Skip digit format. */
2cfbdb9a 128 ptr = __wcschr (ptr, L'\0') + 1;
df9f41c9
RM
129 }
130 }
131 }
132
133 if (data->walt_digits != NULL)
134 result = data->walt_digits[number];
135
136 out:
91e32540 137 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
138
139 return (wchar_t *) result;
140}
141
142
143int
f095bb72 144_nl_parse_alt_digit (const char **strp, struct __locale_data *current)
df9f41c9
RM
145{
146 const char *str = *strp;
147 int result = -1;
148 size_t cnt;
149 size_t maxlen = 0;
150
151 if (CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
152 return result;
153
91e32540 154 __libc_rwlock_wrlock (__libc_setlocale_lock);
df9f41c9 155
7ee41feb
FW
156 struct lc_time_data *data = _nl_init_alt_digit (current);
157 if (data != NULL && data->alt_digits != NULL)
df9f41c9
RM
158 /* Matching is not unambiguous. The alternative digits could be like
159 I, II, III, ... and the first one is a substring of the second
160 and third. Therefore we must keep on searching until we found
161 the longest possible match. Note that this is not specified in
162 the standard. */
163 for (cnt = 0; cnt < 100; ++cnt)
164 {
7ee41feb 165 const char *const dig = data->alt_digits[cnt];
df9f41c9
RM
166 size_t len = strlen (dig);
167
168 if (len > maxlen && strncmp (dig, str, len) == 0)
169 {
170 maxlen = len;
171 result = (int) cnt;
172 }
173 }
174
91e32540 175 __libc_rwlock_unlock (__libc_setlocale_lock);
df9f41c9
RM
176
177 if (result != -1)
178 *strp += maxlen;
179
180 return result;
181}
This page took 0.539387 seconds and 5 git commands to generate.