! @brief split the string input to reference tokens @note This function is only called by the json_pointer constructor. All exceptions below are documented there. @throw parse_error.107 if the pointer is not empty or begins with '/' @throw parse_error.108 if character '~' is not followed by '0' or '1' */
| 12346 | @throw parse_error.108 if character '~' is not followed by '0' or '1' |
| 12347 | */ |
| 12348 | static std::vector<std::string> split(const std::string& reference_string) |
| 12349 | { |
| 12350 | std::vector<std::string> result; |
| 12351 | |
| 12352 | // special case: empty reference string -> no reference tokens |
| 12353 | if (reference_string.empty()) |
| 12354 | { |
| 12355 | return result; |
| 12356 | } |
| 12357 | |
| 12358 | // check if nonempty reference string begins with slash |
| 12359 | if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) |
| 12360 | { |
| 12361 | JSON_THROW(detail::parse_error::create(107, 1, |
| 12362 | "JSON pointer must be empty or begin with '/' - was: '" + |
| 12363 | reference_string + "'")); |
| 12364 | } |
| 12365 | |
| 12366 | // extract the reference tokens: |
| 12367 | // - slash: position of the last read slash (or end of string) |
| 12368 | // - start: position after the previous slash |
| 12369 | for ( |
| 12370 | // search for the first slash after the first character |
| 12371 | std::size_t slash = reference_string.find_first_of('/', 1), |
| 12372 | // set the beginning of the first reference token |
| 12373 | start = 1; |
| 12374 | // we can stop if start == 0 (if slash == std::string::npos) |
| 12375 | start != 0; |
| 12376 | // set the beginning of the next reference token |
| 12377 | // (will eventually be 0 if slash == std::string::npos) |
| 12378 | start = (slash == std::string::npos) ? 0 : slash + 1, |
| 12379 | // find next slash |
| 12380 | slash = reference_string.find_first_of('/', start)) |
| 12381 | { |
| 12382 | // use the text between the beginning of the reference token |
| 12383 | // (start) and the last slash (slash). |
| 12384 | auto reference_token = reference_string.substr(start, slash - start); |
| 12385 | |
| 12386 | // check reference tokens are properly escaped |
| 12387 | for (std::size_t pos = reference_token.find_first_of('~'); |
| 12388 | pos != std::string::npos; |
| 12389 | pos = reference_token.find_first_of('~', pos + 1)) |
| 12390 | { |
| 12391 | JSON_ASSERT(reference_token[pos] == '~'); |
| 12392 | |
| 12393 | // ~ must be followed by 0 or 1 |
| 12394 | if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || |
| 12395 | (reference_token[pos + 1] != '0' && |
| 12396 | reference_token[pos + 1] != '1'))) |
| 12397 | { |
| 12398 | JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); |
| 12399 | } |
| 12400 | } |
| 12401 | |
| 12402 | // finally, store the reference token |
| 12403 | unescape(reference_token); |
| 12404 | result.push_back(reference_token); |
| 12405 | } |