! @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' */
| 14345 | @throw parse_error.108 if character '~' is not followed by '0' or '1' |
| 14346 | */ |
| 14347 | static std::vector<string_t> split(const string_t& reference_string) |
| 14348 | { |
| 14349 | std::vector<string_t> result; |
| 14350 | |
| 14351 | // special case: empty reference string -> no reference tokens |
| 14352 | if (reference_string.empty()) |
| 14353 | { |
| 14354 | return result; |
| 14355 | } |
| 14356 | |
| 14357 | // check if nonempty reference string begins with slash |
| 14358 | if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) |
| 14359 | { |
| 14360 | JSON_THROW(detail::parse_error::create(107, 1, detail::concat("JSON pointer must be empty or begin with '/' - was: '", reference_string, "'"), nullptr)); |
| 14361 | } |
| 14362 | |
| 14363 | // extract the reference tokens: |
| 14364 | // - slash: position of the last read slash (or end of string) |
| 14365 | // - start: position after the previous slash |
| 14366 | for ( |
| 14367 | // search for the first slash after the first character |
| 14368 | std::size_t slash = reference_string.find_first_of('/', 1), |
| 14369 | // set the beginning of the first reference token |
| 14370 | start = 1; |
| 14371 | // we can stop if start == 0 (if slash == string_t::npos) |
| 14372 | start != 0; |
| 14373 | // set the beginning of the next reference token |
| 14374 | // (will eventually be 0 if slash == string_t::npos) |
| 14375 | start = (slash == string_t::npos) ? 0 : slash + 1, |
| 14376 | // find next slash |
| 14377 | slash = reference_string.find_first_of('/', start)) |
| 14378 | { |
| 14379 | // use the text between the beginning of the reference token |
| 14380 | // (start) and the last slash (slash). |
| 14381 | auto reference_token = reference_string.substr(start, slash - start); |
| 14382 | |
| 14383 | // check reference tokens are properly escaped |
| 14384 | for (std::size_t pos = reference_token.find_first_of('~'); |
| 14385 | pos != string_t::npos; |
| 14386 | pos = reference_token.find_first_of('~', pos + 1)) |
| 14387 | { |
| 14388 | JSON_ASSERT(reference_token[pos] == '~'); |
| 14389 | |
| 14390 | // ~ must be followed by 0 or 1 |
| 14391 | if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || |
| 14392 | (reference_token[pos + 1] != '0' && |
| 14393 | reference_token[pos + 1] != '1'))) |
| 14394 | { |
| 14395 | JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'", nullptr)); |
| 14396 | } |
| 14397 | } |
| 14398 | |
| 14399 | // finally, store the reference token |
| 14400 | detail::unescape(reference_token); |
| 14401 | result.push_back(reference_token); |
| 14402 | } |
| 14403 | |
| 14404 | return result; |