Skip to content

Commit ebb96e2

Browse files
committed
Move plural rules logic into a separate class
- Extracts plural rules logic in `TranslationPO` into a new `PluralRules` class. - Changes caching the last used plural index in `TranslationPO` into an LRU cache in `PluralRules`. - Adds tests for `PluralRules`.
1 parent 1f7630f commit ebb96e2

File tree

5 files changed

+311
-150
lines changed

5 files changed

+311
-150
lines changed

core/string/plural_rules.cpp

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
31+
#include "plural_rules.h"
32+
33+
#include "core/math/expression.h"
34+
35+
int PluralRules::_eq_test(const Array &p_input_val, const Ref<EQNode> &p_node, const Variant &p_result) const {
36+
if (p_node.is_null()) {
37+
return p_result;
38+
}
39+
40+
static const Vector<String> input_name = { "n" };
41+
42+
Error err = expr->parse(p_node->regex, input_name);
43+
ERR_FAIL_COND_V_MSG(err != OK, 0, vformat("Cannot parse expression \"%s\". Error: %s", p_node->regex, expr->get_error_text()));
44+
45+
Variant result = expr->execute(p_input_val);
46+
ERR_FAIL_COND_V_MSG(expr->has_execute_failed(), 0, vformat("Cannot evaluate expression \"%s\".", p_node->regex));
47+
48+
if (bool(result)) {
49+
return _eq_test(p_input_val, p_node->left, result);
50+
} else {
51+
return _eq_test(p_input_val, p_node->right, result);
52+
}
53+
}
54+
55+
int PluralRules::_find_unquoted(const String &p_src, char32_t p_chr) const {
56+
const int len = p_src.length();
57+
if (len == 0) {
58+
return -1;
59+
}
60+
61+
const char32_t *src = p_src.get_data();
62+
bool in_quote = false;
63+
for (int i = 0; i < len; i++) {
64+
if (in_quote) {
65+
if (src[i] == ')') {
66+
in_quote = false;
67+
}
68+
} else {
69+
if (src[i] == '(') {
70+
in_quote = true;
71+
} else if (src[i] == p_chr) {
72+
return i;
73+
}
74+
}
75+
}
76+
77+
return -1;
78+
}
79+
80+
void PluralRules::_cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node) {
81+
// Some examples of p_plural_rule passed in can have the form:
82+
// "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)
83+
// "n >= 2" (French) // When evaluating the last, especially careful with this one.
84+
// "n != 1" (English)
85+
86+
String rule = p_plural_rule;
87+
if (rule.begins_with("(") && rule.ends_with(")")) {
88+
int bcount = 0;
89+
for (int i = 1; i < rule.length() - 1 && bcount >= 0; i++) {
90+
if (rule[i] == '(') {
91+
bcount++;
92+
} else if (rule[i] == ')') {
93+
bcount--;
94+
}
95+
}
96+
if (bcount == 0) {
97+
rule = rule.substr(1, rule.length() - 2);
98+
}
99+
}
100+
101+
int first_ques_mark = _find_unquoted(rule, '?');
102+
int first_colon = _find_unquoted(rule, ':');
103+
104+
if (first_ques_mark == -1) {
105+
p_node->regex = rule.strip_edges();
106+
return;
107+
}
108+
109+
p_node->regex = rule.substr(0, first_ques_mark).strip_edges();
110+
111+
p_node->left.instantiate();
112+
_cache_plural_tests(rule.substr(first_ques_mark + 1, first_colon - first_ques_mark - 1).strip_edges(), p_node->left);
113+
p_node->right.instantiate();
114+
_cache_plural_tests(rule.substr(first_colon + 1).strip_edges(), p_node->right);
115+
}
116+
117+
int PluralRules::evaluate(int p_n) const {
118+
const int *cached = cache.getptr(p_n);
119+
if (cached) {
120+
return *cached;
121+
}
122+
123+
const Array &input_val = { p_n };
124+
int index = _eq_test(input_val, equi_tests, 0);
125+
cache.insert(p_n, index);
126+
return index;
127+
}
128+
129+
PluralRules::PluralRules(int p_nplurals, const String &p_plural) :
130+
nplurals(p_nplurals),
131+
plural(p_plural) {
132+
equi_tests.instantiate();
133+
_cache_plural_tests(plural, equi_tests);
134+
135+
expr.instantiate();
136+
}
137+
138+
PluralRules *PluralRules::parse(const String &p_rules) {
139+
// `p_rules` should be in the format "nplurals=<N>; plural=<Expression>;".
140+
141+
const int nplurals_eq = p_rules.find_char('=');
142+
ERR_FAIL_COND_V_MSG(nplurals_eq == -1, nullptr, "Invalid plural rules format. Missing equal sign for `nplurals`.");
143+
144+
const int nplurals_semi_col = p_rules.find_char(';', nplurals_eq);
145+
ERR_FAIL_COND_V_MSG(nplurals_semi_col == -1, nullptr, "Invalid plural rules format. Missing semicolon for `nplurals`.");
146+
147+
const String nplurals_str = p_rules.substr(nplurals_eq + 1, nplurals_semi_col - (nplurals_eq + 1)).strip_edges();
148+
ERR_FAIL_COND_V_MSG(!nplurals_str.is_valid_int(), nullptr, "Invalid plural rules format. `nplurals` should be an integer.");
149+
150+
const int nplurals = nplurals_str.to_int();
151+
ERR_FAIL_COND_V_MSG(nplurals < 1, nullptr, "Invalid plural rules format. `nplurals` should be at least 1.");
152+
153+
const int expression_eq = p_rules.find_char('=', nplurals_semi_col + 1);
154+
ERR_FAIL_COND_V_MSG(expression_eq == -1, nullptr, "Invalid plural rules format. Missing equal sign for `plural`.");
155+
156+
int expression_end = p_rules.rfind_char(';');
157+
if (expression_end == -1) {
158+
WARN_PRINT("Invalid plural rules format. Missing semicolon at the end of `plural` expression. Assuming ends at the end of the string.");
159+
expression_end = p_rules.length();
160+
}
161+
162+
const int expression_start = expression_eq + 1;
163+
ERR_FAIL_COND_V_MSG(expression_end <= expression_start, nullptr, "Invalid plural rules format. `plural` expression is empty.");
164+
165+
const String &plural = p_rules.substr(expression_start, expression_end - expression_start).strip_edges();
166+
return memnew(PluralRules(nplurals, plural));
167+
}

