| 99 | |
| 100 | |
| 101 | Var Query::find(const std::string& path) const |
| 102 | { |
| 103 | Var result = _source; |
| 104 | StringTokenizer tokenizer(path, "."); |
| 105 | for (StringTokenizer::Iterator token = tokenizer.begin(); token != tokenizer.end(); token++) |
| 106 | { |
| 107 | if (!result.isEmpty()) |
| 108 | { |
| 109 | std::vector<int> indexes; |
| 110 | RegularExpression::MatchVec matches; |
| 111 | int firstOffset = -1; |
| 112 | int offset = 0; |
| 113 | RegularExpression regex("\\[([0-9]+)\\]"); |
| 114 | while (regex.match(*token, offset, matches) > 0) |
| 115 | { |
| 116 | if (firstOffset == -1) |
| 117 | { |
| 118 | firstOffset = static_cast<int>(matches[0].offset); |
| 119 | } |
| 120 | std::string num = token->substr(matches[1].offset, matches[1].length); |
| 121 | indexes.push_back(NumberParser::parse(num)); |
| 122 | offset = static_cast<int>(matches[0].offset + matches[0].length); |
| 123 | } |
| 124 | |
| 125 | std::string name(*token); |
| 126 | if (firstOffset != -1) |
| 127 | { |
| 128 | name = name.substr(0, firstOffset); |
| 129 | } |
| 130 | |
| 131 | if (name.length() > 0) |
| 132 | { |
| 133 | if (result.type() == typeid(Object::Ptr)) |
| 134 | { |
| 135 | Object::Ptr o = result.extract<Object::Ptr>(); |
| 136 | result = o->get(name); |
| 137 | } |
| 138 | else if (result.type() == typeid(Object)) |
| 139 | { |
| 140 | Object o = result.extract<Object>(); |
| 141 | result = o.get(name); |
| 142 | } |
| 143 | else |
| 144 | result.empty(); |
| 145 | |
| 146 | } |
| 147 | |
| 148 | if (!result.isEmpty() && !indexes.empty()) |
| 149 | { |
| 150 | for (std::vector<int>::iterator it = indexes.begin(); it != indexes.end(); ++it) |
| 151 | { |
| 152 | if (result.type() == typeid(Array::Ptr)) |
| 153 | { |
| 154 | Array::Ptr array = result.extract<Array::Ptr>(); |
| 155 | result = array->get(*it); |
| 156 | if (result.isEmpty()) break; |
| 157 | } |
| 158 | else if (result.type() == typeid(Array)) |
no test coverage detected