ocpp 0.24.1
A C++ implementation of the Open Charge Point Protocol
cistring.hpp
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2020 - 2023 Pionix GmbH and Contributors to EVerest
3#ifndef OCPP_COMMON_CISTRING_HPP
4#define OCPP_COMMON_CISTRING_HPP
5
6#include <nlohmann/json.hpp>
7
8#include <ocpp/common/string.hpp>
9#include <ocpp/common/utils.hpp>
10
11using json = nlohmann::json;
12
13namespace ocpp {
14
16template <size_t L> class CiString : public String<L> {
17
18public:
20 CiString(const std::string& data, StringTooLarge to_large = StringTooLarge::Throw) : String<L>(data, to_large) {
21 }
22
23 CiString(const char* data, StringTooLarge to_large = StringTooLarge::Throw) : String<L>(data, to_large) {
24 }
25
26 CiString(const CiString<L>& data) : String<L>(data.get()) {
27 }
28
30 CiString() : String<L>() {
31 }
32
33 CiString(CiString&&) = default;
34 CiString& operator=(const CiString&) = default;
35 CiString& operator=(CiString&&) = default;
36
38 bool is_valid(std::string_view data) {
39 for (char character : data) {
40 // printable ASCII starts at code 0x20 (space) and ends with code 0x7e (tilde) and 0xa (\n)
41 if ((character < 0x20 || character > 0x7e) && character != 0xa) {
42 throw std::runtime_error("CiString can only contain printable ASCII characters");
43 }
44 }
45 return true;
46 }
47
49 operator std::string() const {
50 return this->get();
51 }
52};
53
55template <size_t L> bool operator==(const CiString<L>& lhs, const char* rhs) {
56 return iequals(lhs.get(), rhs);
57}
58
60template <size_t L> bool operator==(const CiString<L>& lhs, const CiString<L>& rhs) {
61 return iequals(lhs.get(), rhs.get());
62}
63
65template <size_t L> bool operator!=(const CiString<L>& lhs, const char* rhs) {
66 return !(lhs.get() == rhs);
67}
68
70template <size_t L> bool operator!=(const CiString<L>& lhs, const CiString<L>& rhs) {
71 return !(lhs.get() == rhs.get());
72}
73
75template <size_t L> bool operator<(const CiString<L>& lhs, const CiString<L>& rhs) {
76 return lhs.get() < rhs.get();
77}
78
81template <size_t L> std::ostream& operator<<(std::ostream& os, const CiString<L>& str) {
82 os << str.get();
83 return os;
84}
85
86template <size_t L> void to_json(json& j, const CiString<L>& k) {
87 j = json(k.get());
88}
89
90template <size_t L> void from_json(const json& j, CiString<L>& k) {
91 k.set(j);
92}
93
94} // namespace ocpp
95
96#endif
Contains a CaseInsensitive string implementation that only allows printable ASCII characters.
Definition: cistring.hpp:16
bool is_valid(std::string_view data)
CaseInsensitive string implementation only allows printable ASCII characters.
Definition: cistring.hpp:38
CiString(const std::string &data, StringTooLarge to_large=StringTooLarge::Throw)
Creates a string from the given data.
Definition: cistring.hpp:20
CiString()
Creates a string.
Definition: cistring.hpp:30
Contains a String impementation with a maximum length.
Definition: string.hpp:24
String()=default
Creates a string.
std::string get() const
Provides a std::string representation of the string.
Definition: string.hpp:44