/* Arduino Print buffer. Size is fixed, unlike StreamString. Copyright (C) 2020 by Maxim Prokhorov */ #pragma once #include #include #include struct PrintString final : public Print, public String { PrintString(size_t reserved) : _reserved(reserved) { reserve(reserved); } size_t write(const uint8_t* data, size_t size) override { if (!size || !data) return 0; // we *will* receive C-strings as input size_t want = length() + size; if (data[size - 1] == '\0') { size -= 1; want -= 1; } if (want > _reserved) return 0; // XXX: 2.3.0 uses str... methods that expect '0' at the end of the 'data' // see WString{.cpp,.h} for the implementation #if defined(ARDUINO_ESP8266_RELEASE_2_3_0) std::copy(data, data + size, buffer + len); len = want; buffer[len] = '\0'; #else concat(reinterpret_cast(data), size); #endif return size; } size_t write(uint8_t ch) override { if (length() + 1 > _reserved) return 0; return concat(static_cast(ch)); } private: const size_t _reserved; };