plural_rules.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**************************************************************************/
  2. /* plural_rules.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "plural_rules.h"
  31. #include "core/math/expression.h"
  32. int PluralRules::_eq_test(const Array &p_input_val, const Ref<EQNode> &p_node, const Variant &p_result) const {
  33. if (p_node.is_null()) {
  34. return p_result;
  35. }
  36. static const Vector<String> input_name = { "n" };
  37. Error err = expr->parse(p_node->regex, input_name);
  38. ERR_FAIL_COND_V_MSG(err != OK, 0, vformat("Cannot parse expression \"%s\". Error: %s", p_node->regex, expr->get_error_text()));
  39. Variant result = expr->execute(p_input_val);
  40. ERR_FAIL_COND_V_MSG(expr->has_execute_failed(), 0, vformat("Cannot evaluate expression \"%s\".", p_node->regex));
  41. if (bool(result)) {
  42. return _eq_test(p_input_val, p_node->left, result);
  43. } else {
  44. return _eq_test(p_input_val, p_node->right, result);
  45. }
  46. }
  47. int PluralRules::_find_unquoted(const String &p_src, char32_t p_chr) const {
  48. const int len = p_src.length();
  49. if (len == 0) {
  50. return -1;
  51. }
  52. const char32_t *src = p_src.get_data();
  53. bool in_quote = false;
  54. for (int i = 0; i < len; i++) {
  55. if (in_quote) {
  56. if (src[i] == ')') {
  57. in_quote = false;
  58. }
  59. } else {
  60. if (src[i] == '(') {
  61. in_quote = true;
  62. } else if (src[i] == p_chr) {
  63. return i;
  64. }
  65. }
  66. }
  67. return -1;
  68. }
  69. void PluralRules::_cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node) {
  70. // Some examples of p_plural_rule passed in can have the form:
  71. // "n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5" (Arabic)
  72. // "n >= 2" (French) // When evaluating the last, especially careful with this one.
  73. // "n != 1" (English)
  74. String rule = p_plural_rule;
  75. if (rule.begins_with("(") && rule.ends_with(")")) {
  76. int bcount = 0;
  77. for (int i = 1; i < rule.length() - 1 && bcount >= 0; i++) {
  78. if (rule[i] == '(') {
  79. bcount++;
  80. } else if (rule[i] == ')') {
  81. bcount--;
  82. }
  83. }
  84. if (bcount == 0) {
  85. rule = rule.substr(1, rule.length() - 2);
  86. }
  87. }
  88. int first_ques_mark = _find_unquoted(rule, '?');
  89. int first_colon = _find_unquoted(rule, ':');
  90. if (first_ques_mark == -1) {
  91. p_node->regex = rule.strip_edges();
  92. return;
  93. }
  94. p_node->regex = rule.substr(0, first_ques_mark).strip_edges();
  95. p_node->left.instantiate();
  96. _cache_plural_tests(rule.substr(first_ques_mark + 1, first_colon - first_ques_mark - 1).strip_edges(), p_node->left);
  97. p_node->right.instantiate();
  98. _cache_plural_tests(rule.substr(first_colon + 1).strip_edges(), p_node->right);
  99. }
  100. int PluralRules::evaluate(int p_n) const {
  101. const int *cached = cache.getptr(p_n);
  102. if (cached) {
  103. return *cached;
  104. }
  105. const Array &input_val = { p_n };
  106. int index = _eq_test(input_val, equi_tests, 0);
  107. cache.insert(p_n, index);
  108. return index;
  109. }
  110. PluralRules::PluralRules(int p_nplurals, const String &p_plural) :
  111. nplurals(p_nplurals),
  112. plural(p_plural) {
  113. equi_tests.instantiate();
  114. _cache_plural_tests(plural, equi_tests);
  115. expr.instantiate();
  116. }
  117. PluralRules *PluralRules::parse(const String &p_rules) {
  118. // `p_rules` should be in the format "nplurals=<N>; plural=<Expression>;".
  119. const int nplurals_eq = p_rules.find_char('=');
  120. ERR_FAIL_COND_V_MSG(nplurals_eq == -1, nullptr, "Invalid plural rules format. Missing equal sign for `nplurals`.");
  121. const int nplurals_semi_col = p_rules.find_char(';', nplurals_eq);
  122. ERR_FAIL_COND_V_MSG(nplurals_semi_col == -1, nullptr, "Invalid plural rules format. Missing semicolon for `nplurals`.");
  123. const String nplurals_str = p_rules.substr(nplurals_eq + 1, nplurals_semi_col - (nplurals_eq + 1)).strip_edges();
  124. ERR_FAIL_COND_V_MSG(!nplurals_str.is_valid_int(), nullptr, "Invalid plural rules format. `nplurals` should be an integer.");
  125. const int nplurals = nplurals_str.to_int();
  126. ERR_FAIL_COND_V_MSG(nplurals < 1, nullptr, "Invalid plural rules format. `nplurals` should be at least 1.");
  127. const int expression_eq = p_rules.find_char('=', nplurals_semi_col + 1);
  128. ERR_FAIL_COND_V_MSG(expression_eq == -1, nullptr, "Invalid plural rules format. Missing equal sign for `plural`.");
  129. int expression_end = p_rules.rfind_char(';');
  130. if (expression_end == -1) {
  131. WARN_PRINT("Invalid plural rules format. Missing semicolon at the end of `plural` expression. Assuming ends at the end of the string.");
  132. expression_end = p_rules.length();
  133. }
  134. const int expression_start = expression_eq + 1;
  135. ERR_FAIL_COND_V_MSG(expression_end <= expression_start, nullptr, "Invalid plural rules format. `plural` expression is empty.");
  136. const String &plural = p_rules.substr(expression_start, expression_end - expression_start).strip_edges();
  137. return memnew(PluralRules(nplurals, plural));
  138. }