alt_digit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Helper functions used by strftime/strptime to handle alternate digits.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include "../locale/localeinfo.h"
  16. #include <libc-lock.h>
  17. #include <stdlib.h>
  18. #include <wchar.h>
  19. #include <string.h>
  20. #include <stdint.h>
  21. /* Some of the functions here must not be used while setlocale is called. */
  22. __libc_rwlock_define (extern, __libc_setlocale_lock attribute_hidden)
  23. #define CURRENT(item) (current->values[_NL_ITEM_INDEX (item)].string)
  24. #define CURRENT_WSTR(item) \
  25. ((wchar_t *) current->values[_NL_ITEM_INDEX (item)].wstr)
  26. static struct lc_time_data *
  27. _nl_init_alt_digit (struct __locale_data *current)
  28. {
  29. struct lc_time_data *data = current->private;
  30. if (data == NULL)
  31. {
  32. data = calloc (sizeof *data, 1);
  33. if (data == NULL)
  34. return NULL;
  35. current->private = data;
  36. }
  37. if (! data->alt_digits_initialized)
  38. {
  39. const char *ptr = CURRENT (ALT_DIGITS);
  40. size_t cnt;
  41. data->alt_digits_initialized = 1;
  42. if (ptr != NULL)
  43. {
  44. data->alt_digits = malloc (100 * sizeof (const char *));
  45. if (data->alt_digits != NULL)
  46. for (cnt = 0; cnt < 100; ++cnt)
  47. {
  48. data->alt_digits[cnt] = ptr;
  49. /* Skip digit format. */
  50. ptr = strchr (ptr, '\0') + 1;
  51. }
  52. }
  53. }
  54. return data;
  55. }
  56. const char *
  57. _nl_get_alt_digit (unsigned int number, struct __locale_data *current)
  58. {
  59. const char *result;
  60. if (number >= 100 || CURRENT (ALT_DIGITS)[0] == '\0')
  61. return NULL;
  62. __libc_rwlock_wrlock (__libc_setlocale_lock);
  63. struct lc_time_data *data = _nl_init_alt_digit (current);
  64. result = ((data != NULL
  65. && data->alt_digits != NULL)
  66. ? data->alt_digits[number]
  67. : NULL);
  68. __libc_rwlock_unlock (__libc_setlocale_lock);
  69. return result;
  70. }
  71. const wchar_t *
  72. _nl_get_walt_digit (unsigned int number, struct __locale_data *current)
  73. {
  74. const wchar_t *result = NULL;
  75. if (number >= 100 || CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
  76. return NULL;
  77. __libc_rwlock_wrlock (__libc_setlocale_lock);
  78. struct lc_time_data *data = current->private;
  79. if (data == NULL)
  80. {
  81. data = calloc (sizeof *data, 1);
  82. if (data == NULL)
  83. goto out;
  84. current->private = data;
  85. }
  86. if (! data->walt_digits_initialized)
  87. {
  88. const wchar_t *ptr = CURRENT_WSTR (_NL_WALT_DIGITS);
  89. size_t cnt;
  90. data->walt_digits_initialized = 1;
  91. if (ptr != NULL)
  92. {
  93. data->walt_digits = malloc (100 * sizeof (const uint32_t *));
  94. if (data->walt_digits != NULL)
  95. for (cnt = 0; cnt < 100; ++cnt)
  96. {
  97. data->walt_digits[cnt] = ptr;
  98. /* Skip digit format. */
  99. ptr = __wcschr (ptr, L'\0') + 1;
  100. }
  101. }
  102. }
  103. if (data->walt_digits != NULL)
  104. result = data->walt_digits[number];
  105. out:
  106. __libc_rwlock_unlock (__libc_setlocale_lock);
  107. return (wchar_t *) result;
  108. }
  109. int
  110. _nl_parse_alt_digit (const char **strp, struct __locale_data *current)
  111. {
  112. const char *str = *strp;
  113. int result = -1;
  114. size_t cnt;
  115. size_t maxlen = 0;
  116. if (CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
  117. return result;
  118. __libc_rwlock_wrlock (__libc_setlocale_lock);
  119. struct lc_time_data *data = _nl_init_alt_digit (current);
  120. if (data != NULL && data->alt_digits != NULL)
  121. /* Matching is not unambiguous. The alternative digits could be like
  122. I, II, III, ... and the first one is a substring of the second
  123. and third. Therefore we must keep on searching until we found
  124. the longest possible match. Note that this is not specified in
  125. the standard. */
  126. for (cnt = 0; cnt < 100; ++cnt)
  127. {
  128. const char *const dig = data->alt_digits[cnt];
  129. size_t len = strlen (dig);
  130. if (len > maxlen && strncmp (dig, str, len) == 0)
  131. {
  132. maxlen = len;
  133. result = (int) cnt;
  134. }
  135. }
  136. __libc_rwlock_unlock (__libc_setlocale_lock);
  137. if (result != -1)
  138. *strp += maxlen;
  139. return result;
  140. }