feat(str): add a string data structure for easy use
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Rick Barenthin 2022-07-26 23:47:46 +02:00
parent d81aeb1782
commit 0f305ab9fd
5 changed files with 123 additions and 0 deletions

View File

@ -23,6 +23,7 @@ endif ()
add_subdirectory(app)
add_subdirectory(library/list)
add_subdirectory(library/log)
add_subdirectory(library/str)
if ((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
AND BUILD_TESTING)

View File

@ -0,0 +1,20 @@
cmake_minimum_required(VERSION 3.21 FATAL_ERROR)
include("project-meta-info.in")
project(waitui-str
VERSION ${project_version}
DESCRIPTION ${project_description}
HOMEPAGE_URL ${project_homepage}
LANGUAGES C)
add_library(str OBJECT)
target_sources(str
PRIVATE
"src/str.c"
PUBLIC
"include/waitui/str.h"
)
target_include_directories(str PUBLIC "include")

View File

@ -0,0 +1,91 @@
/**
* @file str.h
* @author rick
* @date 19.02.20
* @brief File for the String implementation
*/
#ifndef WAITUI_STR_H
#define WAITUI_STR_H
#include <stdlib.h>
#include <string.h>
// -----------------------------------------------------------------------------
// Public types
// -----------------------------------------------------------------------------
/**
* @brief Type for strings that do not need to be '\0' terminated
*/
typedef struct str {
char *s;
unsigned long int len;
} str;
// -----------------------------------------------------------------------------
// Public defines
// -----------------------------------------------------------------------------
#define STR_NULL_INIT \
{ NULL, 0 }
#define STR_STATIC_INIT(_str_) \
{ (_str_), sizeof((_str_)) - 1 }
#define STR_FMT(_pstr_) \
(((_pstr_) != (str *) 0) ? (int) (_pstr_)->len : 0), \
(((_pstr_) != (str *) 0) ? (_pstr_)->s : "")
#define STR_STATIC_SET(_pstr_, _str_) \
do { \
if ((_pstr_)) { \
(_pstr_)->s = (_str_); \
(_pstr_)->len = sizeof((_str_)) - 1; \
} \
} while (0)
#define STR_STATIC_COPY(_dstr_, _str_) \
do { \
if ((_dstr_)) { \
char *tmp = (_str_); \
(_dstr_)->len = sizeof((_str_)) - 1; \
(_dstr_)->s = calloc((_dstr_)->len, sizeof(*(_dstr_)->s)); \
if ((_dstr_)->s) { memcpy((_dstr_)->s, tmp, (_dstr_)->len); } \
} \
} while (0)
#define STR_COPY(_dstr_, _pstr_) \
do { \
if ((_pstr_) && (_dstr_)) { \
(_dstr_)->len = (_pstr_)->len; \
(_dstr_)->s = calloc((_pstr_)->len, sizeof(*(_pstr_)->s)); \
if ((_dstr_)->s) { \
memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \
} \
} \
} while (0)
#define STR_COPY_WITH_NUL(_dstr_, _pstr_) \
do { \
if ((_pstr_) && (_dstr_)) { \
(_dstr_)->len = (_pstr_)->len; \
(_dstr_)->s = calloc((_pstr_)->len + 1, sizeof(*(_pstr_)->s)); \
if ((_dstr_)->s) { \
memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \
} \
} \
} while (0)
#define STR_FREE(_pstr_) \
do { \
if ((_pstr_)) { \
if ((_pstr_)->s) { free((_pstr_)->s); }; \
(_pstr_)->len = 0; \
(_pstr_)->s = NULL; \
} \
} while (0)
#endif//WAITUI_STR_H

View File

@ -0,0 +1,3 @@
set(project_version 0.0.1)
set(project_description "waitui str library")
set(project_homepage "http://example.com")

8
library/str/src/str.c Normal file
View File

@ -0,0 +1,8 @@
/**
* @file str.c
* @author rick
* @date 19.02.20
* @brief File for the String implementation
*/
#include "waitui/str.h"