Horizon
string_escape.hpp
1 #pragma once
2 
3 #include <string>
4 #include <nlohmann/detail/macro_scope.hpp>
5 
6 namespace nlohmann
7 {
8 namespace detail
9 {
10 
24 inline void replace_substring(std::string& s, const std::string& f,
25  const std::string& t)
26 {
27  JSON_ASSERT(!f.empty());
28  for (auto pos = s.find(f); // find first occurrence of f
29  pos != std::string::npos; // make sure f was found
30  s.replace(pos, f.size(), t), // replace with t, and
31  pos = s.find(f, pos + t.size())) // find next occurrence of f
32  {}
33 }
34 
42 inline std::string escape(std::string s)
43 {
44  replace_substring(s, "~", "~0");
45  replace_substring(s, "/", "~1");
46  return s;
47 }
48 
56 static void unescape(std::string& s)
57 {
58  replace_substring(s, "~1", "/");
59  replace_substring(s, "~0", "~");
60 }
61 
62 } // namespace detail
63 } // namespace nlohmann
std::string escape(std::string s)
string escaping as described in RFC 6901 (Sect. 4)
Definition: string_escape.hpp:42
void replace_substring(std::string &s, const std::string &f, const std::string &t)
replace all occurrences of a substring by another string
Definition: string_escape.hpp:24
namespace for Niels Lohmann
Definition: adl_serializer.hpp:12