YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
yahal_String.h
1// ---------------------------------------------
2// This file is part of
3// _ _ __ _ _ __ __
4// ( \/ ) /__\ ( )_( ) /__\ ( )
5// \ / /(__)\ ) _ ( /(__)\ )(__
6// (__)(__)(__)(_) (_)(__)(__)(____)
7//
8// Yet Another HW Abstraction Library
9// Copyright (C) Andreas Terstegge
10// BSD Licensed (see file LICENSE)
11//
12// ---------------------------------------------
13//
14// This is a class for handling character strings.
15// The class tries to implement most important
16// functionality of the standard std::string class.
17//
18
19#ifndef _STRING_H
20#define _STRING_H
21
22#include <cstdint>
23
24namespace YAHAL {
25
26class String
27{
28public:
29
30 static const uint16_t npos = -1;
31
32 // Constructors
33 String(const char * ptr = "");
34 String(const String &);
35 String(char, uint16_t=1);
36
37 // Destructor
38 virtual ~String();
39
40 // Assignment
41 String & operator = (const char *);
42 String & operator = (const String &);
43 String & operator = (char);
44
45 // Move support
46 String(String &&);
47 String & operator = (String &&);
48
49 // Element access
50 const char & operator [] (uint16_t) const;
51 char & operator [] (uint16_t);
52 const char * c_str() const { return _ptr; };
53
54 // Comparison operators
55 bool operator == (const String & r) const;
56 bool operator != (const String & r) const;
57
58 // Size of the String (not including any null-termination)
59 bool empty() const { return _len == 0; }
60 uint16_t size() const { return _len; }
61
62 // Capacity handling
63 void reserve(uint16_t);
64 uint16_t capacity() const { return _cap; }
65 void shrink_to_fit();
66
67 // Operations
68 void clear();
69 String & operator += (const String &);
70
71 // Returns a newly constructed string object with
72 // its value initialized to a copy of a substring
73 // of this object. The substring is the portion of
74 // the object that starts at character position pos
75 // and spans len characters (or until the end of the
76 // string, whichever comes first).
77 String substr(uint16_t pos=0, uint16_t n=npos) const;
78
79 // Searches the string for the first occurrence of
80 // character c. When pos is specified, the search
81 // only includes characters at or after position pos,
82 // ignoring any possible occurrences that include
83 // characters before pos.
84 uint16_t find (char c, uint16_t pos=0) const;
85
86 // field width adaptions
87 String fill_left (uint16_t width, const char c=' ');
88 String fill_right(uint16_t width, const char c=' ');
89
90 // Conversions
91 // NOTE: This operator makes trouble when String == "literal"
92 // is used, so comment it out .. Use c_str() instead !
93// operator const char * () { return _ptr; }
94
95protected:
96 uint16_t _len;
97 uint16_t _cap;
98 char * _ptr;
99
100 void reserve_only(uint16_t bytes);
101};
102
103// Conversions
104String to_String(int num, int base=10);
105
106// Global operators
107String operator + (const String & lhs, const String & rhs);
108
109} // namespace YAHAL
110
111using YAHAL::String;
112using YAHAL::to_String;
113
114#endif // _STRING_H
115