-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.c
More file actions
56 lines (44 loc) · 1.43 KB
/
sequence.c
File metadata and controls
56 lines (44 loc) · 1.43 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
//
// Created by mayachen on 2025-09-19.
//
#include "sequence.h"
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "types.h"
// Returns a pointer to the `next` element.
sequence_t *attach(sequence_t *curr_seq_ptr, const str new_elem) {
sequence_t *new_seq_ptr = malloc(sizeof(sequence_t));
new_seq_ptr->elem = new_elem;
curr_seq_ptr->next = new_seq_ptr;
return new_seq_ptr;
}
// The seq should be NULL at the end
str assemble_str(sequence_t *seq, const wchar_t rest_of_the_chars[CHUNK_SIZE / sizeof(wchar_t)]) {
void *backup = seq; // Save starting addresses
wchar_t *ret_str;
// I'm sure the compiler will be glad to optimize this "scope" :clueless:
{
size_t len = wcslen(rest_of_the_chars);
// Reused pointer backup variable
while (seq->elem != NULL) {
len += wcslen(seq->elem);
seq = seq->next; // truly the peakest type of loops
}
ret_str = malloc((len + 1) * sizeof(wchar_t));
}
seq = (sequence_t *) backup;
backup = ret_str;
while (seq->elem != NULL) {
wcscpy(ret_str, seq->elem);
ret_str += wcslen(seq->elem);
sequence_t *old_seq = seq;
seq = seq->next;
free(old_seq); // it has served its purpose
// it can now enjoy crystal stasis
}
free(seq);
wcscpy(ret_str, rest_of_the_chars); // it fits well its name
ret_str = backup;
return ret_str;
}