Skip to content

Commit 9a11e72

Browse files
committed
minor cleanups
1 parent 2f5fcff commit 9a11e72

File tree

1 file changed

+4
-111
lines changed

1 file changed

+4
-111
lines changed

main.c

+4-111
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,8 @@ char *append_string_by_copy(darray *array, char *string);
4747

4848
char *get_string_at(const darray *array, int index);
4949

50-
void remove_string_at(darray *array, int index);
51-
5250
char *save_and_replace(darray *save_array, darray *write_array, char *new_line, int index);
5351

54-
void save_and_remove(darray *lines_deleted, darray *text, int index);
55-
56-
void free_darray(darray *array);
57-
5852
bool contains_index(darray *array, int index);
5953

6054
bool valid_address(int addr1);
@@ -76,8 +70,6 @@ void delete(int addr1, int addr2);
7670

7771
void delete_without_undo(int addr1, int addr2);
7872

79-
void remove_lines(darray *text, int index_to_delete, int number_of_lines);
80-
8173
void undo(int number);
8274

8375
void undo_change(stack_node *undo_node);
@@ -136,33 +128,10 @@ char *append_string_by_copy(darray *array, char *string) {
136128

137129
}
138130

139-
void insert_string_by_reference(darray *array, int index, char *string) {
140-
141-
if (array->n == array->capacity)
142-
enlarge_darray(array);
143-
144-
array->n++;
145-
146-
for (int i = array->n - 1; i > index; i--)
147-
array->strings[i] = array->strings[i - 1];
148-
149-
array->strings[index] = string;
150-
}
151-
152-
153131
char *get_string_at(const darray *array, int index) {
154132
return array->strings[index];
155133
}
156134

157-
void remove_string_at(darray *array, int index) {
158-
159-
//shift all strings by one and free the deleted deleted_string
160-
for (int i = index + 1; i < array->n; i++)
161-
array->strings[i - 1] = array->strings[i];
162-
163-
array->n--;
164-
}
165-
166135
void replace_string_by_reference(darray *array, int index, char *string) {
167136
array->strings[index] = string;
168137
}
@@ -193,38 +162,6 @@ char *save_and_replace(darray *save_array, darray *write_array, char *new_line,
193162

194163
}
195164

196-
void save_and_remove(darray *lines_deleted, darray *text, int index) {
197-
//equivalent to:
198-
//append_string_by_copy(lines_deleted, get_string_at(text_array, line_to_delete)); //save deleted string to undo stack
199-
//remove_string_at(text_array, line_to_delete);
200-
201-
if (lines_deleted->n == lines_deleted->capacity)
202-
enlarge_darray(lines_deleted);
203-
204-
205-
//append to lines deleted
206-
lines_deleted->strings[lines_deleted->n] = text->strings[index];
207-
lines_deleted->n++;
208-
209-
//shift all strings by one
210-
for (int i = index + 1; i < text->n; i++)
211-
text->strings[i - 1] = text->strings[i];
212-
213-
text->n--;
214-
215-
}
216-
217-
void free_darray(darray *array) {
218-
int n = array->n;
219-
220-
for (int i = 0; i < n; i++) {
221-
if (array->strings[i] != NULL)
222-
free(array->strings[i]);
223-
}
224-
free(array->strings);
225-
free(array);
226-
}
227-
228165
bool contains_index(darray *array, int index) {
229166
return index < array->n && index >= 0;
230167
}
@@ -305,7 +242,6 @@ void execute_pending_undo() {
305242
}
306243

307244
void clear_redo() {
308-
309245
redo_stack->top = NULL; //intentional memory leak
310246
redo_stack->size = 0;
311247
}
@@ -508,26 +444,25 @@ void print(int addr1, int addr2) {
508444

509445
//append \n before each line, except if it's first print
510446
if (current_index < 0) {
511-
if (!first_print) fputc('\n', stdout);
512-
fputc('.', stdout);
447+
if (!first_print) fputc_unlocked('\n', stdout);
448+
fputc_unlocked('.', stdout);
513449
return;
514450
}
515451

516452
while (current_index <= addr2 - 1) {
517453

518-
if (!first_print) fputc('\n', stdout);
454+
if (!first_print) fputc_unlocked('\n', stdout);
519455

520456
if (contains_index(text_array, current_index)) {
521457
fputs(get_string_at(text_array, current_index), stdout);
522458
} else
523-
fputc('.', stdout);
459+
fputc_unlocked('.', stdout);
524460

525461
current_index++;
526462
first_print = false;
527463
}
528464
}
529465

530-
//todo
531466
void delete(int addr1, int addr2) {
532467

533468
execute_pending_undo();
@@ -550,11 +485,8 @@ void delete(int addr1, int addr2) {
550485
return;
551486
}
552487

553-
first_print = false;
554-
555488
lines_deleted = new_darray(num_to_delete);
556489

557-
558490
for (int i = first_index; i <= last_index; i++)
559491
append_string_by_reference(lines_deleted, text_array->strings[i]);
560492

@@ -623,45 +555,6 @@ void append_node_lines_to_text(darray *text, stack_node *node, int lines_to_add)
623555

624556
}
625557

626-
void remove_lines(darray *text, int index, int num_lines_to_remove) {
627-
628-
for (int i = 0; i < num_lines_to_remove; i++) {
629-
//remove_string_at(text, index);
630-
631-
//shift all strings by one
632-
for (int i = index + 1; i < text->n; i++)
633-
text->strings[i - 1] = text->strings[i];
634-
635-
text->n--;
636-
}
637-
}
638-
639-
void insert_node_lines_in_text(stack_node *node, int addr1) {
640-
641-
int lines_to_add = node->lines->n;
642-
643-
for (int j = 0; j < lines_to_add; j++) {
644-
//insert_string_by_reference(text_array, addr1 + j - 1, node->lines->strings[j]);
645-
646-
int text_index = addr1 + j - 1;
647-
648-
if (text_array->n == text_array->capacity)
649-
enlarge_darray(text_array);
650-
651-
text_array->n++;
652-
653-
//shift all elements
654-
for (int i = text_array->n - 1; i > text_index; i--)
655-
text_array->strings[i] = text_array->strings[i - 1];
656-
657-
text_array->strings[text_index] = node->lines->strings[j];
658-
659-
}
660-
661-
662-
}
663-
664-
665558
void insert(stack_node *node, int addr1) {
666559

667560
int num_added = node->lines->n;

0 commit comments

Comments
 (0)