core/string/plural_rules.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**************************************************************************/
2+
/* plural_rules.h */
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+
31+
#pragma once
32+
33+
#include "core/object/ref_counted.h"
34+
#include "core/templates/lru.h"
35+
36+
class Expression;
37+
38+
class PluralRules : public Object {
39+
GDSOFTCLASS(PluralRules, Object);
40+
41+
mutable LRUCache<int, int> cache;
42+
43+
// These two fields are initialized in the constructor.
44+
const int nplurals;
45+
const String plural;
46+
47+
// Cache temporary variables related to `evaluate()` to make it faster.
48+
class EQNode : public RefCounted {
49+
GDSOFTCLASS(EQNode, RefCounted);
50+
51+
public:
52+
String regex;
53+
Ref<EQNode> left;
54+
Ref<EQNode> right;
55+
};
56+
Ref<EQNode> equi_tests;
57+
Ref<Expression> expr;
58+
59+
int _find_unquoted(const String &p_src, char32_t p_chr) const;
60+
int _eq_test(const Array &p_input_val, const Ref<EQNode> &p_node, const Variant &p_result) const;
61+
void _cache_plural_tests(const String &p_plural_rule, Ref<EQNode> &p_node);
62+
63+
PluralRules(int p_nplurals, const String &p_plural);
64+
65+
public:
66+
int evaluate(int p_n) const;
67+
68+
int get_nplurals() const { return nplurals; }
69+
String get_plural() const { return plural; }
70+
71+
static PluralRules *parse(const String &p_rules);
72+
};

0 commit comments

Comments
 (0)