-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstringf.cpp
More file actions
104 lines (90 loc) · 2.31 KB
/
Copy pathstringf.cpp
File metadata and controls
104 lines (90 loc) · 2.31 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
104
#include "stringf.h"
const char *WHITE_SPACES = " \t\n";
bool is_white_space(char c) {
return (c == ' ' || c == '\t' || c == '\n');
}
void del_extra_white_spaces(std::string &s) {
int left = s.find_first_not_of(WHITE_SPACES);
int right = s.find_last_not_of(WHITE_SPACES);
int len = right - left + 1;
if (len == 0) {
s = "";
return;
}
else {
s = s.substr(left, len);
std::string *s1 = new std::string;
s1->push_back(s[0]);
for (int i = 1; i < len; ++i) {
if (!(is_white_space(s[i]) && is_white_space(s[i - 1]))) {
(*s1) += s[i];
}
}
s = *s1;
delete s1;
}
}
void parse_by_white_spaces(std::string &s, list &arr) {
int current_position = 0;
int new_position = s.find_first_of(WHITE_SPACES, current_position);
while (new_position != std::string::npos) {
int cur_len = new_position - current_position;
arr.push_back(s.substr(current_position, cur_len));
current_position = new_position + 1;
new_position = s.find_first_of(WHITE_SPACES, current_position);
}
arr.push_back(s.substr(current_position));
}
void print_list(const list &li, std::ostream &out) {
int size = li.size();
out << "Number of words: " << size << "\n";
if (size == 0) {
return;
}
for (int i = 0; i < size; ++i) {
out << "$" << li[i] << "$\n";
}
out << "__end_of_dict__\n";
}
int find_word(const list &li, std::string &word, int position) {
int size = li.size();
if (size == 0) {
return -1;
}
else {
for (int i = position; i < size; ++i) {
if (li[i] == word) {
return i;
}
}
return -1;
}
}
void parse_list_by_word(list_arr_t &li_arr, list &li, std::string &word) {
int current_position = 0;
int new_position = find_word(li, word, current_position);
while (new_position != -1) {
li_arr.push_back(list(li.begin() + current_position, li.begin() + new_position));
current_position = new_position + 1;
new_position = find_word(li, word, current_position);
}
li_arr.push_back(list(li.begin() + current_position, li.end()));
}
void write_str(int fd, std::string str) {
int len = str.size();
char *buffer = new char[len];
for (int i = 0; i < len; ++i) {
buffer[i] = str[i];
}
write(fd, buffer, len);
delete []buffer;
}
std::string itos(int Number) {
std::string result;
while (Number) {
result += Number % 10;
Number /= 10;
}
std::reverse(result.begin(), result.end());
return result;
}