-
Notifications
You must be signed in to change notification settings - Fork 1
/
String.h
126 lines (113 loc) · 4.12 KB
/
String.h
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
// =====================================================================
// Includes
// =====================================================================
#ifndef STRING_H_
#define STRING_H_
#include <cstdlib>
#include <cstdio>
class String{
protected:
// length of the String, unsigned integral type
size_t size_;
// \0 ended tab of char representing the String
char* data_;
// size of the storage space currently allocated for the String
size_t capacity_;
// Max length a String can reach
static const size_t MAX_SIZE;
// ================================================================
// Protected Methods
// ================================================================
size_t getCapacity(size_t size);
static size_t length(const char* s);
public:
// =================================================================
// Constructors
// =================================================================
String();
String(size_t n , char c) ;
String(char* str_in);
String(const String& str);
// =================================================================
// Destructor
// =================================================================
~String();
// =================================================================
// Public Methods
// =================================================================
const char* c_str() const noexcept;
void clear () noexcept;
bool empty() const noexcept;
void reserve (size_t n );
void reserve ();
void resize(size_t count);
void resize(size_t count, char c);
// =================================================================
// Getters
// =================================================================
size_t length() const noexcept;
size_t max_size() const noexcept;
size_t size() const noexcept;
size_t capacity() const noexcept;
// =================================================================
// Operators
// =================================================================
inline String& operator=(const char* other);
inline String& operator=(const String elem);
inline String& operator=(char c);
// operator + overloading with string given
friend String operator+(const String& s, const char c);
friend String operator+(const char c, const String& s);
friend String operator+(const String& lhs, const String& rhs);
friend String operator+(const String& lhs, const char* rhs);
friend String operator+(const char* lhs, const String& rhs);
char& operator[] (size_t pos);
const char& operator[] (size_t pos) const;
};
// =====================================================================
// Operators' inline definitions
// =====================================================================
/**
* \brief operator= overloading
* \details overloading with char* given
* \param char*
* \return *this
*/
inline String& String :: operator=(const char* other){
size_=length(other);
capacity_=getCapacity(size_);
data_=new char[capacity_+1];
for (unsigned int i=0; i<size_; i++)
data_[i]=other[i];
data_[size_]='\0';
return *this;
}
/**
* \brief operator= overloading
* \details overloading with char given
* \param char
* \return *this
*/
inline String& String :: operator=(char c){
size_=1;
capacity_=getCapacity(size_);
data_=new char[capacity_+1];
data_[0]=c;
data_[size_]='\0';
return *this;
}
/**
* \brief operator= overloading
* \details overloading with String given
* \param String
* \return *this
*/
inline String& String::operator=(const String elem){
size_=elem.size_;
capacity_=getCapacity(size_);
data_= new char[capacity_+1];
for(unsigned int i=0; i<=size_;i++)
data_[i]=elem.data_[i];
return *this;
}
#endif // STRING_H_