-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
105 lines (86 loc) · 2.3 KB
/
Copy pathstring.cpp
File metadata and controls
105 lines (86 loc) · 2.3 KB
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
//
// string.cpp
// Perfection
//
// Created by Mathias Brekkan on 06/04/2018.
// Copyright © 2018 Mathias Brekkan. All rights reserved.
//
#include "string.hpp"
namespace perf {
inline int stlen(const char* value) {
int length = 0;
while (*value != '\0') {
++length;
++value;
}
return length;
}
string::string()
: value(nullptr)
{
}
string::string(const char* initValue) {
length = stlen(initValue);
value = new char[length];
for(int i = 0; i < length; i++) {
value[i] = initValue[i];
}
}
string::string(int size) {
length = size;
value = new char[size];
}
string::string(const string& other) {
length = other.length;
value = new char[length];
for(int i = 0; i < length; i++) {
value[i] = other.value[i];
}
}
void string::operator=(const string& other) {
length = other.length;
if(value != nullptr)
delete[] value;
value = new char[length];
for(int i = 0; i < length; i++) {
value[i] = other.value[i];
}
}
bool string::operator==(const string& other) const {
if(value == other.value)
return true;
return false;
}
string::~string() {
delete[] value;
}
char string::operator[](int i) const {
return value[i];
}
string string::operator+(const string& other) const {
string sum(length + other.length);
for(int i = 0; i < length; i++) {
sum.value[i] = value[i];
}
for(int i = length; i < other.length + length; i++) {
sum.value[i] = other.value[i - length];
}
return sum;
}
string string::operator+=(const string& other) {
*this = *this + other;
return *this;
}
std::ostream& operator<<(std::ostream& o, const string& msg) {
for(int i = 0; i < msg.length; i++) {
o << msg.value[i];
}
return o;
}
std::istream& operator>>(std::istream& i, string& msg) {
char* value = nullptr;
i >> value;
msg = string(value);
return i;
}
}