1 /* Helper functions used by strftime/strptime to handle alternate digits.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include "../locale/localeinfo.h"
20 #include <libc-lock.h>
26 /* Some of the functions here must not be used while setlocale is called. */
27 __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden
)
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)
33 static struct lc_time_data
*
34 _nl_init_alt_digit (struct __locale_data
*current
)
36 struct lc_time_data
*data
= current
->private;
40 data
= calloc (sizeof *data
, 1);
43 current
->private = data
;
46 if (! data
->alt_digits_initialized
)
48 const char *ptr
= CURRENT (ALT_DIGITS
);
51 data
->alt_digits_initialized
= 1;
55 data
->alt_digits
= malloc (100 * sizeof (const char *));
56 if (data
->alt_digits
!= NULL
)
57 for (cnt
= 0; cnt
< 100; ++cnt
)
59 data
->alt_digits
[cnt
] = ptr
;
61 /* Skip digit format. */
62 ptr
= strchr (ptr
, '\0') + 1;
71 _nl_get_alt_digit (unsigned int number
, struct __locale_data
*current
)
75 if (number
>= 100 || CURRENT (ALT_DIGITS
)[0] == '\0')
78 __libc_rwlock_wrlock (__libc_setlocale_lock
);
80 struct lc_time_data
*data
= _nl_init_alt_digit (current
);
82 result
= ((data
!= NULL
83 && data
->alt_digits
!= NULL
)
84 ? data
->alt_digits
[number
]
87 __libc_rwlock_unlock (__libc_setlocale_lock
);
94 _nl_get_walt_digit (unsigned int number
, struct __locale_data
*current
)
96 const wchar_t *result
= NULL
;
98 if (number
>= 100 || CURRENT_WSTR (_NL_WALT_DIGITS
)[0] == L
'\0')
101 __libc_rwlock_wrlock (__libc_setlocale_lock
);
103 struct lc_time_data
*data
= current
->private;
106 data
= calloc (sizeof *data
, 1);
109 current
->private = data
;
112 if (! data
->walt_digits_initialized
)
114 const wchar_t *ptr
= CURRENT_WSTR (_NL_WALT_DIGITS
);
117 data
->walt_digits_initialized
= 1;
121 data
->walt_digits
= malloc (100 * sizeof (const uint32_t *));
122 if (data
->walt_digits
!= NULL
)
123 for (cnt
= 0; cnt
< 100; ++cnt
)
125 data
->walt_digits
[cnt
] = ptr
;
127 /* Skip digit format. */
128 ptr
= __wcschr (ptr
, L
'\0') + 1;
133 if (data
->walt_digits
!= NULL
)
134 result
= data
->walt_digits
[number
];
137 __libc_rwlock_unlock (__libc_setlocale_lock
);
139 return (wchar_t *) result
;
144 _nl_parse_alt_digit (const char **strp
, struct __locale_data
*current
)
146 const char *str
= *strp
;
151 if (CURRENT_WSTR (_NL_WALT_DIGITS
)[0] == L
'\0')
154 __libc_rwlock_wrlock (__libc_setlocale_lock
);
156 struct lc_time_data
*data
= _nl_init_alt_digit (current
);
157 if (data
!= NULL
&& data
->alt_digits
!= NULL
)
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
163 for (cnt
= 0; cnt
< 100; ++cnt
)
165 const char *const dig
= data
->alt_digits
[cnt
];
166 size_t len
= strlen (dig
);
168 if (len
> maxlen
&& strncmp (dig
, str
, len
) == 0)
175 __libc_rwlock_unlock (__libc_setlocale_lock
);