Browse Source

Modfiying corrected cstring interface, much smoother, and updated Makefile to test

Luiserebii 10 months ago
parent
commit
766759ee9e
Signed by: Luiserebii <luis@serebii.io> GPG Key ID: 58A745E0A5422230
4 changed files with 76 additions and 75 deletions
  1. 47
    47
      include/cstl/cstring.h
  2. 24
    23
      src/cstring.c
  3. 1
    1
      src/string.c
  4. 4
    4
      test/Makefile

+ 47
- 47
include/cstl/cstring.h View File

@@ -1,33 +1,7 @@
1 1
 #ifndef CSTL_CSTRING_H
2 2
 #define CSTL_CSTRING_H
3 3
 
4
-#include <string.h>
5
-
6
-/**
7
- * Guarantees copying lim-1 chars to the destination string, reserving at least
8
- * one for a null character ('\0'). Strings copied are thus guaranteed to be
9
- * null-terminated, and provides safety by truncating silently.
10
- */
11
-void safestrcpy(char* dest, const char* src, size_t lim);
12
-
13
-/**
14
- * Guarantees concatenating as many characters as possible from src to dest.
15
- * This is intended to act as a convenience macro, to mimic safestrcpy's usage.
16
- */
17
-#define safestrcat(dest, src, dest_lim) strncat(dest, src, dest_lim - strlen(dest) - 1)
18
-
19
-/**
20
- * fstrcat, as in, "fast" strcat.
21
- * Returns a pointer to the last element, to try to alleviate future O(n) calls.
22
- */
23
-char* fstrcat(char* dest, const char* src);
24
-
25
-/**
26
- * sfstrcat, as in, "safe-fast" strcat.
27
- * Guarantees concatenating as many characters as possible from src to dest.
28
- * Returns a pointer to the last element, to try to alleviate future O(n) calls.
29
- */
30
-char* sfstrcat(char* dest, const char* src, size_t lim);
4
+#include <stdlib.h>
31 5
 
32 6
 /**
33 7
  * String OO-like functions: cstring
@@ -42,28 +16,17 @@ char* sfstrcat(char* dest, const char* src, size_t lim);
42 16
  * NULL, and reserve deinit/destroy for the actual calling of cstring_destroy.
43 17
  */
44 18
 
45
-/**
46
- * Options for supplanting a user-defined malloc/realloc equivalent for vectors.
47
- */
48
-#ifndef CSTL_MALLOC
49
-#define CSTL_MALLOC malloc
50
-#include <stdlib.h>
51
-#endif
52
-
53
-#ifndef CSTL_REALLOC
54
-#define CSTL_REALLOC realloc
55
-#include <stdlib.h>
56
-#endif
57
-
58
-#ifndef CSTL_FREE
59
-#define CSTL_FREE free
60
-#include <stdlib.h>
61
-#endif
62
-
63 19
 /**
64 20
  * Initialize a c-string to a valid state, or, NULL.
21
+ *
22
+ * NOTE: The true interface for this function is:
23
+ *
24
+ * void cstring_init(char* s)
25
+ *
26
+ * Macros have simply been use to unify the interface.
65 27
  */
66
-void cstring_init(char** s);
28
+#define cstring_init(s) cstring_init_(&s)
29
+void cstring_init_(char** s);
67 30
 
68 31
 /**
69 32
  * Create a new c-string, and fill it with the contents of c-string s.
@@ -77,12 +40,49 @@ char* cstring_create(const char* s);
77 40
  * This will cause a deallocation, if s1 already has been filled.
78 41
  * As NULL is a valid state for a char*, this is checked for; if s2 is NULL,
79 42
  * then s1 will simply be freed and will stay as NULL.
43
+ * 
44
+ * NOTE: The true interface for this function is:
45
+ *
46
+ * void cstring_asn(char* s1, const char* s2)
80 47
  */
81
-void cstring_asn(char** s1, const char* s2);
48
+#define cstring_asn(s1, s2) cstring_asn_(&s1, s2)
49
+void cstring_asn_(char** s1, const char* s2);
82 50
 
83 51
 /**
84 52
  * Destroys and releases the memory for a c-string.
85 53
  */
86 54
 void cstring_destroy(char* s);
87 55
 
56
+
57
+/**
58
+ * Utility functions for handling c-strings
59
+ */
60
+
61
+/**
62
+ * Guarantees copying lim-1 chars to the destination string, reserving at least
63
+ * one for a null character ('\0'). Strings copied are thus guaranteed to be
64
+ * null-terminated, and provides safety by truncating silently.
65
+ */
66
+void safestrcpy(char* dest, const char* src, size_t lim);
67
+
68
+/**
69
+ * Guarantees concatenating as many characters as possible from src to dest.
70
+ * This is intended to act as a convenience macro, to mimic safestrcpy's usage.
71
+ */
72
+#define safestrcat(dest, src, dest_lim) strncat(dest, src, dest_lim - strlen(dest) - 1)
73
+
74
+/**
75
+ * fstrcat, as in, "fast" strcat.
76
+ * Returns a pointer to the last element, to try to alleviate future O(n) calls.
77
+ */
78
+char* fstrcat(char* dest, const char* src);
79
+
80
+/**
81
+ * sfstrcat, as in, "safe-fast" strcat.
82
+ * Guarantees concatenating as many characters as possible from src to dest.
83
+ * Returns a pointer to the last element, to try to alleviate future O(n) calls.
84
+ */
85
+char* sfstrcat(char* dest, const char* src, size_t lim);
86
+
87
+
88 88
 #endif

