-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstr.h
executable file
·148 lines (117 loc) · 3.57 KB
/
str.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* Copyright (C) 2000, 2013 Thierry Crozat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact: [email protected]
*/
#ifndef str_h
#define str_h
#include <stdarg.h>
#include <string.h>
#ifndef NULL
#define NULL 0
#endif
class StringSharedData {
public:
StringSharedData() : size_(0), str_(NULL), capacity_(0), ref_cpt_(0) {}
StringSharedData(const StringSharedData& d) : size_(0), str_(NULL), capacity_(0), ref_cpt_(0) {
size_ = d.size_;
capacity_ = d.capacity_;
if (capacity_ > 0) {
str_ = new char[capacity_];
memcpy(str_, d.str_, size_+1);
}
}
~StringSharedData() {delete [] str_;}
int size_;
char* str_;
int capacity_;
int ref_cpt_;
};
class String {
public:
String();
String(const char *str);
String(const char *str, int len);
String(const String &str);
~String();
String &operator=(const char *str);
String &operator=(const String &str);
String &operator+=(const char *str);
String &operator+=(const String &str);
String &operator+=(char c);
bool operator==(const String &x) const;
bool operator==(const char *x) const;
bool operator!=(const String &x) const;
bool operator!=(const char *x) const;
bool contains(const String &x) const;
bool contains(const char *x) const;
bool startsWith(const String&) const;
bool endsWith(const String&) const;
bool isSpace(int) const;
const char* const c_str() const;
int length() const;
bool isEmpty() const;
char operator[](int idx) const;
String left(int to) const;
String right(int from) const;
String mid(int from, int to) const;
int toInt() const;
void deleteChar(int p);
void setChar(char c, int p);
void insertChar(char c, int p);
void replaceChar(char, char);
int findSpace(int from = 0) const;
int findChar(char, int from = 0) const;
int countChar(char, int from = 0) const;
void clear();
void trim();
String trimmed() const;
void simplify();
String simplified() const;
void simplify(char);
String simplified(char) const;
static String format(const char *fmt, ...);
static String vformat(const char *fmt, va_list args);
protected:
void ensureUnique();
void ensureCapacity(int new_size, bool keep_old);
void initWithCStr(const char *str, int len);
private:
StringSharedData* data_;
static const char* const nullStr;
};
String operator+(const String &x, const String &y);
String operator+(const char *x, const String &y);
String operator+(const String &x, const char *y);
String operator+(const String &x, char y);
String operator+(char x, const String &y);
bool operator==(const char *x, const String &y);
bool operator!=(const char *x, const String &y);
inline const char* const String::c_str() const {
return ((data_ == NULL || data_->str_ == NULL) ? nullStr : data_->str_);
}
inline int String::length() const {
return (data_ == NULL ? 0 : data_->size_);
}
inline bool String::isEmpty() const {
return (data_ == NULL || data_->size_ == 0);
}
inline char String::operator[](int idx) const {
if (idx < 0 || idx >= length())
return 0;
return data_->str_[idx];
}
#endif // str_h