forked from glynos/url
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcheck_input.hpp
More file actions
53 lines (43 loc) · 1.65 KB
/
check_input.hpp
File metadata and controls
53 lines (43 loc) · 1.65 KB
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
// Copyright 2020 Glyn Matthews.
// 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 SKYR_CORE_CHECK_INPUT_HPP
#define SKYR_CORE_CHECK_INPUT_HPP
#include <algorithm>
#include <iterator>
#include <locale>
#include <string>
namespace skyr {
constexpr static auto is_c0_control_or_space = [](auto byte) {
return std::iscntrl(byte, std::locale::classic()) || std::isspace(byte, std::locale::classic());
};
constexpr auto remove_leading_c0_control_or_space(std::string_view input, bool* validation_error) {
auto first = std::cbegin(input), last = std::cend(input);
auto it = std::find_if_not(first, last, is_c0_control_or_space);
*validation_error |= (it != first);
input.remove_prefix(std::distance(first, it));
return input;
}
constexpr auto remove_trailing_c0_control_or_space(std::string_view input, bool* validation_error) {
auto first = std::crbegin(input), last = std::crend(input);
auto it = std::find_if_not(first, last, is_c0_control_or_space);
*validation_error |= (it != first);
input.remove_suffix(std::distance(first, it));
return input;
}
constexpr static auto is_tab_or_newline = [](auto byte) { return (byte == '\t') || (byte == '\r') || (byte == '\n'); };
inline auto remove_tabs_and_newlines(std::string_view input, bool* validation_error) -> std::string {
std::string result;
result.reserve(input.size());
for (auto byte : input) {
if (is_tab_or_newline(byte)) {
*validation_error = true;
} else {
result.push_back(byte);
}
}
return result;
}
} // namespace skyr
#endif // SKYR_CORE_CHECK_INPUT_HPP