SNode.C
Loading...
Searching...
No Matches
String.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "iot/mqtt/types/String.h"
21
22#include "iot/mqtt/types/TypeBase.hpp"
23
24#ifndef DOXYGEN_SHOULD_SKIP_THIS
25
26#include <cstdint>
27#include <vector>
28
29#endif // DOXYGEN_SHOULD_SKIP_THIS
30
31namespace iot::mqtt::types {
32
34 : TypeBase(0) {
35 }
36
37 String::String(const std::string& value) {
38 this->value = std::vector<char>(value.begin(), value.end());
39 stringLength = static_cast<uint16_t>(value.size());
40 }
41
43 }
44
45 std::size_t String::deserialize(MqttContext* mqttContext) {
46 std::size_t consumed = 0;
47
48 switch (state) {
49 case 0:
50 consumed = stringLength.deserialize(mqttContext);
51 if (!stringLength.isComplete()) {
52 break;
53 }
54
55 setSize(stringLength);
56 state++;
57 [[fallthrough]];
58 case 1:
59 consumed += TypeBase::deserialize(mqttContext);
60 break;
61 }
62
63 // MUST close
64 // Check for UTF-16 (U+D800 - U+DFFF) surrogates
65 // Check for U+0000 in string
66
67 // MAY close
68 // Check for U+0001 - U+001F
69 // Check for U+007F - U+009F
70 // Check for non-characters
71
72 return consumed;
73 }
74
75 std::vector<char> String::serialize() const {
76 std::vector<char> returnVector = stringLength.serialize();
77 returnVector.insert(returnVector.end(), value.begin(), value.end());
78
79 return returnVector;
80 }
81
82 String& String::operator=(const std::string& newValue) {
83 value = std::vector<char>(newValue.begin(), newValue.end());
84 stringLength = static_cast<uint16_t>(value.size());
85
86 return *this;
87 }
88
89 String::operator std::string() const {
90 return std::string(value.begin(), value.end());
91 }
92
93 bool String::operator==(const std::string& rhsValue) const {
94 return static_cast<std::string>(*this) == rhsValue;
95 }
96
97 bool String::operator!=(const std::string& rhsValue) const {
98 return static_cast<std::string>(*this) != rhsValue;
99 }
100
101 void String::reset([[maybe_unused]] std::size_t size) {
102 stringLength.reset();
103 TypeBase::reset(0);
104
105 state = 0;
106 }
107
108 template class TypeBase<std::string>;
109
110} // namespace iot::mqtt::types
std::size_t deserialize(iot::mqtt::MqttContext *mqttContext) override
Definition String.cpp:45
void reset(std::size_t size=0) override
Definition String.cpp:101
std::vector< char > serialize() const override
Definition String.cpp:75