! @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' */
| 14469 | @throw parse_error.108 if character '~' is not followed by '0' or '1' |
| 14470 | */ |
| 14471 | static std::vector<string_t> split(const string_t& reference_string) |
| 14472 | { |
| 14473 | std::vector<string_t> result; |
| 14474 | |
| 14475 | // special case: empty reference string -> no reference tokens |
| 14476 | if (reference_string.empty()) |
| 14477 | { |
| 14478 | return result; |
| 14479 | } |
| 14480 | |
| 14481 | // check if nonempty reference string begins with slash |
| 14482 | if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) |
| 14483 | { |
| 14484 | JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); |
| 14485 | } |
| 14486 | |
| 14487 | // extract the reference tokens: |
| 14488 | // - slash: position of the last read slash (or end of string) |
| 14489 | // - start: position after the previous slash |
| 14490 | for ( |
| 14491 | // search for the first slash after the first character |
| 14492 | std::size_t slash = reference_string.find_first_of('/', 1), |
| 14493 | // set the beginning of the first reference token |
| 14494 | start = 1; |
| 14495 | // we can stop if start == 0 (if slash == string_t::npos) |
| 14496 | start != 0; |
| 14497 | // set the beginning of the next reference token |
| 14498 | // (will eventually be 0 if slash == string_t::npos) |
| 14499 | start = (slash == string_t::npos) ? 0 : slash + 1, |
| 14500 | // find next slash |
| 14501 | slash = reference_string.find_first_of('/', start)) |
| 14502 | { |
| 14503 | // use the text between the beginning of the reference token |
| 14504 | // (start) and the last slash (slash). |
| 14505 | auto reference_token = reference_string.substr(start, slash - start); |
| 14506 | |
| 14507 | // check reference tokens are properly escaped |
| 14508 | for (std::size_t pos = reference_token.find_first_of('~'); |
| 14509 | pos != string_t::npos; |
| 14510 | pos = reference_token.find_first_of('~', pos + 1)) |
| 14511 | { |
| 14512 | JSON_ASSERT(reference_token[pos] == '~'); |
| 14513 | |
| 14514 | // ~ must be followed by 0 or 1 |
| 14515 | if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || |
| 14516 | (reference_token[pos + 1] != '0' && |
| 14517 | reference_token[pos + 1] != '1'))) |
| 14518 | { |
| 14519 | JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); |
| 14520 | } |
| 14521 | } |
| 14522 | |
| 14523 | // finally, store the reference token |
| 14524 | detail::unescape(reference_token); |
| 14525 | result.push_back(reference_token); |
| 14526 | } |
| 14527 | |
| 14528 | return result; |