616caea27a
* Make exception context optional Change exception context parameter to pointer and replace context with nullptr where appropriate. * Support escaping other string types * Add string concatenation function Add variadic concat() function for concatenating char *, char, and string types. * Replace string concatenations using + with concat() * Template json_pointer on string type Change json_pointer from being templated on basic_json to being templated on string type. * Add unit test for #3388 Closes #3388. * Fix regression test for #2958 * Add backwards compatibility with json_pointer<basic_json> * Update json_pointer docs * Allow comparing different json_pointers * Update version numbers
66 lines
1.8 KiB
C++
66 lines
1.8 KiB
C++
#pragma once
|
|
|
|
#include <nlohmann/detail/macro_scope.hpp>
|
|
|
|
namespace nlohmann
|
|
{
|
|
namespace detail
|
|
{
|
|
|
|
/*!
|
|
@brief replace all occurrences of a substring by another string
|
|
|
|
@param[in,out] s the string to manipulate; changed so that all
|
|
occurrences of @a f are replaced with @a t
|
|
@param[in] f the substring to replace with @a t
|
|
@param[in] t the string to replace @a f
|
|
|
|
@pre The search string @a f must not be empty. **This precondition is
|
|
enforced with an assertion.**
|
|
|
|
@since version 2.0.0
|
|
*/
|
|
template<typename StringType>
|
|
inline void replace_substring(StringType& s, const StringType& f,
|
|
const StringType& t)
|
|
{
|
|
JSON_ASSERT(!f.empty());
|
|
for (auto pos = s.find(f); // find first occurrence of f
|
|
pos != StringType::npos; // make sure f was found
|
|
s.replace(pos, f.size(), t), // replace with t, and
|
|
pos = s.find(f, pos + t.size())) // find next occurrence of f
|
|
{}
|
|
}
|
|
|
|
/*!
|
|
* @brief string escaping as described in RFC 6901 (Sect. 4)
|
|
* @param[in] s string to escape
|
|
* @return escaped string
|
|
*
|
|
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
|
|
*/
|
|
template<typename StringType>
|
|
inline StringType escape(StringType s)
|
|
{
|
|
replace_substring(s, StringType{"~"}, StringType{"~0"});
|
|
replace_substring(s, StringType{"/"}, StringType{"~1"});
|
|
return s;
|
|
}
|
|
|
|
/*!
|
|
* @brief string unescaping as described in RFC 6901 (Sect. 4)
|
|
* @param[in] s string to unescape
|
|
* @return unescaped string
|
|
*
|
|
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
|
|
*/
|
|
template<typename StringType>
|
|
static void unescape(StringType& s)
|
|
{
|
|
replace_substring(s, StringType{"~1"}, StringType{"/"});
|
|
replace_substring(s, StringType{"~0"}, StringType{"~"});
|
|
}
|
|
|
|
} // namespace detail
|
|
} // namespace nlohmann
|