! @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' */
| 12272 | @throw parse_error.108 if character '~' is not followed by '0' or '1' |
| 12273 | */ |
| 12274 | static std::vector<std::string> split(const std::string& reference_string) |
| 12275 | { |
| 12276 | std::vector<std::string> result; |
| 12277 | |
| 12278 | // special case: empty reference string -> no reference tokens |
| 12279 | if (reference_string.empty()) |
| 12280 | { |
| 12281 | return result; |
| 12282 | } |
| 12283 | |
| 12284 | // check if nonempty reference string begins with slash |
| 12285 | if (JSON_HEDLEY_UNLIKELY(reference_string[0] != '/')) |
| 12286 | { |
| 12287 | JSON_THROW(detail::parse_error::create(107, 1, |
| 12288 | "JSON pointer must be empty or begin with '/' - was: '" + |
| 12289 | reference_string + "'")); |
| 12290 | } |
| 12291 | |
| 12292 | // extract the reference tokens: |
| 12293 | // - slash: position of the last read slash (or end of string) |
| 12294 | // - start: position after the previous slash |
| 12295 | for ( |
| 12296 | // search for the first slash after the first character |
| 12297 | std::size_t slash = reference_string.find_first_of('/', 1), |
| 12298 | // set the beginning of the first reference token |
| 12299 | start = 1; |
| 12300 | // we can stop if start == 0 (if slash == std::string::npos) |
| 12301 | start != 0; |
| 12302 | // set the beginning of the next reference token |
| 12303 | // (will eventually be 0 if slash == std::string::npos) |
| 12304 | start = (slash == std::string::npos) ? 0 : slash + 1, |
| 12305 | // find next slash |
| 12306 | slash = reference_string.find_first_of('/', start)) |
| 12307 | { |
| 12308 | // use the text between the beginning of the reference token |
| 12309 | // (start) and the last slash (slash). |
| 12310 | auto reference_token = reference_string.substr(start, slash - start); |
| 12311 | |
| 12312 | // check reference tokens are properly escaped |
| 12313 | for (std::size_t pos = reference_token.find_first_of('~'); |
| 12314 | pos != std::string::npos; |
| 12315 | pos = reference_token.find_first_of('~', pos + 1)) |
| 12316 | { |
| 12317 | JSON_ASSERT(reference_token[pos] == '~'); |
| 12318 | |
| 12319 | // ~ must be followed by 0 or 1 |
| 12320 | if (JSON_HEDLEY_UNLIKELY(pos == reference_token.size() - 1 || |
| 12321 | (reference_token[pos + 1] != '0' && |
| 12322 | reference_token[pos + 1] != '1'))) |
| 12323 | { |
| 12324 | JSON_THROW(detail::parse_error::create(108, 0, "escape character '~' must be followed with '0' or '1'")); |
| 12325 | } |
| 12326 | } |
| 12327 | |
| 12328 | // finally, store the reference token |
| 12329 | unescape(reference_token); |
| 12330 | result.push_back(reference_token); |
| 12331 | } |