Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
#ifndef ADA_HELPERS_H
#define ADA_HELPERS_H
#include "ada/common_defs.h"
#include "ada/url_base.h"
#include <string>
#include <string_view>
#include <optional>
#if ADA_DEVELOPMENT_CHECKS
#include <iostream>
#endif // ADA_DEVELOPMENT_CHECKS
namespace ada::helpers {
template <typename out_iter>
void encode_json(std::string_view view, out_iter out);
ada_really_inline std::optional<std::string_view> prune_hash(
std::string_view& input) noexcept;
ada_really_inline bool shorten_path(std::string& path, ada::scheme::type type);
ada_really_inline bool shorten_path(std::string_view& path,
ada::scheme::type type);
ada_really_inline void parse_prepared_path(std::string_view input,
ada::scheme::type type,
std::string& path);
ada_really_inline void remove_ascii_tab_or_newline(std::string& input);
ada_really_inline constexpr std::string_view substring(std::string_view input,
size_t pos);
bool overlaps(std::string_view input1, const std::string& input2) noexcept;
ada_really_inline constexpr std::string_view substring(std::string_view input,
size_t pos1,
size_t pos2) {
#if ADA_DEVELOPMENT_CHECKS
if (pos2 < pos1) {
std::cerr << "Negative-length substring: [" << pos1 << " to " << pos2 << ")"
<< std::endl;
abort();
}
#endif
return input.substr(pos1, pos2 - pos1);
}
ada_really_inline void resize(std::string_view& input, size_t pos) noexcept;
ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
bool is_special, std::string_view& view) noexcept;
void trim_c0_whitespace(std::string_view& input) noexcept;
template <class url_type>
ada_really_inline void strip_trailing_spaces_from_opaque_path(url_type& url);
ada_really_inline size_t
find_authority_delimiter_special(std::string_view view) noexcept;
ada_really_inline size_t
find_authority_delimiter(std::string_view view) noexcept;
template <typename T, typename... Args>
inline void inner_concat(std::string& buffer, T t) {
buffer.append(t);
}
template <typename T, typename... Args>
inline void inner_concat(std::string& buffer, T t, Args... args) {
buffer.append(t);
return inner_concat(buffer, args...);
}
template <typename... Args>
std::string concat(Args... args) {
std::string answer;
inner_concat(answer, args...);
return answer;
}
inline int leading_zeroes(uint32_t input_num) noexcept {
#if ADA_REGULAR_VISUAL_STUDIO
unsigned long leading_zero(0);
unsigned long in(input_num);
return _BitScanReverse(&leading_zero, in) ? int(31 - leading_zero) : 32;
#else
return __builtin_clz(input_num);
#endif // ADA_REGULAR_VISUAL_STUDIO
}
inline int fast_digit_count(uint32_t x) noexcept {
auto int_log2 = [](uint32_t z) -> int {
return 31 - ada::helpers::leading_zeroes(z | 1);
};
// Compiles to very few instructions. Note that the
// table is static and thus effectively a constant.
// We leave it inside the function because it is meaningless
// outside of it (this comes at no performance cost).
const static uint64_t table[] = {
4294967296, 8589934582, 8589934582, 8589934582, 12884901788,
12884901788, 12884901788, 17179868184, 17179868184, 17179868184,
21474826480, 21474826480, 21474826480, 21474826480, 25769703776,
25769703776, 25769703776, 30063771072, 30063771072, 30063771072,
34349738368, 34349738368, 34349738368, 34349738368, 38554705664,
38554705664, 38554705664, 41949672960, 41949672960, 41949672960,
42949672960, 42949672960};
return int((x + table[int_log2(x)]) >> 32);
}
} // namespace ada::helpers
#endif // ADA_HELPERS_H
common_defs.h
Cross-platform compiler macros and common definitions.
ada_really_inline
#define ada_really_inline
Definition common_defs.h:85
ada::helpers
Includes the definitions for helper functions.
ada::scheme::type
type
Enumeration of URL scheme types.
Definition scheme.h:41
ada::url
Represents a parsed URL with individual string components.
Definition url.h:62
url_base.h
Base class and common definitions for URL types.