add string type
This commit is contained in:
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
|
||||
|
||||
|
||||
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
|
||||
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;
|
||||
}
|
||||
@@ -2,10 +2,11 @@
|
||||
|
||||
#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.1"
|
||||
#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);
|
||||
@@ -16,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