YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
yahal_String.cpp
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
15#include <cassert>
16#include <cstring>
17#include "yahal_String.h"
18
19namespace YAHAL {
20
21String::String(const char * ptr)
22 : _len(strlen(ptr)), _cap(_len), _ptr(new char[_cap+1]) {
23 strcpy(_ptr, ptr);
24}
25
26String::String(const String & s)
27 : _len(s._len), _cap(_len), _ptr(new char[_cap+1]) {
28 strcpy(_ptr, s._ptr);
29}
30
31String::String(char c, uint16_t n)
32 : _len(n), _cap(n), _ptr(new char[n+1]) {
33 uint16_t i=0;
34 for (; i < n; ++i) _ptr[i] = c;
35 _ptr[i] = 0;
36}
37
38String::~String()
39{
40 delete [] _ptr;
41}
42
43String & String::operator = (const char * ptr) {
44 uint16_t temp = strlen(ptr);
45 reserve_only(temp);
46 strcpy(_ptr, ptr);
47 _len = temp;
48 return *this;
49}
50
51String & String::operator = (const String & s) {
52 reserve_only(s._len);
53 strcpy(_ptr, s._ptr);
54 _len = s._len;
55 return *this;
56}
57
58String & String::operator = (char c) {
59 reserve_only(1);
60 _ptr[0] = c;
61 _ptr[1] = 0;
62 _len = 1;
63 return *this;
64}
65
66String::String(String && s) {
67 _ptr = s._ptr;
68 _len = s._len;
69 _cap = s._cap;
70 s._ptr = nullptr;
71}
72
73String & String::operator = (String && s) {
74 char * tmp = _ptr;
75 _ptr = s._ptr;
76 _len = s._len;
77 _cap = s._cap;
78 s._ptr = tmp;
79 return *this;
80}
81
82const char & String::operator [] (uint16_t pos) const {
83 assert(pos < _len);
84 return _ptr[pos];
85}
86
87char & String::operator [] (uint16_t pos) {
88 assert(pos < _len);
89 return _ptr[pos];
90}
91
92bool String::operator == (const String & r) const
93{
94 if (_len != r._len) return false;
95 for (uint16_t pos = 0; pos < _len; ++pos) {
96 if (_ptr[pos] != r._ptr[pos])
97 return false;
98 }
99 return true;
100}
101
102bool String::operator != (const String & r) const
103{
104 return !((*this) == r);
105}
106
107void String::reserve(uint16_t size) {
108 if(size > _cap) {
109 char *temp = new char[size+1];
110 strcpy(temp, _ptr);
111 delete [] _ptr;
112 _ptr = temp;
113 _cap = size;
114 }
115}
116
117void String::shrink_to_fit() {
118 if(_cap > _len) {
119 char * temp = new char[_len+1];
120 strcpy(temp, _ptr);
121 delete [] _ptr;
122 _ptr = temp;
123 _cap = _len;
124 }
125}
126
127void String::clear() {
128 _ptr[0] = 0;
129 _len = 0;
130}
131
132String & String::operator += (const String & s) {
133 uint16_t temp = s._len;
134 reserve(_len + temp);
135 strcat(_ptr, s._ptr);
136 _len += temp;
137 return *this;
138}
139
140String String::substr(uint16_t pos, uint16_t n) const
141{
142 String s("");
143 if (pos >= _len) return s;
144 for (; pos < _len; ++pos) {
145 if (!n) break; else --n;
146 s += _ptr[pos];
147 }
148 return s;
149}
150
151uint16_t String::find (char c, uint16_t pos) const
152{
153 for (; pos < _len; ++pos) {
154 if (c == _ptr[pos]) return pos;
155 }
156 return npos;
157}
158
159String String::fill_left (uint16_t width, const char c) {
160 if (_len < width) {
161 return String(c, width-_len) + *this;
162 } else {
163 return *this;
164 }
165}
166
167String String::fill_right(uint16_t width, const char c) {
168 if (_len < width) {
169 return *this + String(c, width-_len);
170 } else {
171 return *this;
172 }
173}
174
175
176// private methods
178
179void String::reserve_only(uint16_t size) {
180 if(size > _cap) {
181 char * temp = new char[size+1];
182 delete [] _ptr;
183 _ptr = temp;
184 _cap = size;
185 }
186}
187
188String to_String(int num, int base) {
189 char buf[20];
190 int i = sizeof(buf);
191 bool isNegative = false;
192
193 // Terminate with '\0'
194 buf[--i] = '\0';
195
196 // Check for 0
197 if (num == 0)
198 {
199 buf[--i] = '0';
200 return String(buf+i);
201 }
202 // Negative numbers are only handled with base 10
203 if (num < 0 && base == 10)
204 {
205 isNegative = true;
206 num = -num;
207 }
208 // Process digits
209 while (num != 0)
210 {
211 int rem = num % base;
212 buf[--i] = (rem > 9) ? (rem-10) + 'a' : rem + '0';
213 num = num / base;
214 }
215 // If number is negative, append '-'
216 if (isNegative)
217 buf[--i] = '-';
218
219 return String(buf+i);
220}
221
222String operator + (const String & lhs, const String & rhs) {
223 String res(lhs);
224 return res += rhs;
225}
226
227} // namespace YAHAL