Blender V5.0
uri_convert.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <cctype>
6#include <cstdio>
7
8#include "uri_convert.hh" /* Own include. */
9
10bool url_encode(const char *str, char *dst, size_t dst_size)
11{
12 size_t i = 0;
13
14 while (*str && i < dst_size - 1) {
15 char c = char(*str);
16
17 if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
18 dst[i++] = *str;
19 }
20 else if (c == ' ') {
21 dst[i++] = '+';
22 }
23 else {
24 if (i + 3 >= dst_size) {
25 /* There is not enough space for %XX. */
26 dst[i] = '\0';
27 return false;
28 }
29 sprintf(&dst[i], "%%%02X", c);
30 i += 3;
31 }
32 ++str;
33 }
34
35 dst[i] = '\0';
36
37 if (*str != '\0') {
38 /* Output buffer was too small. */
39 return false;
40 }
41 return true;
42}
#define str(s)
i
Definition text_draw.cc:230
bool url_encode(const char *str, char *dst, size_t dst_size)
Encodes a string into URL format by converting special characters into percent-encoded sequences.