_i18n_number.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Copyright (C) 2000, 2004, 2008 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@gnu.org>, 2000.
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. /* Look up the value of the next multibyte character and return its numerical
  17. value if it is one of the digits known in the locale. If *DECIDED is
  18. -1 this means it is not yet decided which form it is and we have to
  19. search through all available digits. Otherwise we know which script
  20. the digits are from. */
  21. static inline char *
  22. outdigit_value (char *s, int n)
  23. {
  24. const char *outdigit;
  25. size_t dlen;
  26. assert (0 <= n && n <= 9);
  27. outdigit = nl_langinfo (_NL_CTYPE_OUTDIGIT0_MB + n);
  28. dlen = strlen (outdigit);
  29. s -= dlen;
  30. while (dlen-- > 0)
  31. s[dlen] = outdigit[dlen];
  32. return s;
  33. }
  34. /* Look up the value of the next multibyte character and return its numerical
  35. value if it is one of the digits known in the locale. If *DECIDED is
  36. -1 this means it is not yet decided which form it is and we have to
  37. search through all available digits. Otherwise we know which script
  38. the digits are from. */
  39. static inline wchar_t
  40. outdigitwc_value (int n)
  41. {
  42. assert (0 <= n && n <= 9);
  43. return nl_langinfo_wc (_NL_CTYPE_OUTDIGIT0_WC + n);
  44. }
  45. static char *
  46. _i18n_number_rewrite (char *w, char *rear_ptr, char *end)
  47. {
  48. char decimal[MB_LEN_MAX + 1];
  49. char thousands[MB_LEN_MAX + 1];
  50. /* "to_outpunct" is a map from ASCII decimal point and thousands-sep
  51. to their equivalent in locale. This is defined for locales which
  52. use extra decimal point and thousands-sep. */
  53. wctrans_t map = wctrans ("to_outpunct");
  54. wint_t wdecimal = towctrans (L_('.'), map);
  55. wint_t wthousands = towctrans (L_(','), map);
  56. if (__builtin_expect (map != NULL, 0))
  57. {
  58. mbstate_t state;
  59. memset (&state, '\0', sizeof (state));
  60. size_t n = wcrtomb (decimal, wdecimal, &state);
  61. if (n == (size_t) -1)
  62. memcpy (decimal, ".", 2);
  63. else
  64. decimal[n] = '\0';
  65. memset (&state, '\0', sizeof (state));
  66. n = wcrtomb (thousands, wthousands, &state);
  67. if (n == (size_t) -1)
  68. memcpy (thousands, ",", 2);
  69. else
  70. thousands[n] = '\0';
  71. }
  72. /* Copy existing string so that nothing gets overwritten. */
  73. char *src;
  74. int use_alloca = (rear_ptr - w) < 4096;
  75. if (__builtin_expect (use_alloca, 1))
  76. src = (char *) alloca (rear_ptr - w);
  77. else
  78. {
  79. src = (char *) malloc (rear_ptr - w);
  80. if (src == NULL)
  81. /* If we cannot allocate the memory don't rewrite the string.
  82. It is better than nothing. */
  83. return w;
  84. }
  85. memcpy (src, w, rear_ptr - w);
  86. char *s = src + (rear_ptr - w);
  87. w = end;
  88. /* Process all characters in the string. */
  89. while (--s >= src)
  90. {
  91. if (*s >= '0' && *s <= '9')
  92. {
  93. if (sizeof (char) == 1)
  94. w = (char *) outdigit_value ((char *) w, *s - '0');
  95. else
  96. *--w = (char) outdigitwc_value (*s - '0');
  97. }
  98. else if (__builtin_expect (map == NULL, 1) || (*s != '.' && *s != ','))
  99. *--w = *s;
  100. else
  101. {
  102. if (sizeof (char) == 1)
  103. {
  104. const char *outpunct = *s == '.' ? decimal : thousands;
  105. size_t dlen = strlen (outpunct);
  106. w -= dlen;
  107. while (dlen-- > 0)
  108. w[dlen] = outpunct[dlen];
  109. }
  110. else
  111. *--w = *s == '.' ? (char) wdecimal : (char) wthousands;
  112. }
  113. }
  114. if (! use_alloca)
  115. free (src);
  116. return w;
  117. }