-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpystring.cpp
218 lines (215 loc) · 4.55 KB
/
pystring.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
#include<cstring>
#include<string>
#include<cctype>
#include<stdexcept>
#include<algorithm>
#include<iomanip>
using namespace std;
class pystring
{
private:
char* S = nullptr;
int n = 0;
int num = 0;
public:
pystring() = default;
friend ostream& operator<<(ostream& os, pystring& p) {
os << p.S;
return os;
}
friend ostream& operator<<(ostream& os, pystring&& p) {
os << p.S;
return os;
}
friend int len(pystring const& a) {
return a.n;
}
pystring capitalize();
pystring casefold();
char center();
int count(char a);
int count(const char* p);
//pystring encode();编码问题
bool endswith(const char* p, int start = 0, int end = -1);
pystring expandtabs(int tabsize=8);
int find(const char*p, int start = 0, int end = -1);
int index(const char*p, int start = 0, int end = -1);
bool isalnum();
pystring(const char* p);
~pystring();
};
pystring::pystring(const char* p)
{
n = strlen(p);
num = n + 1;
S = new char [num];
if (S != nullptr) {
memcpy(S, p, n + 1);
}
else {
throw range_error("字符串内存不够");
}
}
pystring pystring::capitalize() {
pystring a(this->S);
if (islower(a.S[0])) {
a.S[0] = toupper(a.S[0]);
}
return a;
}
pystring pystring::casefold() {
pystring a(this->S);
int i = 0;
for (int i = 0; i < a.n; i++)
{
if (islower(a.S[i])) {
a.S[i] = toupper(a.S[i]);
}
}
return a;
}
char pystring::center() {
return this->S[n / 2];
}
#ifndef cstr
int pystring::count(char a) {
int cou = 0;
for (int i = 0; i < this->n; i++)
{
if (this->S[i] == a)
{
cou++;
}
}
return cou;
}
int pystring::count(const char* p) {
int cou = 0;
int np = strlen(p);
for (int i = 0; i < this->n; i++)
{
if (strncmp(p, S + i, np) == 0) {
cou++;
i += np-1;
}
}
return cou;
}
pystring pystring::expandtabs(int tabsize){
int sizet=this->count('\t');
char*p=new char[sizet*tabsize+num];
for (int i = 0,j = 0; i < num; i++)
{
if(this->S[i]=='\t'){
for (int k = 0; k < tabsize; k++)
{
p[i+k]=' ';
}
j+=tabsize;
}
else{
p[j]=this->S[i];
j++;
}
}
return pystring(p);
}
int pystring::find(const char*p, int start, int end){
int np=strlen(p);
if(end<0)
end+=n;
if (end - start + 1 < np) {
return -1;
}
for (int i = start; i <= end-np+1; i++)
{
int j=0;
while (p[j]==this->S[i+j]&&j<np)
{
j++;
}
if(j==np){
return i;
}
}
return -1;
}
int pystring::index(const char*p, int start, int end){
int k=this->find(p,start,end);
if(n<0){
throw out_of_range("can't find the string");
}
else
{
return k;
}
}
bool pystring::isalnum(){
for (int i = 0; i < n; i++)
{
if(!std::isalnum(this->S[i]))
return false;
}
return true;
}
#elif defined cppago
int pystring::count(char a) {
int cou = std::count(this->S, this->S + this->n, a);
return cou;
}
int pystring::count(const char* p) {
int cou = 0;
size_t np=strlen(p);
string s(this->S);
size_t a=s.find(p);
while(a!=string::npos){
cou++;
a=s.find(p,a+np);
}
return cou;
}
pystring pystring::expandtabs(int tabsize){
string tabs(tabsize,' ');
string p(this->S);
size_t t=p.find("\t");
while(t!=string::npos){
p.replace(t,1,tabs);
t=p.find("\t",t+1);
}
return pystring(p.c_str());
}
#else
int pystring::count(char a) {
int cou=0;
char* p=strchr(this->S,a);
while (p) {
cou++;
p = strchr(p, a);
}
return cou;
}
int pystring::count(const char* p) {
int cou = 0;
char*q=strstr(this->S,p);
while(q){
cou++;
q=strstr(q,p);
}
return cou;
}
#endif
bool pystring::endswith(const char*p,int start,int end) {
int np = strlen(p);
int qi = 0;
if (end < 0) {
end += this->n;
}
if (end - start + 1 < np) {
return false;
}
return strncmp(p, this->S+end-np+1, np)==0;
}
pystring::~pystring()
{
}