3#ifndef OCPP_COMMON_STRING_HPP
4#define OCPP_COMMON_STRING_HPP
15 using std::runtime_error::runtime_error;
18enum class StringTooLarge {
27 static constexpr size_t length = L;
31 explicit String(
const std::string& data, StringTooLarge to_large = StringTooLarge::Throw) {
32 this->
set(data, to_large);
35 explicit String(
const char* data, StringTooLarge to_large = StringTooLarge::Throw) {
36 this->
set(data, to_large);
44 std::string
get()
const {
49 void set(
const std::string& data, StringTooLarge to_large = StringTooLarge::Throw) {
50 std::string_view view = data;
51 if (view.length() > this->length) {
52 if (to_large == StringTooLarge::Throw) {
54 ") exceeds permitted length (" + std::to_string(this->length) +
")");
57 view = view.substr(0, length);
74 operator std::string() {
80template <
size_t L>
bool operator==(
const String<L>& lhs,
const char* rhs) {
81 return lhs.get() == rhs;
85template <
size_t L>
bool operator==(
const String<L>& lhs,
const String<L>& rhs) {
86 return lhs.get() == rhs.get();
90template <
size_t L>
bool operator!=(
const String<L>& lhs,
const char* rhs) {
91 return !(lhs.get() == rhs);
95template <
size_t L>
bool operator!=(
const String<L>& lhs,
const String<L>& rhs) {
96 return !(lhs.get() == rhs.get());
101template <
size_t L> std::ostream& operator<<(std::ostream& os,
const String<L>& str) {
Definition: string.hpp:14
Contains a String impementation with a maximum length.
Definition: string.hpp:24
void set(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Sets the content of the string to the given data.
Definition: string.hpp:49
String()=default
Creates a string.
std::string get() const
Provides a std::string representation of the string.
Definition: string.hpp:44
String(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Creates a string from the given data.
Definition: string.hpp:31
bool is_valid(std::string_view data)
Override this to check for a specific format.
Definition: string.hpp:68