Support escaping other string types
This commit is contained in:
parent
a3492aef21
commit
b9d27b6a1f
@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <nlohmann/detail/macro_scope.hpp>
|
#include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
|
||||||
namespace nlohmann
|
namespace nlohmann
|
||||||
@ -21,12 +20,13 @@ enforced with an assertion.**
|
|||||||
|
|
||||||
@since version 2.0.0
|
@since version 2.0.0
|
||||||
*/
|
*/
|
||||||
inline void replace_substring(std::string& s, const std::string& f,
|
template<typename StringType>
|
||||||
const std::string& t)
|
inline void replace_substring(StringType& s, const StringType& f,
|
||||||
|
const StringType& t)
|
||||||
{
|
{
|
||||||
JSON_ASSERT(!f.empty());
|
JSON_ASSERT(!f.empty());
|
||||||
for (auto pos = s.find(f); // find first occurrence of f
|
for (auto pos = s.find(f); // find first occurrence of f
|
||||||
pos != std::string::npos; // make sure f was found
|
pos != StringType::npos; // make sure f was found
|
||||||
s.replace(pos, f.size(), t), // replace with t, and
|
s.replace(pos, f.size(), t), // replace with t, and
|
||||||
pos = s.find(f, pos + t.size())) // find next occurrence of f
|
pos = s.find(f, pos + t.size())) // find next occurrence of f
|
||||||
{}
|
{}
|
||||||
@ -39,10 +39,11 @@ inline void replace_substring(std::string& s, const std::string& f,
|
|||||||
*
|
*
|
||||||
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
|
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
|
||||||
*/
|
*/
|
||||||
inline std::string escape(std::string s)
|
template<typename StringType>
|
||||||
|
inline StringType escape(StringType s)
|
||||||
{
|
{
|
||||||
replace_substring(s, "~", "~0");
|
replace_substring(s, StringType{"~"}, StringType{"~0"});
|
||||||
replace_substring(s, "/", "~1");
|
replace_substring(s, StringType{"/"}, StringType{"~1"});
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,10 +54,11 @@ inline std::string escape(std::string s)
|
|||||||
*
|
*
|
||||||
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
|
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
|
||||||
*/
|
*/
|
||||||
static void unescape(std::string& s)
|
template<typename StringType>
|
||||||
|
static void unescape(StringType& s)
|
||||||
{
|
{
|
||||||
replace_substring(s, "~1", "/");
|
replace_substring(s, StringType{"~1"}, StringType{"/"});
|
||||||
replace_substring(s, "~0", "~");
|
replace_substring(s, StringType{"~0"}, StringType{"~"});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|||||||
@ -181,7 +181,6 @@ inline bool operator<(const value_t lhs, const value_t rhs) noexcept
|
|||||||
// #include <nlohmann/detail/string_escape.hpp>
|
// #include <nlohmann/detail/string_escape.hpp>
|
||||||
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
// #include <nlohmann/detail/macro_scope.hpp>
|
// #include <nlohmann/detail/macro_scope.hpp>
|
||||||
|
|
||||||
|
|
||||||
@ -2749,12 +2748,13 @@ enforced with an assertion.**
|
|||||||
|
|
||||||
@since version 2.0.0
|
@since version 2.0.0
|
||||||
*/
|
*/
|
||||||
inline void replace_substring(std::string& s, const std::string& f,
|
template<typename StringType>
|
||||||
const std::string& t)
|
inline void replace_substring(StringType& s, const StringType& f,
|
||||||
|
const StringType& t)
|
||||||
{
|
{
|
||||||
JSON_ASSERT(!f.empty());
|
JSON_ASSERT(!f.empty());
|
||||||
for (auto pos = s.find(f); // find first occurrence of f
|
for (auto pos = s.find(f); // find first occurrence of f
|
||||||
pos != std::string::npos; // make sure f was found
|
pos != StringType::npos; // make sure f was found
|
||||||
s.replace(pos, f.size(), t), // replace with t, and
|
s.replace(pos, f.size(), t), // replace with t, and
|
||||||
pos = s.find(f, pos + t.size())) // find next occurrence of f
|
pos = s.find(f, pos + t.size())) // find next occurrence of f
|
||||||
{}
|
{}
|
||||||
@ -2767,10 +2767,11 @@ inline void replace_substring(std::string& s, const std::string& f,
|
|||||||
*
|
*
|
||||||
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
|
* Note the order of escaping "~" to "~0" and "/" to "~1" is important.
|
||||||
*/
|
*/
|
||||||
inline std::string escape(std::string s)
|
template<typename StringType>
|
||||||
|
inline StringType escape(StringType s)
|
||||||
{
|
{
|
||||||
replace_substring(s, "~", "~0");
|
replace_substring(s, StringType{"~"}, StringType{"~0"});
|
||||||
replace_substring(s, "/", "~1");
|
replace_substring(s, StringType{"/"}, StringType{"~1"});
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2781,10 +2782,11 @@ inline std::string escape(std::string s)
|
|||||||
*
|
*
|
||||||
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
|
* Note the order of escaping "~1" to "/" and "~0" to "~" is important.
|
||||||
*/
|
*/
|
||||||
static void unescape(std::string& s)
|
template<typename StringType>
|
||||||
|
static void unescape(StringType& s)
|
||||||
{
|
{
|
||||||
replace_substring(s, "~1", "/");
|
replace_substring(s, StringType{"~1"}, StringType{"/"});
|
||||||
replace_substring(s, "~0", "~");
|
replace_substring(s, StringType{"~0"}, StringType{"~"});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user