Merge pull request #68917 from Mickeon/double-the-pride-twice-the-fall

Double precision of `String.split_floats`
This commit is contained in:
Rémi Verschelde 2022-11-20 15:37:31 +01:00
commit ead18458c7
No known key found for this signature in database
GPG Key ID: C3336907360768E1
6 changed files with 16 additions and 15 deletions

View File

@ -1256,8 +1256,8 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int
return ret;
}
Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
Vector<float> ret;
Vector<double> String::split_floats(const String &p_splitter, bool p_allow_empty) const {
Vector<double> ret;
int from = 0;
int len = length();

View File

@ -348,7 +348,7 @@ public:
Vector<String> split(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> split_spaces() const;
Vector<float> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;
Vector<int> split_ints_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;

View File

@ -738,7 +738,7 @@
</description>
</method>
<method name="split_floats" qualifiers="const">
<return type="PackedFloat32Array" />
<return type="PackedFloat64Array" />
<param index="0" name="delimiter" type="String" />
<param index="1" name="allow_empty" type="bool" default="true" />
<description>

View File

@ -1363,7 +1363,7 @@ void EditorHelp::_update_doc() {
if (constants[i].value.begins_with("Color(") && constants[i].value.ends_with(")")) {
String stripped = constants[i].value.replace(" ", "").replace("Color(", "").replace(")", "");
Vector<float> color = stripped.split_floats(",");
PackedFloat64Array color = stripped.split_floats(",");
if (color.size() >= 3) {
class_desc->push_color(Color(color[0], color[1], color[2]));
_add_bulletpoint();

View File

@ -1860,7 +1860,7 @@ void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
if (valid) {
color_args = line.substr(begin, end - begin);
String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
Vector<float> color = stripped.split_floats(",");
PackedFloat64Array color = stripped.split_floats(",");
if (color.size() > 2) {
float alpha = color.size() > 3 ? color[3] : 1.0f;
color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));

View File

@ -516,21 +516,22 @@ TEST_CASE("[String] Splitting") {
s = "1.2;2.3 4.5";
const double slices_d[3] = { 1.2, 2.3, 4.5 };
Vector<float> f;
f = s.split_floats(";");
CHECK(f.size() == 2);
for (int i = 0; i < f.size(); i++) {
CHECK(ABS(f[i] - slices_d[i]) <= 0.00001);
Vector<double> d_arr;
d_arr = s.split_floats(";");
CHECK(d_arr.size() == 2);
for (int i = 0; i < d_arr.size(); i++) {
CHECK(ABS(d_arr[i] - slices_d[i]) <= 0.00001);
}
Vector<String> keys;
keys.push_back(";");
keys.push_back(" ");
f = s.split_floats_mk(keys);
CHECK(f.size() == 3);
for (int i = 0; i < f.size(); i++) {
CHECK(ABS(f[i] - slices_d[i]) <= 0.00001);
Vector<float> f_arr;
f_arr = s.split_floats_mk(keys);
CHECK(f_arr.size() == 3);
for (int i = 0; i < f_arr.size(); i++) {
CHECK(ABS(f_arr[i] - slices_d[i]) <= 0.00001);
}
s = "1;2 4";