Reproduces Arduino's String class
| 8 | |
| 9 | // Reproduces Arduino's String class |
| 10 | class String { |
| 11 | public: |
| 12 | String() = default; |
| 13 | String(const char* s) { |
| 14 | if (s) |
| 15 | str_.assign(s); |
| 16 | } |
| 17 | |
| 18 | void limitCapacityTo(size_t maxCapacity) { |
| 19 | maxCapacity_ = maxCapacity; |
| 20 | } |
| 21 | |
| 22 | unsigned char concat(const char* s) { |
| 23 | return concat(s, strlen(s)); |
| 24 | } |
| 25 | |
| 26 | size_t length() const { |
| 27 | return str_.size(); |
| 28 | } |
| 29 | |
| 30 | const char* c_str() const { |
| 31 | return str_.c_str(); |
| 32 | } |
| 33 | |
| 34 | bool operator==(const char* s) const { |
| 35 | return str_ == s; |
| 36 | } |
| 37 | |
| 38 | String& operator=(const char* s) { |
| 39 | if (s) |
| 40 | str_.assign(s); |
| 41 | else |
| 42 | str_.clear(); |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | char operator[](unsigned int index) const { |
| 47 | if (index >= str_.size()) |
| 48 | return 0; |
| 49 | return str_[index]; |
| 50 | } |
| 51 | |
| 52 | friend std::ostream& operator<<(std::ostream& lhs, const ::String& rhs) { |
| 53 | lhs << rhs.str_; |
| 54 | return lhs; |
| 55 | } |
| 56 | |
| 57 | protected: |
| 58 | // This function is protected in most Arduino cores |
| 59 | unsigned char concat(const char* s, size_t n) { |
| 60 | if (str_.size() + n > maxCapacity_) |
| 61 | return 0; |
| 62 | str_.append(s, n); |
| 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | std::string str_; |
no test coverage detected