Compare commits
6 Commits
5f93db596e
...
96e1a8e0d6
| Author | SHA1 | Date | |
|---|---|---|---|
|
96e1a8e0d6
|
|||
|
34b49d6090
|
|||
|
da792dda8a
|
|||
|
a09faf4d2d
|
|||
|
5b46fc25a1
|
|||
|
c1303e4ed7
|
2
.clangd
2
.clangd
@@ -1,2 +1,2 @@
|
||||
CompileFlags:
|
||||
Add: [-xc, -DL_ARRAY_IMPLEMENTATION]
|
||||
Add: [-xc, -DL_ARRAY_IMPLEMENTATION, -DL_STRING_IMPLEMENTATION]
|
||||
1
Makefile
1
Makefile
@@ -8,6 +8,7 @@ BUILD_DIR := build
|
||||
TEST_BUILD_DIR := $(BUILD_DIR)/test
|
||||
TEST_BUILD_FILES := $(TEST_BUILD_DIR)/test.o \
|
||||
$(TEST_BUILD_DIR)/array.o \
|
||||
$(TEST_BUILD_DIR)/string.o \
|
||||
|
||||
INCLUDE_INSTALL_PATH := /usr/local/include/LTypes
|
||||
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
To use any single header file, either include `version.h` or remove the include from the top.
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef LTYPES_ARRAY_H
|
||||
#define LTYPES_ARRAY_H
|
||||
|
||||
#include "version.h"
|
||||
#include <stddef.h>
|
||||
|
||||
typedef struct {
|
||||
@@ -13,6 +12,7 @@ typedef struct {
|
||||
LArray* l_array_create();
|
||||
void l_array_delete(LArray* array);
|
||||
int l_array_append(LArray* array, void* item);
|
||||
int l_array_swap(LArray* array, int indexA, int indexB);
|
||||
void l_array_clear(LArray* array);
|
||||
|
||||
#ifdef L_ARRAY_IMPLEMENTATION
|
||||
@@ -39,6 +39,16 @@ int l_array_append(LArray* array, void* item) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int l_array_swap(LArray* array, int indexA, int indexB) {
|
||||
if (array->count < (size_t)indexA || array->count < (size_t)indexB || indexA == indexB) return 1;
|
||||
|
||||
void* itemA = array->items[indexA];
|
||||
array->items[indexA] = array->items[indexB];
|
||||
array->items[indexB] = itemA;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void l_array_clear(LArray* array) {
|
||||
free(array->items);
|
||||
array->items = NULL;
|
||||
60
include/lstring.h
Normal file
60
include/lstring.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#ifndef LTYPES_STRING_H
|
||||
#define LTYPES_STRING_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STRING_LEN 1024
|
||||
|
||||
typedef struct {
|
||||
char* buf;
|
||||
size_t len;
|
||||
} LString;
|
||||
|
||||
LString* l_string_new(const char* fmt, ...);
|
||||
void l_string_delete(LString* str);
|
||||
int l_string_cmp(LString* strA, LString* strB);
|
||||
LString* l_string_concat(LString* strA, LString* strB);
|
||||
|
||||
#ifdef L_STRING_IMPLEMENTATION
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
LString* l_string_new(const char* fmt, ...) {
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
|
||||
char temp_string[MAX_STRING_LEN];
|
||||
vsnprintf(temp_string, MAX_STRING_LEN, fmt, args);
|
||||
|
||||
va_end(args);
|
||||
|
||||
LString* str = calloc(1, sizeof(LString));
|
||||
str->len = strlen(temp_string);
|
||||
|
||||
str->buf = malloc(str->len + 1);
|
||||
if (str->buf != NULL) {
|
||||
strcpy(str->buf, temp_string);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
void l_string_delete(LString* str) {
|
||||
free(str);
|
||||
}
|
||||
|
||||
int l_string_cmp(LString* strA, LString* strB) {
|
||||
int num_chars = (strA->len <= strB->len) ? strB->len : strA->len;
|
||||
return strncmp(strA->buf, strB->buf, num_chars);
|
||||
}
|
||||
|
||||
LString* l_string_concat(LString* strA, LString* strB) {
|
||||
return l_string_new("%.*s%.*s", strA->len, strA->buf, strB->len, strB->buf);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
@@ -1,9 +0,0 @@
|
||||
#ifndef LTYPES_VERSION_H
|
||||
#define LTYPES_VERSION_H
|
||||
|
||||
#define L_TYPES_AUTHOR "Lucielle <luci@git.lunarware.tech"
|
||||
#define L_TYPES_REPO "https://git.lunarware.tech/lucielle/ltypes.git"
|
||||
#define L_TYPES_VERSION "0.1.0"
|
||||
#define L_TYPES_LICENSE "GNU GPL v3"
|
||||
|
||||
#endif
|
||||
@@ -1,5 +1,5 @@
|
||||
#define L_ARRAY_IMPLEMENTATION
|
||||
#include "../include/array.h"
|
||||
#include "../include/larray.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int test_l_array() {
|
||||
@@ -19,9 +19,14 @@ int test_l_array() {
|
||||
if (array->count != 2) RETURN_CODE++;
|
||||
if (array->capacity != 8) RETURN_CODE++;
|
||||
|
||||
l_array_swap(array, 0, 1);
|
||||
if (*(int*)array->items[0] != 5) RETURN_CODE++;
|
||||
if (*(int*)array->items[1] != 15) RETURN_CODE++;
|
||||
|
||||
l_array_clear(array);
|
||||
if (array->items != NULL) RETURN_CODE++;
|
||||
|
||||
printf("%d\n", RETURN_CODE);
|
||||
l_array_delete(array);
|
||||
return RETURN_CODE;
|
||||
}
|
||||
29
test/string.c
Normal file
29
test/string.c
Normal file
@@ -0,0 +1,29 @@
|
||||
#define L_STRING_IMPLEMENTATION
|
||||
#include "../include/lstring.h"
|
||||
#include <stdio.h>
|
||||
|
||||
int test_l_string() {
|
||||
int RETURN_CODE = 0;
|
||||
printf("test_l_string() : ");
|
||||
|
||||
int integer_0 = 25;
|
||||
int integer_1 = 15;
|
||||
|
||||
LString* test_string_0 = l_string_new("I would like to purchase item id `%d`", integer_0);
|
||||
LString* test_string_1 = l_string_new(" for `%d` coins.", integer_1);
|
||||
|
||||
if (l_string_cmp(test_string_0, l_string_new("I would like to purchase item id `25`"))) RETURN_CODE++;
|
||||
if (l_string_cmp(test_string_1, l_string_new(" for `15` coins."))) RETURN_CODE++;
|
||||
|
||||
LString* test_string_2 = l_string_new("I would like to purchase item id `25` for `15` coins.");
|
||||
LString* test_string_3 = l_string_concat(test_string_0, test_string_1);
|
||||
if (l_string_cmp(test_string_2, test_string_3)) RETURN_CODE++;
|
||||
|
||||
l_string_delete(test_string_0);
|
||||
l_string_delete(test_string_1);
|
||||
l_string_delete(test_string_2);
|
||||
l_string_delete(test_string_3);
|
||||
|
||||
printf("%d\n", RETURN_CODE);
|
||||
return RETURN_CODE;
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include "../include/version.h"
|
||||
|
||||
#define L_TYPES_AUTHOR "Lucielle <luci@git.lunarware.tech"
|
||||
#define L_TYPES_REPO "https://git.lunarware.tech/lucielle/ltypes.git"
|
||||
#define L_TYPES_VERSION "0.2.0"
|
||||
#define L_TYPES_LICENSE "GNU GPL v3"
|
||||
|
||||
int test_l_array();
|
||||
int test_l_string();
|
||||
|
||||
int main(void) {
|
||||
printf("AUTHOR: %s\n", L_TYPES_AUTHOR);
|
||||
@@ -12,6 +17,7 @@ int main(void) {
|
||||
int RETURN_CODE = 0;
|
||||
|
||||
RETURN_CODE += test_l_array();
|
||||
RETURN_CODE += test_l_string();
|
||||
|
||||
printf("\nError Total: %d\n", RETURN_CODE);
|
||||
return RETURN_CODE;
|
||||
|
||||
Reference in New Issue
Block a user