+ 24
- 23
src/cstring.c View File

@@ -1,6 +1,30 @@
1 1
 #include "../include/cstl/cstring.h"
2
+#include "../include/cstl/memory.h"
2 3
 #include <string.h>
3 4
 
5
+void cstring_init_(char** s) {
6
+    *s = NULL;
7
+}
8
+
9
+char* cstring_create(const char* s) {
10
+    char* st = cstl_malloc(strlen(s) + 1);
11
+    strcpy(st, s);
12
+    return st;
13
+}
14
+
15
+void cstring_asn_(char** s1, const char* s2) {
16
+    cstl_free(*s1);
17
+    if(s2) {
18
+        *s1 = cstring_create(s2);
19
+    } else {
20
+        *s1 = NULL;
21
+    }
22
+}
23
+
24
+void cstring_destroy(char* s) {
25
+    cstl_free(s);
26
+}
27
+
4 28
 void safestrcpy(char* dest, const char* src, size_t lim) {
5 29
     //Minor dev-note; *dest++ = *src++ can resolve to '\0'
6 30
     //being set, rendering the final statement potentially pointless
@@ -26,26 +50,3 @@ char* sfstrcat(char* dest, const char* src, size_t lim) {
26 50
         ;
27 51
     return dest;
28 52
 }
29
-
30
-void cstring_init(char** s) {
31
-    *s = NULL;
32
-}
33
-
34
-char* cstring_create(const char* s) {
35
-    char* st = CSTL_MALLOC(strlen(s) + 1);
36
-    strcpy(st, s);
37
-    return st;
38
-}
39
-
40
-void cstring_asn(char** s1, const char* s2) {
41
-    CSTL_FREE(*s1);
42
-    if(s2) {
43
-        *s1 = cstring_create(s2);
44
-    } else {
45
-        *s1 = NULL;
46
-    }
47
-}
48
-
49
-void cstring_destroy(char* s) {
50
-    CSTL_FREE(s);
51
-}

+ 1
- 1
src/string.c View File

@@ -17,7 +17,7 @@
17 17
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18 18
  */
19 19
 
20
-#define CSTL_VECTOR_ALLOC_SZ(sz) ((sz) + 1)
20
+#define CSTL_VECTOR_ALLOC_SZ(sz) ((sz) + STRING_CAPACITY_EXTRA)
21 21
 #define CSTL_VECTOR_INIT(v) string_init_capacity(v, 4)
22 22
 #include "../include/cstl/string.h"
23 23
 #include "../include/cstl/algorithm.h"

+ 4
- 4
test/Makefile View File

@@ -9,12 +9,12 @@ string:
9 9
 	$(CC) $(CFLAGS) ./src/string/*.c ../src/*.c lib/*.c && ./a.out
10 10
 vector: vector-int vector-struct-point
11 11
 vector-int:
12
-	$(CC) $(CFLAGS) ./src/vector/test-vector-int.c ./src/vector/vector-int.c lib/*.c && ./a.out
12
+	$(CC) $(CFLAGS) ./src/vector/test-vector-int.c ./src/vector/vector-int.c ../src/*.c lib/*.c && ./a.out
13 13
 vector-struct-point:
14
-	$(CC) $(CFLAGS) ./src/vector/test-vector-struct-point.c ./src/vector/vector-struct-point.c lib/*.c && ./a.out
14
+	$(CC) $(CFLAGS) ./src/vector/test-vector-struct-point.c ./src/vector/vector-struct-point.c ../src/*.c lib/*.c && ./a.out
15 15
 valgrind:
16
-	$(CC) $(INCLUDE_FLAGS) ./src/vector/test-vector-int.c ./src/vector/vector-int.c lib/*.c && valgrind ./a.out
17
-	$(CC) $(INCLUDE_FLAGS) ./src/vector/test-vector-struct-point.c ./src/vector/vector-struct-point.c lib/*.c && valgrind ./a.out
16
+	$(CC) $(INCLUDE_FLAGS) ./src/vector/test-vector-int.c ./src/vector/vector-int.c ../src/* lib/*.c && valgrind ./a.out
17
+	$(CC) $(INCLUDE_FLAGS) ./src/vector/test-vector-struct-point.c ./src/vector/vector-struct-point.c ../src/* lib/*.c && valgrind ./a.out
18 18
 	$(CC) $(INCLUDE_FLAGS) ./src/string/*.c ../src/*.c lib/*.c && valgrind ./a.out
19 19
 
20 20
 .PHONY: clean

Loading…
Cancel
Save