Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:02:22

0001 //     __ _____ _____ _____
0002 //  __|  |   __|     |   | |  JSON for Modern C++
0003 // |  |  |__   |  |  | | | |  version 3.11.2
0004 // |_____|_____|_____|_|___|  https://github.com/nlohmann/json
0005 //
0006 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me>
0007 // SPDX-License-Identifier: MIT
0008 
0009 #pragma once
0010 
0011 #include <nlohmann/detail/abi_macros.hpp>
0012 
0013 NLOHMANN_JSON_NAMESPACE_BEGIN
0014 namespace detail
0015 {
0016 
0017 /*!
0018 @brief replace all occurrences of a substring by another string
0019 
0020 @param[in,out] s  the string to manipulate; changed so that all
0021                occurrences of @a f are replaced with @a t
0022 @param[in]     f  the substring to replace with @a t
0023 @param[in]     t  the string to replace @a f
0024 
0025 @pre The search string @a f must not be empty. **This precondition is
0026 enforced with an assertion.**
0027 
0028 @since version 2.0.0
0029 */
0030 template<typename StringType>
0031 inline void replace_substring(StringType& s, const StringType& f,
0032                               const StringType& t)
0033 {
0034     JSON_ASSERT(!f.empty());
0035     for (auto pos = s.find(f);                // find first occurrence of f
0036             pos != StringType::npos;          // make sure f was found
0037             s.replace(pos, f.size(), t),      // replace with t, and
0038             pos = s.find(f, pos + t.size()))  // find next occurrence of f
0039     {}
0040 }
0041 
0042 /*!
0043  * @brief string escaping as described in RFC 6901 (Sect. 4)
0044  * @param[in] s string to escape
0045  * @return    escaped string
0046  *
0047  * Note the order of escaping "~" to "~0" and "/" to "~1" is important.
0048  */
0049 template<typename StringType>
0050 inline StringType escape(StringType s)
0051 {
0052     replace_substring(s, StringType{"~"}, StringType{"~0"});
0053     replace_substring(s, StringType{"/"}, StringType{"~1"});
0054     return s;
0055 }
0056 
0057 /*!
0058  * @brief string unescaping as described in RFC 6901 (Sect. 4)
0059  * @param[in] s string to unescape
0060  * @return    unescaped string
0061  *
0062  * Note the order of escaping "~1" to "/" and "~0" to "~" is important.
0063  */
0064 template<typename StringType>
0065 static void unescape(StringType& s)
0066 {
0067     replace_substring(s, StringType{"~1"}, StringType{"/"});
0068     replace_substring(s, StringType{"~0"}, StringType{"~"});
0069 }
0070 
0071 }  // namespace detail
0072 NLOHMANN_JSON_NAMESPACE_END