plural-exp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* Expression parsing for plural form selection.
  2. Copyright (C) 2000-2019 Free Software Foundation, Inc.
  3. Written by Ulrich Drepper <drepper@cygnus.com>, 2000.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifdef HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. #include <ctype.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <plural-exp.h>
  21. #if (defined __GNUC__ && !(defined __APPLE_CC_ && __APPLE_CC__ > 1) && \
  22. !defined __cplusplus) \
  23. || (defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L)
  24. /* These structs are the constant expression for the germanic plural
  25. form determination. It represents the expression "n != 1". */
  26. static const struct expression plvar =
  27. {
  28. .nargs = 0,
  29. .operation = var,
  30. };
  31. static const struct expression plone =
  32. {
  33. .nargs = 0,
  34. .operation = num,
  35. .val =
  36. {
  37. .num = 1
  38. }
  39. };
  40. const struct expression GERMANIC_PLURAL =
  41. {
  42. .nargs = 2,
  43. .operation = not_equal,
  44. .val =
  45. {
  46. .args =
  47. {
  48. [0] = (struct expression *) &plvar,
  49. [1] = (struct expression *) &plone
  50. }
  51. }
  52. };
  53. # define INIT_GERMANIC_PLURAL()
  54. #else
  55. /* For compilers without support for ISO C 99 struct/union initializers:
  56. Initialization at run-time. */
  57. static struct expression plvar;
  58. static struct expression plone;
  59. struct expression GERMANIC_PLURAL;
  60. static void
  61. init_germanic_plural (void)
  62. {
  63. if (plone.val.num == 0)
  64. {
  65. plvar.nargs = 0;
  66. plvar.operation = var;
  67. plone.nargs = 0;
  68. plone.operation = num;
  69. plone.val.num = 1;
  70. GERMANIC_PLURAL.nargs = 2;
  71. GERMANIC_PLURAL.operation = not_equal;
  72. GERMANIC_PLURAL.val.args[0] = &plvar;
  73. GERMANIC_PLURAL.val.args[1] = &plone;
  74. }
  75. }
  76. # define INIT_GERMANIC_PLURAL() init_germanic_plural ()
  77. #endif
  78. void
  79. EXTRACT_PLURAL_EXPRESSION (const char *nullentry,
  80. const struct expression **pluralp,
  81. unsigned long int *npluralsp)
  82. {
  83. if (nullentry != NULL)
  84. {
  85. const char *plural;
  86. const char *nplurals;
  87. plural = strstr (nullentry, "plural=");
  88. nplurals = strstr (nullentry, "nplurals=");
  89. if (plural == NULL || nplurals == NULL)
  90. goto no_plural;
  91. else
  92. {
  93. char *endp;
  94. unsigned long int n;
  95. struct parse_args args;
  96. /* First get the number. */
  97. nplurals += 9;
  98. while (*nplurals != '\0' && isspace ((unsigned char) *nplurals))
  99. ++nplurals;
  100. if (!(*nplurals >= '0' && *nplurals <= '9'))
  101. goto no_plural;
  102. #if defined HAVE_STRTOUL || defined _LIBC
  103. n = strtoul (nplurals, &endp, 10);
  104. #else
  105. for (endp = nplurals, n = 0; *endp >= '0' && *endp <= '9'; endp++)
  106. n = n * 10 + (*endp - '0');
  107. #endif
  108. if (nplurals == endp)
  109. goto no_plural;
  110. *npluralsp = n;
  111. /* Due to the restrictions bison imposes onto the interface of the
  112. scanner function we have to put the input string and the result
  113. passed up from the parser into the same structure which address
  114. is passed down to the parser. */
  115. plural += 7;
  116. args.cp = plural;
  117. if (PLURAL_PARSE (&args) != 0)
  118. goto no_plural;
  119. *pluralp = args.res;
  120. }
  121. }
  122. else
  123. {
  124. /* By default we are using the Germanic form: singular form only
  125. for `one', the plural form otherwise. Yes, this is also what
  126. English is using since English is a Germanic language. */
  127. no_plural:
  128. INIT_GERMANIC_PLURAL ();
  129. *pluralp = &GERMANIC_PLURAL;
  130. *npluralsp = 2;
  131. }
  132. }