annotate src/str.c @ 12:0cc2555db371

*: make our string functions not stupid strncat() made everything slow! new addition internally: an EDL_header structure to make things not stupid. if we were using C++ it wouldn't really be necessary, but alas :)
author Paper <paper@paper.us.eu.org>
date Wed, 27 Mar 2024 13:38:30 -0400
parents 0c98b46eaf73
children 2d7c810a1ac2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
1 #include "str.h"
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
2
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <stdlib.h>
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <string.h>
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
5
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
6 /* rudimentary string functions so the code in edl.c isn't
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
7 * as painful to read
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
8 */
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 int EDL_internal_string_init(EDL_internal_string* str) {
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 if (!str)
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 return 0;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 str->size = 0;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 str->capacity = 0;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16 str->data = NULL;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 return 1;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 }
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20 int EDL_internal_string_allocate(EDL_internal_string* str, size_t new_capacity) {
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 if (new_capacity == str->capacity)
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
22 return 1; /* nothing to do */
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 str->data = realloc(str->data, new_capacity * sizeof(char));
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 if (!str->data)
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 return 0;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
28 if (new_capacity > str->capacity)
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
29 memset(&str->data[str->capacity], 0, (new_capacity - str->capacity) * sizeof(char));
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 str->capacity = new_capacity;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33 return 1;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 }
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 int EDL_internal_string_append(EDL_internal_string* str, const char* data, const size_t length) {
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
37 if (str->capacity == 0)
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
38 if (!EDL_internal_string_allocate(str, 1))
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
39 return 0;
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
40
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
41 /* increase capacity if needed */
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
42 if (str->size + length + 1 >= str->capacity) {
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 size_t capacity = 1;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 while (capacity < (str->size + length + 1))
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45 capacity *= 2;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
47 if (!EDL_internal_string_allocate(str, capacity))
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 return 0;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49 }
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50
12
0cc2555db371 *: make our string functions not stupid
Paper <paper@paper.us.eu.org>
parents: 8
diff changeset
51 memcpy(&str->data[str->size], data, length);
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 str->size += length;
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
8
0c98b46eaf73 *: update API to 2.0, big changes
Paper <paper@paper.us.eu.org>
parents: 5
diff changeset
54 return length;
4
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 }
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57 void EDL_internal_string_free(EDL_internal_string* str) {
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 if (str->data)
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 free(str->data);
c2408abb258a *: add dumping to string, rename EDL_file to EDL
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 }