Merge pull request #7269 from minggo/fix-crash

Fix crash
This commit is contained in:
minggo 2014-07-01 16:30:50 +08:00
commit 05ca61d62b
3 changed files with 133 additions and 101 deletions

View File

@ -861,24 +861,25 @@ void Node::enumerateChildren(const std::string &name, std::function<bool (Node *
}
}
// TODO: support ends with '/..'
// End with '/..'?
bool searchFromParent = false;
if (length > 3 &&
name[length-3] == '/' &&
name[length-2] == '.' &&
name[length-1] == '.')
{
searchFromParent = true;
subStrlength -= 3;
}
// bool searchFromParent = false;
// if (length > 3 &&
// name[length-3] == '/' &&
// name[length-2] == '.' &&
// name[length-1] == '.')
// {
// searchFromParent = true;
// subStrlength -= 3;
// }
// Remove '/', '//' and '/..' if exist
// Remove '/', '//' if exist
std::string newName = name.substr(subStrStartPos, subStrlength);
// If search from parent, then add * at first to make it match its children, which will do make
if (searchFromParent)
{
newName.insert(0, "[[:alnum:]]+/");
}
// if (searchFromParent)
// {
// newName.insert(0, "[[:alnum:]]+/");
// }
if (searchFromRoot)
{
@ -943,10 +944,14 @@ bool Node::doEnumerate(std::string name, std::function<bool (Node *)> callback)
needRecursive = true;
}
std::hash<std::string> h;
size_t hashOfName = h(searchName);
bool ret = false;
for (const auto& child : _children)
{
if(std::regex_match(child->_name, std::regex(searchName)))
// TODO: regular expression support
// Android doesn't support c++ 11 regular expression well, may use external lib
if (hashOfName == child->_hashOfName && searchName.compare(child->_name) == 0)
{
if (!needRecursive)
{

View File

@ -714,20 +714,17 @@ public:
virtual Node* getChildByName(const std::string& name) const;
/** Search the children of the receiving node to perform processing for nodes which share a name.
*
* @param name The name to search for, support c++11 regular expression
* @param name The name to search for
* Search syntax options:
* `/` : When placed at the start of the search string, this indicates that the search should be performed on the tree's node.
* `//`: Can only be placed at the begin of the search string. This indicates that the search should be performed on the tree's node
* and be performed recursively across the entire node tree.
* `..`: The search should move up to the node's parent. Can only be placed at the end of string
* `/` : When placed anywhere but the start of the search string, this indicates that the search should move to the node's children
*
* @code
* enumerateChildren("/MyName", ...): This searches the root's children and matches any node with the name `MyName`.
* enumerateChildren("//MyName", ...): This searches the root's children recursively and matches any node with the name `MyName`.
* enumerateChildren("[[:alnum:]]+", ...): This search string matches every node of its children.
* enumerateChildren("/MyName", ...): This searches the node tree and matches the parent node of every node named `MyName`.
* enumerateChildren("A[[:digit:]]", ...): This searches the node's children and returns any child named `A0`, `A1`, ..., `A9`
* enumerateChildren("Abby/Normal", ...): This searches the node's grandchildren and returns any node whose name is `Normal`
* and whose parent is named `Abby`.
* enumerateChildren("//Abby/Normal", ...): This searches the node tree and returns any node whose name is `Normal` and whose

View File

@ -1269,28 +1269,28 @@ void NodeNameTest::onEnter()
// enumerateChildren()
// name = regular expression
int i = 0;
parent = Node::create();
for (int i = 0; i < 100; ++i)
{
auto node = Node::create();
sprintf(name, "node%d", i);
node->setName(name);
parent->addChild(node);
}
i = 0;
parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 100, "");
i = 0;
parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
// parent = Node::create();
// for (int i = 0; i < 100; ++i)
// {
// auto node = Node::create();
// sprintf(name, "node%d", i);
// node->setName(name);
// parent->addChild(node);
// }
//
// i = 0;
// parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 100, "");
//
// i = 0;
// parent->enumerateChildren("node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return true;
// });
// CCAssert(i == 1, "");
// enumerateChildren
@ -1326,89 +1326,119 @@ void NodeNameTest::onEnter()
});
CCAssert(i == 1, "");
i = 0;
parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 10000, "");
i = 0;
parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
// search from parent
// name is xxx/..
i = 0;
parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 10000, "");
// name = /xxx : search from root
// search from root
parent = getScene();
for (int j = 0; j < 100; j++)
for (int i = 0; i < 100; ++i)
{
auto node = Node::create();
sprintf(name, "node%d", j);
node->setName(name);
node->setName("node");
parent->addChild(node);
for (int k = 0; k < 100; ++k)
for (int j = 0; j < 100; ++j)
{
auto child = Node::create();
sprintf(name, "node%d", k);
child->setName(name);
child->setName("child");
node->addChild(child);
}
}
i = 0;
enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
parent->enumerateChildren("/node", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 100, "");
i = 0;
enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 10100, ""); // 10000(children) + 100(parent)
i = 0;
enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
++i;
return true;
});
CCAssert(i == 1, "");
i = 0;
enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
parent->enumerateChildren("//child", [&i](Node* node) -> bool {
++i;
return false;
});
CCAssert(i == 10000, "");
// i = 0;
// parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 10000, "");
//
// i = 0;
// parent->enumerateChildren("node[[:digit:]]+/node", [&i](Node* node) -> bool {
// ++i;
// return true;
// });
// CCAssert(i == 1, "");
// search from parent
// name is xxx/..
// i = 0;
// parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
// ++i;
// return true;
// });
// CCAssert(i == 1, "");
//
// i = 0;
// parent->enumerateChildren("node/..", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 10000, "");
// name = /xxx : search from root
// parent = getScene();
// for (int j = 0; j < 100; j++)
// {
// auto node = Node::create();
// sprintf(name, "node%d", j);
// node->setName(name);
// parent->addChild(node);
//
// for (int k = 0; k < 100; ++k)
// {
// auto child = Node::create();
// sprintf(name, "node%d", k);
// child->setName(name);
// node->addChild(child);
// }
// }
//
// i = 0;
// enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 100, "");
// i = 0;
// enumerateChildren("/node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return true;
// });
// CCAssert(i == 1, "");
//
// i = 0;
// enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 10100, ""); // 10000(children) + 100(parent)
//
// i = 0;
// enumerateChildren("//node[[:digit:]]+", [&i](Node* node) -> bool {
// ++i;
// return true;
// });
// CCAssert(i == 1, "");
//
// i = 0;
// enumerateChildren("//node[[:digit:]]+/..", [&i](Node* node) -> bool {
// ++i;
// return false;
// });
// CCAssert(i == 10000, "");
// utils::findChildren()
parent = Node::create();