Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a82c11dd8d0d7fc2625c9a5ad28cc41a537eac3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*******************************************************************************
 * Copyright (c) 2017 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 *
 *******************************************************************************/

#include "common/containers/String.h"

namespace etRuntime {

size_t String::nallocs = 0;
size_t String::ndeallocs = 0;
size_t String::ncreated = 0;
size_t String::ndestroyed = 0;

String::String()
: _size(0)
, _data(new char[1])
{
	++nallocs;
	++ncreated;
	_data[0] = '\0';
}

String::~String(void) {
	++ndestroyed;
	if (_data) {
		delete[] _data;
		++ndeallocs;
	}
}

/*
 * added this according to
 * http://stackoverflow.com/questions/1634359/is-there-a-reverse-function-for-strstr
 */
static const char *rstrstr(const char * s1, const char * s2) {
	size_t s1len = strlen(s1);
	size_t s2len = strlen(s2);
	const char *s;

	if (s2len > s1len) {
		return NULL;
	}
	for (s = s1 + s1len - s2len; s >= s1; --s) {
		if (strncmp(s, s2, s2len) == 0) {
			return s;
		}
	}
	return NULL;
}

String& String::operator=(const char *rhs) {
	if (rhs && rhs!=this->_data) {
		size_t new_size = std::strlen(rhs) + 1;
		if (new_size > _size) {
			_size = new_size;
			if (_data) {
				delete[] _data;
				++ndeallocs;
			}
			_data = new char[new_size];
			++nallocs;
		}
		std::strcpy(this->_data, rhs);
	}
	return *this;
}

const String String::operator+=(const char* rhs) {
	if (rhs) {
		size_t new_size = this->length() + std::strlen(rhs) + 1;
		if (new_size > _size) {
			_size = new_size;
			char* new_data = new char[new_size];
			++nallocs;
			if (_data) {
				std::strcpy(new_data, _data);
				delete[] _data;
				++ndeallocs;
			}
			else {
				new_data[0] = '\0';
			}
			_data = new_data;
		}
		std::strcat(this->_data, rhs);
	}

	// Note: this function returns a const, not a const&!
	// this prohibits things like (a + b) = c
	return *this;
}

const String String::operator+=(char c) {
	size_t new_size = this->length() + 2;
	if (new_size > _size) {
		_size = new_size;
		char* new_data = new char[new_size];
		++nallocs;
		if (_data) {
			std::strcpy(new_data, _data);
			delete[] _data;
			++ndeallocs;
		}
		_data = new_data;
	}
	_data[new_size-2] = c;
	_data[new_size-1] = '\0';

	// Note: this function returns a const, not a const&!
	// this prohibits things like (a + b) = c
	return *this;
}

size_t String::find(const String& str, size_t pos) const {
	if (pos>length()) {
		return npos;
	}
	return std::strstr(_data + pos, str._data) - _data;
}

size_t String::find(const char* str, size_t pos) const {
	if (pos>length()) {
		return npos;
	}
	return std::strstr(_data + pos, str) - _data;
}

size_t String::find(char c, size_t pos) const {
	if (pos>length()) {
		return npos;
	}
	return std::strchr(_data + pos, c) - _data;
}

size_t String::rfind(const String& str) const {
	return rstrstr(_data, str._data) - _data;
}

size_t String::rfind(const char* str) const {
	return rstrstr(_data, str) - _data;
}

size_t String::rfind(char c) const {
	return std::strrchr(_data, c) - _data;
}


String String::substr(size_t pos, size_t len) const {
	if (pos > length()) {
		return String("");
	}

	String result(_data+pos);
	if (len < result.length()) {
		result._data[len] = '\0';
	}

	return result;
}

} /* namespace etRuntime */

Back to the top