REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
StringPrint.h
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#pragma once
6
7#include <string>
8#include <Arduino.h> // Print
9
10namespace utils {
11
12 // This is like ArduinoStreamUtils' StringPrint (https://github.com/bblanchon/ArduinoStreamUtils?tab=readme-ov-file#writing-to-a-string)
13 // but with STL strings and not Arduino Strings as target
14 class StringPrint : public Print {
15 public:
17
18 explicit StringPrint(std::string str) : _str(str) {}
19
20 size_t write(const uint8_t* p, size_t n) override {
21 for (size_t i = 0; i < n; i++) {
22 uint8_t c = p[i];
23 if (c == 0)
24 return i;
25 write(c);
26 }
27 return n;
28 }
29
30 size_t write(uint8_t c) override {
31 if (c == 0)
32 return 0;
33 _str += static_cast<char>(c);
34 return 1;
35 }
36
37 const std::string& str() const {
38 return _str;
39 }
40
41 void str(std::string str) {
42 _str = str;
43 }
44
45 void clear() {
46 _str.clear();
47 }
48
49 private:
50 std::string _str;
51 };
52
53}
const std::string & str() const
Definition StringPrint.h:37
void str(std::string str)
Definition StringPrint.h:41
StringPrint(std::string str)
Definition StringPrint.h:18
size_t write(uint8_t c) override
Definition StringPrint.h:30
size_t write(const uint8_t *p, size_t n) override
Definition StringPrint.h:20