A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
percent.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
25#ifndef A11_PERCENT_H_
26#define A11_PERCENT_H_
27
28#include <cstddef>
29#include <string>
30#include <string_view>
31
32#include <absl/status/status.h>
33#include <absl/status/statusor.h>
34#include <absl/strings/ascii.h>
35#include <absl/strings/str_cat.h>
36
37namespace a11::percent {
38
40inline int HexDigit(char one) {
41 if (one >= '0' && one <= '9') {
42 return one - '0';
43 }
44 if (one >= 'a' && one <= 'f') {
45 return one - 'a' + 10;
46 }
47 if (one >= 'A' && one <= 'F') {
48 return one - 'A' + 10;
49 }
50 return -1;
51}
52
63inline std::string Decode(std::string_view value, bool plus_is_space = false) {
64 std::string decoded;
65 decoded.reserve(value.size());
66 for (size_t index = 0; index < value.size(); ++index) {
67 const char one = value[index];
68 if (plus_is_space && one == '+') {
69 decoded.push_back(' ');
70 continue;
71 }
72 if (one == '%' && index + 2 < value.size()) {
73 const int high = HexDigit(value[index + 1]);
74 const int low = HexDigit(value[index + 2]);
75 if (high >= 0 && low >= 0) {
76 decoded.push_back(static_cast<char>((high << 4) | low));
77 index += 2;
78 continue;
79 }
80 }
81 decoded.push_back(one);
82 }
83 return decoded;
84}
85
94inline absl::StatusOr<std::string> DecodeStrict(std::string_view value,
95 std::string_view what) {
96 std::string decoded;
97 decoded.reserve(value.size());
98 for (size_t index = 0; index < value.size(); ++index) {
99 if (value[index] != '%') {
100 decoded.push_back(value[index]);
101 continue;
102 }
103 if (index + 2 >= value.size()) {
104 return absl::InvalidArgumentError(
105 absl::StrCat("Truncated percent escape in ", what));
106 }
107 const int high = HexDigit(value[index + 1]);
108 const int low = HexDigit(value[index + 2]);
109 if (high < 0 || low < 0) {
110 return absl::InvalidArgumentError(
111 absl::StrCat("Invalid percent escape in ", what));
112 }
113 decoded.push_back(static_cast<char>((high << 4) | low));
114 index += 2;
115 }
116 return decoded;
117}
118
120inline std::string Encode(std::string_view value) {
121 static constexpr std::string_view kLowerHex = "0123456789abcdef";
122 std::string out;
123 out.reserve(value.size());
124 for (const char ch : value) {
125 const auto byte = static_cast<unsigned char>(ch);
126 if (absl::ascii_isalnum(byte) || byte == '-' || byte == '.' ||
127 byte == '_' || byte == '~') {
128 out.push_back(ch);
129 continue;
130 }
131 out.push_back('%');
132 out.push_back(kLowerHex[byte >> 4U]);
133 out.push_back(kLowerHex[byte & 0x0FU]);
134 }
135 return out;
136}
137
138} // namespace a11::percent
139
140#endif // A11_PERCENT_H_
std::string value
Definition discover.cc:100
Definition percent.h:37
int HexDigit(char one)
The value 0-15 of a hex digit, either case, or -1 if one is not one.
Definition percent.h:40
absl::StatusOr< std::string > DecodeStrict(std::string_view value, std::string_view what)
Percent-decodes value, or says why it cannot be decoded.
Definition percent.h:94
std::string Decode(std::string_view value, bool plus_is_space=false)
Percent-decodes value, leaving anything malformed as it stands.
Definition percent.h:63
std::string Encode(std::string_view value)
Percent-encodes everything outside RFC 3986's unreserved set.
Definition percent.h:120