MushMachine/external/json/include/nlohmann/detail/string_escape.hpp

73 lines
2.1 KiB
C++
Raw Normal View History

2022-08-02 14:28:39 +02:00
// __ _____ _____ _____
// __| | __| | | | JSON for Modern C++
2022-10-30 02:36:30 +02:00
// | | |__ | | | | | | version 3.11.2
2022-08-02 14:28:39 +02:00
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
//
// SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
// SPDX-License-Identifier: MIT
2021-12-11 23:32:21 +01:00
#pragma once
2022-08-02 14:28:39 +02:00
#include <nlohmann/detail/abi_macros.hpp>
2021-12-11 23:32:21 +01:00
2022-08-02 14:28:39 +02:00
NLOHMANN_JSON_NAMESPACE_BEGIN
2021-12-11 23:32:21 +01:00
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
*/
2022-08-02 14:28:39 +02:00
template<typename StringType>
inline void replace_substring(StringType& s, const StringType& f,
const StringType& t)
2021-12-11 23:32:21 +01:00
{
JSON_ASSERT(!f.empty());
for (auto pos = s.find(f); // find first occurrence of f
2022-08-02 14:28:39 +02:00
pos != StringType::npos; // make sure f was found
2021-12-11 23:32:21 +01:00
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.
*/
2022-08-02 14:28:39 +02:00
template<typename StringType>
inline StringType escape(StringType s)
2021-12-11 23:32:21 +01:00
{
2022-08-02 14:28:39 +02:00
replace_substring(s, StringType{"~"}, StringType{"~0"});
replace_substring(s, StringType{"/"}, StringType{"~1"});
2021-12-11 23:32:21 +01:00
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.
*/
2022-08-02 14:28:39 +02:00
template<typename StringType>
static void unescape(StringType& s)
2021-12-11 23:32:21 +01:00
{
2022-08-02 14:28:39 +02:00
replace_substring(s, StringType{"~1"}, StringType{"/"});
replace_substring(s, StringType{"~0"}, StringType{"~"});
2021-12-11 23:32:21 +01:00
}
2022-08-02 14:28:39 +02:00
} // namespace detail
NLOHMANN_JSON_NAMESPACE_END