alt_digit.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Helper functions used by strftime/strptime to handle alternate digits.
  2. Copyright (C) 1995-2019 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. <http://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 void
  27. _nl_init_alt_digit (struct __locale_data *current)
  28. {
  29. struct lc_time_data *data;
  30. if (current->private.time == NULL)
  31. {
  32. current->private.time = malloc (sizeof *current->private.time);
  33. if (current->private.time == NULL)
  34. return;
  35. memset (current->private.time, 0, sizeof *current->private.time);
  36. current->private.cleanup = &_nl_cleanup_time;
  37. }
  38. data = current->private.time;
  39. if (! data->alt_digits_initialized)
  40. {
  41. const char *ptr = CURRENT (ALT_DIGITS);
  42. size_t cnt;
  43. data->alt_digits_initialized = 1;
  44. if (ptr != NULL)
  45. {
  46. data->alt_digits = malloc (100 * sizeof (const char *));
  47. if (data->alt_digits != NULL)
  48. for (cnt = 0; cnt < 100; ++cnt)
  49. {
  50. data->alt_digits[cnt] = ptr;
  51. /* Skip digit format. */
  52. ptr = strchr (ptr, '\0') + 1;
  53. }
  54. }
  55. }
  56. }
  57. const char *
  58. _nl_get_alt_digit (unsigned int number, struct __locale_data *current)
  59. {
  60. const char *result;
  61. if (number >= 100 || CURRENT (ALT_DIGITS)[0] == '\0')
  62. return NULL;
  63. __libc_rwlock_wrlock (__libc_setlocale_lock);
  64. if (current->private.time == NULL
  65. || ! current->private.time->alt_digits_initialized)
  66. _nl_init_alt_digit (current);
  67. result = ((current->private.time != NULL
  68. && current->private.time->alt_digits != NULL)
  69. ? current->private.time->alt_digits[number]
  70. : NULL);
  71. __libc_rwlock_unlock (__libc_setlocale_lock);
  72. return result;
  73. }
  74. const wchar_t *
  75. _nl_get_walt_digit (unsigned int number, struct __locale_data *current)
  76. {
  77. const wchar_t *result = NULL;
  78. struct lc_time_data *data;
  79. if (number >= 100 || CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
  80. return NULL;
  81. __libc_rwlock_wrlock (__libc_setlocale_lock);
  82. if (current->private.time == NULL)
  83. {
  84. current->private.time = malloc (sizeof *current->private.time);
  85. if (current->private.time == NULL)
  86. goto out;
  87. memset (current->private.time, 0, sizeof *current->private.time);
  88. current->private.cleanup = &_nl_cleanup_time;
  89. }
  90. data = current->private.time;
  91. if (! data->walt_digits_initialized)
  92. {
  93. const wchar_t *ptr = CURRENT_WSTR (_NL_WALT_DIGITS);
  94. size_t cnt;
  95. data->walt_digits_initialized = 1;
  96. if (ptr != NULL)
  97. {
  98. data->walt_digits = malloc (100 * sizeof (const uint32_t *));
  99. if (data->walt_digits != NULL)
  100. for (cnt = 0; cnt < 100; ++cnt)
  101. {
  102. data->walt_digits[cnt] = ptr;
  103. /* Skip digit format. */
  104. ptr = __wcschr (ptr, L'\0') + 1;
  105. }
  106. }
  107. }
  108. if (data->walt_digits != NULL)
  109. result = data->walt_digits[number];
  110. out:
  111. __libc_rwlock_unlock (__libc_setlocale_lock);
  112. return (wchar_t *) result;
  113. }
  114. int
  115. _nl_parse_alt_digit (const char **strp, struct __locale_data *current)
  116. {
  117. const char *str = *strp;
  118. int result = -1;
  119. size_t cnt;
  120. size_t maxlen = 0;
  121. if (CURRENT_WSTR (_NL_WALT_DIGITS)[0] == L'\0')
  122. return result;
  123. __libc_rwlock_wrlock (__libc_setlocale_lock);
  124. if (current->private.time == NULL
  125. || ! current->private.time->alt_digits_initialized)
  126. _nl_init_alt_digit (current);
  127. if (current->private.time != NULL &&
  128. current->private.time->alt_digits != NULL)
  129. /* Matching is not unambiguous. The alternative digits could be like
  130. I, II, III, ... and the first one is a substring of the second
  131. and third. Therefore we must keep on searching until we found
  132. the longest possible match. Note that this is not specified in
  133. the standard. */
  134. for (cnt = 0; cnt < 100; ++cnt)
  135. {
  136. const char *const dig = current->private.time->alt_digits[cnt];
  137. size_t len = strlen (dig);
  138. if (len > maxlen && strncmp (dig, str, len) == 0)
  139. {
  140. maxlen = len;
  141. result = (int) cnt;
  142. }
  143. }
  144. __libc_rwlock_unlock (__libc_setlocale_lock);
  145. if (result != -1)
  146. *strp += maxlen;
  147. return result;
  148. }