-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalphabet.c
More file actions
226 lines (202 loc) · 7.17 KB
/
alphabet.c
File metadata and controls
226 lines (202 loc) · 7.17 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
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
// ALL WHAT HAS TO DO WITH ALPHABET
struct index {
unsigned int size;
unsigned int index;
};
unsigned int strgs_counter = 0;
void set_strgs_counter(unsigned int n){
strgs_counter = n;
}
unsigned int get_strgs_counter(){
return strgs_counter;
}
void str_unset(char* str, unsigned int size){
for (unsigned int i = 0; i < size; i++){
str[i] = '\0';
}
}
// rewrites in the same buffer the allowed charachters and adds 0 at the end
unsigned short str_format(char* word){
u8 count = 0;
u8 check = 0;
unsigned int i = 0;
char c = word[i];
while(c != '\0'){
if( (u8) c > ' ') check++;
if( (u8) c < 127) check++;
if(check == 2){
word[count] = word[i];
//printf("%c", word[count]);
count++;
}
check = 0;
i++;
c = word[i];
}
word[count] = '\0';
return count;
}
// dreaming a world where no libraries already exist, no univerities, no predefied quantities, just a list, of where to get what... where the poor is really the poor, and the other, is in one of those places.
unsigned int str_cat( char* result, char* s1 , char* s2){
unsigned int zise = 0;
unsigned int i = 0;
while (s1[i] != '\0'){
result[i] = s1[i];
i++;
}
zise = i;
i = 0;
while (s2[i] != '\0'){
result[zise] = s2[i];
i++;
zise++;
}
result[zise] = '\0';
return zise;
}
// // // // // // // // // // // // // // // // // // // // LENGT OF STRING // // // // // // // //
unsigned int str_len(char* str){
unsigned int size = 0;
unsigned int i = 0;
while(str[i] != '\0'){
size++;
i++;
}
return size;
}
// // // // // // // // // // // // // // // // // // // // COMPARE 2 STRINGS // // // // // // // //
unsigned char str_cmp(unsigned int size, char* _1 , char* _2){
unsigned char result = 1;
for(unsigned int i = 0; i < size; i++){
if (_1[i] != _2[i]) return 0;
}
return result;
}
// // // // // // // // // // // // // // // // // // // // PREPROCESS STRING , LOAD STRUCTS WITH INDEX AND SIZE OF SUBSTRINGS // // // // // // // //
struct index* strpreprocess(char* splitter , char* str){
// splitter , str : size
unsigned int spl_sz = str_len(splitter);
unsigned int str_sz = str_len(str);
// get first char
register char frc = splitter[0];
struct index iiddxx;
iiddxx.size = 0;
iiddxx.index = 0;
// worst case scenario
struct index *idx_s = malloc(sizeof iiddxx * (str_sz / 2) );
// set o as first index
unsigned int indextosave = 0;
// iterators
unsigned int i = 0;
unsigned int structs_counter = 0;
while ( str[i] != '\0' ){
// chaeck if first char of splitter is found
if(str[i] == frc){
if( str_cmp(spl_sz , splitter , &str[i]) ) {
iiddxx.index = indextosave;
iiddxx.size = i - indextosave; // size of that part is indextosave = 0; i = 99; splitter found = i
// set iterators
i+= spl_sz; // set i after splitter ab[c]<,>[i]bcdef
indextosave = i; // set indx after splitter ab c <,>[a]bcdef
// load struct in array of structs
idx_s[structs_counter++] = iiddxx;
}
}
i++;
}
// if splitter never found set as only one string found
if(iiddxx.size == 0 ){
iiddxx.size = str_len(str);
idx_s = realloc( idx_s , sizeof(iiddxx) );
idx_s[0] = iiddxx;
set_strgs_counter(1);
return idx_s;
}
// collecting the last from last struct
iiddxx.index = iiddxx.index + iiddxx.size + spl_sz;
iiddxx.size = i - iiddxx.index ;
idx_s[structs_counter++] = iiddxx;
// memory
idx_s = realloc(idx_s , sizeof(iiddxx)* structs_counter);
set_strgs_counter(structs_counter);
return idx_s;
}
// # # # # # # # # # # # # # # # # # # # # # # # # # # # K E E P T R A C K O F S T R I N G S I N T H E H E A P # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
void * strings_in_heap[1024];
unsigned int strings_in_heap_count = 0;
void track_string( void *p ){
strings_in_heap[strings_in_heap_count] = p;
strings_in_heap_count++;
}
void str_free_all(){
for (size_t i = 0; i < strings_in_heap_count; i++){
free(strings_in_heap[i]);
}
strings_in_heap_count = 0;
}
// # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
// // // // // // // // // // // // // // // // // // // // GET ARRAY OF POINTERS FOR STRINGS // // // // // // // //
char ** str_split(char* splitter , char* str){
// getting struct array of data abou splitting
struct index* idx_s;
idx_s = strpreprocess(splitter , str);
// knowing size of array of pointers
unsigned int count = get_strgs_counter();
char ** splitted_strings = malloc( sizeof (char*) * count );
// get substring from to following index structure
char* new_string_from(char* str , unsigned int index , unsigned int size){
char* newstr = malloc( (sizeof (char) * size) + 1 );
newstr[0] = '\0'; // if size is 0
track_string(newstr); // keep count of strings on the heap
for (unsigned int i = 0; i < size; i++){
newstr[i] = str[index + i];
}
newstr[size] = '\0';
return newstr;
};
// making a new string substringing str and saving the pointer in splitted_strings
char *newstr;
for (unsigned int i = 0; i < count; i++){
newstr = new_string_from(str , idx_s[i].index, idx_s[i].size ) ;
splitted_strings[i] = newstr;
}
// is already count, but just in this unix time
set_strgs_counter(count);
free(idx_s);
track_string(splitted_strings);
return splitted_strings;
}
// // // // // // // // // // // // // // // // // // // // GPRINTING MANY STRINGS // // // // // // // //
void print_strings(char ** strings){
printf("\n");
unsigned int count = get_strgs_counter();
for (unsigned int i = 0; i < count; i++){
printf("%s\n" , strings[i] );
}
set_strgs_counter(0);
}
void str_cpy(char* result, char* str){
int count = str_len(str);
for (unsigned int i = 0; i < count; i++){
result[i] = str[i];
}
result[count] = '\0';
}
void sub_str(char*bf_substr, char* bf_input , unsigned int first, unsigned int length ){
unsigned int itr = 0;
for (unsigned int i = first; i < first+length; i++){
bf_substr[itr] = bf_input[i];
itr++;
}
bf_substr[itr] = '\0';
}
int indexOf(char * input , char c){
int i = 0;
while(input[i] != '\0'){
if(input[i] == c){
return i;
}
i++;
}
return 0;
}