Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Generic / String / String.cpp
blob: 5bc17b4a22304fb4f748129c57a8c3a0a9fa1481 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "String.h"
#include <cstring>

/* Private */

void String::free() {
	delete[] str;
}

void String::copyFrom(const String& other) {
	str = new char[other.length + 1];
	strncpy(str, other.str, other.length + 1);
	length = other.length;
}

/* Public */

/*!
 * Allocated with length + 1 size, so there is a terminating zero at the end.
 */
String::String(unsigned length) {
	this->length = length;
	str = new char[length + 1];
	str[length] = '\0';
}

/*! \params str C-style string
 */
String::String(const char* str) {
	length = strlen(str);
	this->str = new char[length + 1]; // capture terminating zero
	strncpy(this->str, str, length + 1);
}

const char* String::get_cstr() const {
	return str;
}

unsigned String::get_length() const {
	return length;
}

char& String::operator[](unsigned index) {
	return str[index];
}

/*! \params str C-style string
 */
String& String::operator+=(const char* str) {
	unsigned strLen = strlen(str);
	char* newStr = new char[length + strLen + 1];
	strncpy(newStr, this->str, length + 1);
	strncat(newStr, str, strLen + 1);

	delete[] this->str;
	this->str = newStr;
	length += strLen;

	return *this;
}

/*!
 * Converts the number to a C-style string and then uses += to append it.
 */
String& String::operator+=(int number) {
	if (number < 0) {
		*this += "-";
		number *= -1;
	}
	return *this += (unsigned)number;
}

/*!
 * Converts the number to a C-style string and then uses += to append it.
 */
String& String::operator+=(unsigned number) {
	unsigned tempLen = 0;
	for (unsigned tmp = number; tmp > 0 || tempLen == 0; tmp /= 10)
		tempLen++;

	char* tmp = new char[tempLen + 1];
	tmp[tempLen] = '\0';
	for (unsigned i = tempLen - 1; i < tempLen; i--, number /= 10)
		tmp[i] = number % 10 + '0';

	*this += tmp;
	delete[] tmp;
	return *this;
}

/*!
 * Converts the number to a C-style string and then uses += to append it.
 */
String& String::operator+=(double number) {
	if (number < 0) {
		*this += "-";
		number *= -1;
	}
	number += 1.0 / (DOUBLE_PRECISION * 10); // bump up the number, in cases it becomes something like 8.59999999999...
	return ((*this += (unsigned)number) += ".")
				   += (unsigned)((number - (unsigned)number) * DOUBLE_PRECISION);
}

String::String() : String("") {}

String::String(const String& other) {
	copyFrom(other);
}

String& String::operator=(const String& other) {
	if (this != &other) {
		free();
		copyFrom(other);
	}
	return *this;
}

String::~String() {
	free();
}

String::String(String&& other) {
	str = other.str;
	other.str = nullptr;
	length = other.length;
}

String& String::operator=(String&& other) {
	if (this != &other) {
		delete[] str;
		str = other.str;
		other.str = nullptr;
		length = other.length;
	}
	return *this;
}

/*! \param istr An input stream
 *
 * Directly reads bytes from stream (calls read() function).
 * First reads the string length, then the underlying C-style string (including terminating zero).
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ifstream
 */
void String::read(std::istream& istr) {
	istr.read((char*)&length, sizeof(length));
	istr.read(str, sizeof(char) * (length + 1));
}

/*! \param ostr An output stream
 *
 * Directly writes bytes to stream (calls write() function).
 * First writes the string length, then the underlying C-style string (including terminating zero).
 * 
 * \remark Doesn't alter the stream in any other way.
 * \note Best used with binary ofstream
 */
void String::write(std::ostream& ostr) const {
	ostr.write((char*)&length, sizeof(length));
	ostr.write(str, sizeof(char) * (length + 1));
}

/*! \params other C-style string
 *  \returns strcmp between underlying C-style string and "other"
 */
int String::compare(const String& other) const {
	return strcmp(str, other.str);
}

/*!
 * Uses the stream's getline function to read the data.
 *
 * \warning It takes at most 1024 characters from the stream!
 * \note Best used with std::cin or text std::ifstream
 */
std::istream& operator>>(std::istream& istr, String& event) {
	istr.getline(event.str, 1024);
	event.length = strlen(event.str);

	return istr;
}

/*!
 * Uses the stream's << operator to write the underlying C-style string
 *
 * \note Best used with std::cout or text std::ofstream
 */
std::ostream& operator<<(std::ostream& ostr, const String& event) {
	return ostr << event.str;
}