grouping.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* Internal header for proving correct grouping in strings of numbers.
  2. Copyright (C) 1995-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
  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. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <limits.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #ifndef MAX
  20. #define MAX(a,b) ({ typeof(a) _a = (a); typeof(b) _b = (b); \
  21. _a > _b ? _a : _b; })
  22. #endif
  23. #ifdef USE_WIDE_CHAR
  24. # include <wctype.h>
  25. # define L_(Ch) L##Ch
  26. # define UCHAR_TYPE wint_t
  27. # define STRING_TYPE wchar_t
  28. #else
  29. # define L_(Ch) Ch
  30. # define UCHAR_TYPE unsigned char
  31. # define STRING_TYPE char
  32. #endif
  33. #include "grouping.h"
  34. /* Find the maximum prefix of the string between BEGIN and END which
  35. satisfies the grouping rules. It is assumed that at least one digit
  36. follows BEGIN directly. */
  37. const STRING_TYPE *
  38. #ifdef USE_WIDE_CHAR
  39. __correctly_grouped_prefixwc (const STRING_TYPE *begin, const STRING_TYPE *end,
  40. wchar_t thousands,
  41. #else
  42. __correctly_grouped_prefixmb (const STRING_TYPE *begin, const STRING_TYPE *end,
  43. const char *thousands,
  44. #endif
  45. const char *grouping)
  46. {
  47. #ifndef USE_WIDE_CHAR
  48. size_t thousands_len;
  49. int cnt;
  50. #endif
  51. if (grouping == NULL)
  52. return end;
  53. #ifndef USE_WIDE_CHAR
  54. thousands_len = strlen (thousands);
  55. #endif
  56. while (end > begin)
  57. {
  58. const STRING_TYPE *cp = end - 1;
  59. const char *gp = grouping;
  60. /* Check first group. */
  61. while (cp >= begin)
  62. {
  63. #ifdef USE_WIDE_CHAR
  64. if (*cp == thousands)
  65. break;
  66. #else
  67. if (cp[thousands_len - 1] == *thousands)
  68. {
  69. for (cnt = 1; thousands[cnt] != '\0'; ++cnt)
  70. if (thousands[cnt] != cp[thousands_len - 1 - cnt])
  71. break;
  72. if (thousands[cnt] == '\0')
  73. break;
  74. }
  75. #endif
  76. --cp;
  77. }
  78. /* We allow the representation to contain no grouping at all even if
  79. the locale specifies we can have grouping. */
  80. if (cp < begin)
  81. return end;
  82. if (end - cp == (int) *gp + 1)
  83. {
  84. /* This group matches the specification. */
  85. const STRING_TYPE *new_end;
  86. if (cp < begin)
  87. /* There is just one complete group. We are done. */
  88. return end;
  89. /* CP points to a thousands separator character. The preceding
  90. remainder of the string from BEGIN to NEW_END is the part we
  91. will consider if there is a grouping error in this trailing
  92. portion from CP to END. */
  93. new_end = cp - 1;
  94. /* Loop while the grouping is correct. */
  95. while (1)
  96. {
  97. /* Get the next grouping rule. */
  98. ++gp;
  99. if (*gp == 0)
  100. /* If end is reached use last rule. */
  101. --gp;
  102. /* Skip the thousands separator. */
  103. --cp;
  104. if (*gp == CHAR_MAX
  105. #if CHAR_MIN < 0
  106. || *gp < 0
  107. #endif
  108. )
  109. {
  110. /* No more thousands separators are allowed to follow. */
  111. while (cp >= begin)
  112. {
  113. #ifdef USE_WIDE_CHAR
  114. if (*cp == thousands)
  115. break;
  116. #else
  117. for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
  118. if (thousands[cnt] != cp[thousands_len - cnt - 1])
  119. break;
  120. if (thousands[cnt] == '\0')
  121. break;
  122. #endif
  123. --cp;
  124. }
  125. if (cp < begin)
  126. /* OK, only digits followed. */
  127. return end;
  128. }
  129. else
  130. {
  131. /* Check the next group. */
  132. const STRING_TYPE *group_end = cp;
  133. while (cp >= begin)
  134. {
  135. #ifdef USE_WIDE_CHAR
  136. if (*cp == thousands)
  137. break;
  138. #else
  139. for (cnt = 0; thousands[cnt] != '\0'; ++cnt)
  140. if (thousands[cnt] != cp[thousands_len - cnt - 1])
  141. break;
  142. if (thousands[cnt] == '\0')
  143. break;
  144. #endif
  145. --cp;
  146. }
  147. if (cp < begin && group_end - cp <= (int) *gp)
  148. /* Final group is correct. */
  149. return end;
  150. if (cp < begin || group_end - cp != (int) *gp)
  151. /* Incorrect group. Punt. */
  152. break;
  153. }
  154. }
  155. /* The trailing portion of the string starting at NEW_END
  156. contains a grouping error. So we will look for a correctly
  157. grouped number in the preceding portion instead. */
  158. end = new_end;
  159. }
  160. else
  161. {
  162. /* Even the first group was wrong; determine maximum shift. */
  163. if (end - cp > (int) *gp + 1)
  164. end = cp + (int) *gp + 1;
  165. else if (cp < begin)
  166. /* This number does not fill the first group, but is correct. */
  167. return end;
  168. else
  169. /* CP points to a thousands separator character. */
  170. end = cp;
  171. }
  172. }
  173. return MAX (begin, end);
  174. }