split the string input to reference tokens
| 10288 | |
| 10289 | /// split the string input to reference tokens |
| 10290 | static std::vector<std::string> split(const std::string& reference_string) { |
| 10291 | std::vector<std::string> result; |
| 10292 | |
| 10293 | // special case: empty reference string -> no reference tokens |
| 10294 | if (reference_string.empty()) { return result; } |
| 10295 | |
| 10296 | // check if nonempty reference string begins with slash |
| 10297 | if (reference_string[0] != '/') { |
| 10298 | JSON_THROW(std::domain_error("JSON pointer must be empty or begin with '/'")); |
| 10299 | } |
| 10300 | |
| 10301 | // extract the reference tokens: |
| 10302 | // - slash: position of the last read slash (or end of string) |
| 10303 | // - start: position after the previous slash |
| 10304 | for ( |
| 10305 | // search for the first slash after the first character |
| 10306 | size_t slash = reference_string.find_first_of('/', 1), |
| 10307 | // set the beginning of the first reference token |
| 10308 | start = 1; |
| 10309 | // we can stop if start == string::npos+1 = 0 |
| 10310 | start != 0; |
| 10311 | // set the beginning of the next reference token |
| 10312 | // (will eventually be 0 if slash == std::string::npos) |
| 10313 | start = slash + 1, |
| 10314 | // find next slash |
| 10315 | slash = reference_string.find_first_of('/', start)) { |
| 10316 | // use the text between the beginning of the reference token |
| 10317 | // (start) and the last slash (slash). |
| 10318 | auto reference_token = reference_string.substr(start, slash - start); |
| 10319 | |
| 10320 | // check reference tokens are properly escaped |
| 10321 | for (size_t pos = reference_token.find_first_of('~'); pos != std::string::npos; |
| 10322 | pos = reference_token.find_first_of('~', pos + 1)) { |
| 10323 | assert(reference_token[pos] == '~'); |
| 10324 | |
| 10325 | // ~ must be followed by 0 or 1 |
| 10326 | if (pos == reference_token.size() - 1 or |
| 10327 | (reference_token[pos + 1] != '0' and reference_token[pos + 1] != '1')) { |
| 10328 | JSON_THROW(std::domain_error( |
| 10329 | "escape error: '~' must be followed with '0' or '1'")); |
| 10330 | } |
| 10331 | } |
| 10332 | |
| 10333 | // finally, store the reference token |
| 10334 | unescape(reference_token); |
| 10335 | result.push_back(reference_token); |
| 10336 | } |
| 10337 | |
| 10338 | return result; |
| 10339 | } |
| 10340 | |
| 10341 | private: |
| 10342 | /*! |