Revise serial naming behavior

This commit is contained in:
Pedro J. Estébanez 2016-10-10 13:06:13 +02:00
parent fea1fb0925
commit 84c525ba1b

View file

@ -1310,6 +1310,7 @@ String Node::_generate_serial_child_name(Node *p_child) {
name = p_child->get_type(); name = p_child->get_type();
} }
// Extract trailing number
String nums; String nums;
for(int i=name.length()-1;i>=0;i--) { for(int i=name.length()-1;i>=0;i--) {
CharType n=name[i]; CharType n=name[i];
@ -1320,18 +1321,20 @@ String Node::_generate_serial_child_name(Node *p_child) {
} }
} }
int num=nums.to_int();
if (num<1)
num=1;
String nnsep=_get_name_num_separator(); String nnsep=_get_name_num_separator();
name = name.substr(0,name.length()-nums.length()).strip_edges(); int num=0;
if ( name.substr(name.length()-nnsep.length(),nnsep.length()) == nnsep) { bool explicit_zero=false;
name = name.substr(0,name.length()-nnsep.length()); if (nums.length()>0 && name.substr(name.length()-nnsep.length()-nums.length(),nnsep.length()) == nnsep) {
// Base name + Separator + Number
num=nums.to_int();
name=name.substr(0,name.length()-nnsep.length()-nums.length()); // Keep base name
if (num==0) {
explicit_zero=true;
}
} }
for(;;) { for(;;) {
String attempt = (name + (num > 1 ? nnsep + itos(num) : "")).strip_edges(); String attempt = (name + (num > 0 || explicit_zero ? nnsep + itos(num) : "")).strip_edges();
bool found=false; bool found=false;
for(int i=0;i<data.children.size();i++) { for(int i=0;i<data.children.size();i++) {
if (data.children[i]==p_child) if (data.children[i]==p_child)
@ -1344,7 +1347,17 @@ String Node::_generate_serial_child_name(Node *p_child) {
if (!found) { if (!found) {
return attempt; return attempt;
} else { } else {
num++; if (num==0) {
if (explicit_zero) {
// Name ended in separator + 0; user expects to get to separator + 1
num=1;
} else {
// Name was undecorated so skip to 2 for a more natural result
num=2;
}
} else {
num++;
}
} }
} }
} }