rublon-ssh/PAM/ssh/include/rublon/static_string.hpp
rublon-bwi 0934902bba
Bwi/v2.3.0 (#17)
* Add base PROXY support implementation

* Remove some dynamic memory allocations

* Rewrite configuration reading module

* Make everything in core connector memory resource aware

* Add logs to check is proxy is used

* Add a proxy fallback, and read proxy from env

* Add config entry to check application

* Cleanup includes

* Ddd configuration dump to check application

* Update rhel8 packages

* Fix http headers bug when using proxy server

* Fix formatting

* Fix bad optional access

* Fix configuration check regresion

* Fix memory management issue, remove strict allocators and make connector more polite to memory overflow errors

* Fix initialization of core handler
2025-07-18 11:48:18 +02:00

110 lines
2.9 KiB
C++

#pragma once
#include <array>
#include <cstddef>
#include <cstring>
#include <string_view>
#include <utility>
namespace rublon {
template < typename T, std::size_t N, std::size_t... Idx >
constexpr std::array< T, N > toStdArray(T (&arr)[N], std::index_sequence< Idx... >) {
return {arr[Idx]...};
}
template < std::size_t NewSize, typename T, std::size_t OldSize, std::size_t... Indexes >
constexpr std::array< T, NewSize > resize(const std::array< T, OldSize > & arr, std::index_sequence< Indexes... >) {
return {arr[Indexes]...};
}
template < std::size_t NewSize, typename T, std::size_t OldSize >
constexpr std::array< T, NewSize > resize(const std::array< T, OldSize > & arr) {
constexpr std::size_t minSize = std::min(OldSize, NewSize);
return resize(arr, std::make_index_sequence< minSize >());
}
namespace details {
class StaticStringBase {};
} // namespace details
// statically allocates a string buffer of (N+1) chars
template < size_t N >
class StaticString : public details::StaticStringBase {
public:
constexpr StaticString() = default;
constexpr StaticString(const char (&chars)[N]) : m_str(toStdArray(chars)), _size{N} {}
constexpr StaticString(std::array< const char, N > chars) : m_str(std::move(chars)), _size{N} {}
constexpr StaticString(const char * str) {
_size = std::min(strlen(str), N);
std::memcpy(m_str.data(), str, _size);
}
void operator=(const char * str) {
_size = std::min(strlen(str), N);
std::memcpy(m_str.data(), str, _size);
}
void operator=(std::string_view str) {
_size = std::min(str.size(), N);
std::memcpy(m_str.data(), str.data(), _size);
}
const char * c_str() const noexcept {
return m_str.data();
}
const char * data() const noexcept {
return m_str.data();
}
char * data() noexcept {
return m_str.data();
}
constexpr char & operator[](size_t pos) {
return m_str[pos];
}
constexpr const char & operator[](size_t pos) const {
return m_str[pos];
}
std::size_t size() const {
return _size;
}
constexpr std::size_t capacity() const noexcept {
return N - 1;
}
StaticString< N > & operator+=(const char * rhs) {
auto remaining = capacity() - _size;
auto rhs_len = std::strlen(rhs);
auto copy_len = std::min(rhs_len, remaining);
std::strncpy(data() + _size, rhs, copy_len);
_size += copy_len;
data()[_size] = '\0'; // null
return *this;
}
template < std::size_t M >
friend class StaticString;
operator std::string_view() const {
return {m_str.data(), size()};
}
private:
std::array< char, N + 1 > m_str{};
std::size_t _size;
};
template < size_t N >
bool operator==(const StaticString< N > & lhs, const char * rhs) {
return strcmp(lhs.c_str(), rhs) == 0;
}
} // namespace rublon