-
Notifications
You must be signed in to change notification settings - Fork 0
/
SanaeStr.h
351 lines (346 loc) · 9.28 KB
/
SanaeStr.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#pragma once
#ifndef INCLUDE_GUARD_SANAESTR_H
#define INCLUDE_GUARD_SANAESTR_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*ERROR用*/
#include <stdexcept>
#include <exception>
/*splitメンバ用*/
#include <vector>
#pragma warning(disable:4267)
#pragma warning(disable:4477)
/*-----------------------------------------------
* Project:SanaeProject-UsefulHeadersProject
* Dev:SanaeProject
* Function:
this headers have "String type and mathematics
functions and else many functions and types.".
*Copyright 2021 SanaeProject.ALL Rights Reserved.
-----------------------------------------------*/
namespace sanae {
/*-----------------------------------------------
* Project:SanaeProject-UsefulHeadersProject
* Dev:SanaeProject
* Function:
This header has String type.
*Copyright 2021 SanaeProject.ALL Rights Reserved.
-----------------------------------------------*/
class str {
private:
//freeした後ポインタをNULLにします
template<typename T>
void sfree(T* d) { free(d); d = NULL; }
//errorを返します
[[noreturn]] void mem_err() {
throw std::runtime_error("メモリ確保に失敗しました。");
}
//callocで確保します。 成功 true 失敗:false
bool _calloc(char** to, size_t count, bool dofree = true) {
if (count == 0) {count += 1;}
if (dofree)sfree(*to);
*to = (char*)calloc(count,sizeof(char*));
return *to == NULL?false:true;
}
char* st = NULL;
/*mode:
true=コピー先の初期化
false=初期化なし*/
void copystring(char** to,char** from,bool dofree=true) {
if (dofree)sfree(*to);
if (!_calloc(to, (strlen(*from) + 3),false))mem_err();
strcpy_s(*to, strlen(*from)+3,*from);
}
void copystring(char** to, const char** from, bool dofree = true) {
if (dofree)sfree(*to);
if (!_calloc(to, (strlen(*from) + 3),false))mem_err();
strcpy_s(*to, strlen(*from) + 3, *from);
}
void replace_c(int position,int len,char* to) {
char* str1 = NULL;
if (!_calloc(&str1, position))mem_err();
char* str2 = NULL;
char* str3 = NULL;
if (!_calloc(&str3, strlen(st) + 1 - (position + len)))mem_err();
/*str1=前方文字列 str2=挿入文字列 str3=後方文字列*/
char* text = NULL;
if (!_calloc(&text, position + strlen(to) + strlen(st) + 1 - (position + len) + 1))mem_err();
/*text=str1+str2+str3*/
//str1格納
for (int i = 0; i < position; i++) {
*(str1+i) = *(st + i);
}
*(str1+position) = 0;
//str2格納
copystring(&str2,&to,true);
//str3格納
for (unsigned int i = position + len, i2 = 0; i <= strlen(st);i++,i2++) {
*(str3 + i2) = *(st + i);
}
*(str3 + (strlen(str3) + 1)) = 0;
if (!_calloc(&st, strlen(str1) + strlen(str2) + strlen(str3) + 3))mem_err();
copystring(&st,&str1,false);
strcat_s(st, strlen(str1) + strlen(str2) + strlen(str3) + 3,str2);
strcat_s(st, strlen(str1) + strlen(str2) + strlen(str3) + 3, str3);
if (position>0) {sfree(str1);}//0の場合エラーが起きるため1以上の場合許可
sfree(str2);
sfree(str3);
}
public:
//指定されたサイズ確保する
void secure(size_t count) {
if (!_calloc(&st, count))mem_err();
}
/*コンストラクタ*/
str(const char text[]) {
copystring(&st, &text);
}
/*コンストラクタ*/
str() {
const char* text = "";
copystring(&st, &text);
}
/*コピーコンストラクタ*/
str(const str& strc) {
char* test = strc.st;
copystring(&st, &test);
}
/*デストラクタ*/
~str() {
free(st);
st = NULL;
}
size_t len() {
return strlen(st);
}
str& operator =(const char text[]) {
copystring(&st, &text);
return *this;
}
str& operator =(const str& strc) {
char* test = strc.st;
copystring(&st, &test);
return *this;
}
/*その他処理*/
char& operator [](int t) {
if (strlen(st)<=(unsigned int)t) {
throw std::out_of_range("範囲外の値にアクセスしようとしました。");
}
return *(st+t);
}
str& operator +=(const char* t){
this->add(t);
return *this;
}
str& operator +=(int i){
this->addint(i);
return *this;
}
str& operator <<(const char* t) {
this->add(t);
return *this;
}
str& operator <<(char t) {
this->addchr(t);
return *this;
}
str& operator <<(int t) {
this->addint(t);
return *this;
}
/*if*/
bool operator ==(const char text[]){
if (strcmp(st, text) == 0) { return true; }
return false;
}
bool operator ==(str& t){
if (strcmp(st, t.c_str()) == 0) { return true; }
return false;
}
bool operator !=(const char text[]){
if (strcmp(st, text) != 0) { return true; }
return false;
}
bool operator !=(str& t){
if (strcmp(st, t.c_str()) != 0) { return true; }
return false;
}
/*変換関数*/
operator const char* () {
return st;
}
operator char* (){
return st;
}
operator int(){return atoi(st);}
/*char*型で返します。*/
const char* c_str(){
return st;
}
/*文字を追加*/
str& addchr(char d){
char buf[4];
snprintf(buf, 4, "%c", d);
add(buf);
return *this;
}
//指定文字の個数を数えます。
int count(char t) {
int count = 0;
for (unsigned int i = 0; i < strlen(this->st);i++) {
if ((this->st[i]) == t)count++;
}
return count;
}
//追記します。
str& add(const char* text){
char* copyst = NULL;
if (!_calloc(©st, strlen(st) + 1, false))mem_err();
copystring(©st,&st,false);
sfree(st);
if (!_calloc(&st, strlen(copyst) + strlen(text) + 1, false))mem_err();
copystring(&st,©st,false);
strcat_s(st, strlen(copyst) + strlen(text) + 2,text);
sfree(copyst);
return *this;
}
//前方に追記します。
str& add_front(const char* text) {
char* copyst = NULL;
if (!_calloc(©st, strlen(st) + 1, false))mem_err();
copystring(©st, &st);
sfree(st);
if (!_calloc(&st, strlen(copyst) + strlen(text) + 1, false))mem_err();
copystring(&st,&text,false);
strcat_s(st, strlen(copyst) + strlen(text) + 2, copyst);
sfree(copyst);
return *this;
}
//int型を追加します。
str& addint(int i){
char buf[12];
snprintf(buf, 12, "%d", i);
add((const char*)buf);
return *this;
}
/*検索文字列が存在しない場合は - 1を返します。
存在する場合は配列番号を返します。
*/
int find(const char* to){
if (strstr(st, to)==0)return -1;
if (st == NULL) return -1;
#pragma warning(disable:6387)
return strlen(st)-strlen(strstr(st,to));
}
/*検索文字が存在しない場合は - 1を返します。
存在する場合は配列番号を返します。
*/
int find(const char to) {
for (unsigned int i = 0; i < strlen(st);i++) {
if (st[i]==to) {
return i;
}
}
return -1;
}
/*検索文字が存在しない場合は - 1を返します。
存在する場合は配列番号を返します。(後ろから探します)*/
int rfind(const char to) {
for(int i = strlen(this->st) - 1; i >= 0;i--) {
if (this->st[i]==to) {
return i;
}
}
return -1;
}
char* for_substr = NULL;
/*切り抜いて返します。 substr(切り抜く配列番号,そこから切り抜く個数)
第二引数で0が渡された場合切り抜く配列番号~最後まで切り抜きます。
*/
const char* substr_c(unsigned int position,int count=0) {
count == 0 ? count = strlen(this->st) - position:count;
if (!_calloc(&for_substr, count + position + 1), false)mem_err();
for (unsigned int i = position,now=0; i < (count + position);i++,now++) {
for_substr[now] = this->st[i];
}
return for_substr;
}
/*切り抜いて返します。
第一引数~第二引数まで切り抜きます.
*/
const char* substr(char start,char finish) {
int s = this->find(start);
int f = this->rfind(finish);
int count = f - s;
return this->substr_c(s,count+1);
}
/*文字と文字を入れ替えます。*/
int replace(const char* from,const char* to){
const int position = this->find(from);
if (position == -1) { return -1; }
const int len = strlen(from);
replace_c(position,len,(char*)to);
return 0;
}
/*値を消去します。*/
virtual void clear() {
sfree(st);
sfree(for_substr);
}
/*入力取得
mode:0 代入
mode:1 追記
*/
void input(size_t size = 1024, unsigned int mode = 0) {
char* t = NULL;
if (!_calloc(&t, size, false))mem_err();
scanf_s("%s", t, size);
if (mode == 0) {
copystring(&st, &t, true);
}
else if (mode == 1) {
for (unsigned int i = 0; i < strlen(t); i++) {
this->addchr(*(t + i));
}
}
sfree(t);
}
//指定した文字で分割しstd::vectorで返します。
std::vector<str> split(const char split_text) {
std::vector<str> data;
int hear = this->find(split_text);
if (hear==-1) {
data.push_back(st);
return data;
}
while (1) {
int found = this->find(split_text);
char* deldata = NULL;
if (!_calloc(&deldata, found, false))mem_err();
for (int i = 0; i < found+1;i++) {
deldata[i] = st[i];
}
str adddata="";
adddata.add((const char*)deldata);
this->replace(adddata.c_str(),"");
data.push_back(adddata);
found = this->find(split_text);
if (found == -1) {
str g = "";
g.add(st);
g.replace(deldata,"");
data.push_back(g);
for (unsigned int i = 0; i < data.size();i++) {
if (data[i].find(split_text)!=-1) {
data[i][strlen(data[i].c_str())-1] = 0;
}
}
return data;
}
}
}
};
}
#endif