summaryrefslogtreecommitdiff
path: root/Build/source/utils/asymptote/LspCpp/network/uri/detail/encode.hpp
blob: be1dec9fd1e78d07120685ad684e616bb5c11150 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Glyn Matthews 2011-2016.
// Copyright 2012 Dean Michael Berris <dberris@google.com>
// Copyright 2012 Google, Inc.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

#ifndef NETWORK_URI_ENCODE_INC
#define NETWORK_URI_ENCODE_INC

#include <network/uri/uri_errors.hpp>
#include <iterator>
#include <cstring>
#include <cassert>
#include <algorithm>

namespace network {
namespace detail {
template <typename CharT>
inline CharT hex_to_letter(CharT in) {
  if ((in >= 0) && (in < 10)) {
    return in + '0';
  }

  if ((in >= 10) && (in < 16)) {
    return in - 10 + 'A';
  }

  return in;
}

template <class charT, class OutputIterator>
void percent_encode(charT in, OutputIterator &out) {
  out++ = '%';
  out++ = hex_to_letter((in >> 4) & 0x0f);
  out++ = hex_to_letter(in & 0x0f);
}

template <class charT>
bool is_unreserved(charT in) {
  return ((in >= 'a') && (in <= 'z')) ||
         ((in >= 'A') && (in <= 'Z')) ||
         ((in >= '0') && (in <= '9')) ||
         (in == '-') ||
         (in == '.') ||
         (in == '_') ||
         (in == '~');
}

template <class charT, class OutputIterator>
void encode_char(charT in, OutputIterator &out, const char *ignore = "") {
  if (is_unreserved(in)) {
    out++ = in;
  } else {
    auto first = ignore, last = ignore + std::strlen(ignore);
    if (std::find(first, last, in) != last) {
      out++ = in;
    } else {
      percent_encode(in, out);
    }
  }
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_user_info(InputIterator first, InputIterator last,
                                OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out, ":");
    ++it;
  }
  return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_host(InputIterator first, InputIterator last,
                           OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out, "[:]");
    ++it;
  }
  return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_port(InputIterator first, InputIterator last,
                           OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out);
    ++it;
  }
  return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_path(InputIterator first, InputIterator last,
                           OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out, "/.@%;=");
    ++it;
  }
  return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_query_component(InputIterator first, InputIterator last,
                                      OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out, "/?");
    ++it;
  }
  return out;
}

template <typename InputIterator, typename OutputIterator>
OutputIterator encode_fragment(InputIterator first, InputIterator last,
                               OutputIterator out) {
  auto it = first;
  while (it != last) {
    detail::encode_char(*it, out, "/.@&l;=%");
    ++it;
  }
  return out;
}

template <class String>
String encode_user_info(const String &user_info) {
  String encoded;
  encode_user_info(std::begin(user_info), std::end(user_info),
                   std::back_inserter(encoded));
  return encoded;
}

template <class String>
String encode_host(const String &host) {
  String encoded;
  encode_host(std::begin(host), std::end(host), std::back_inserter(encoded));
  return encoded;
}

template <class String>
String encode_port(const String &port) {
  String encoded;
  encode_port(std::begin(port), std::end(port), std::back_inserter(encoded));
  return encoded;
}

template <class String>
String encode_path(const String &path) {
  String encoded;
  encode_path(std::begin(path), std::end(path), std::back_inserter(encoded));
  return encoded;
}

template <class String>
String encode_fragment(const String &fragment) {
  String encoded;
  encode_fragment(std::begin(fragment), std::end(fragment),
                  std::back_inserter(encoded));
  return encoded;
}
}  // namespace detail
}  // namespace network

#endif  // NETWORK_URI_ENCODE_INC