O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
utils.c
1/* ************************************************************************** */
2/* ____ _ _____ ____ _ _ _ _____ _ _ ____ ____ */
3/* / ___| / \ | ___| _ \ / \ | \ | | |_ _| || | | _ \/ ___| */
4/* \___ \ / _ \ | |_ | |_) | / _ \ | \| | | | | || |_| | | \___ \ */
5/* ___) / ___ \| _| | _ < / ___ \| |\ | | | |__ _| |_| |___) | */
6/* |____/_/ \_|_| |_| \_/_/ \_|_| \_| |_| |_| |____/|____/ */
7/* */
8/* Copyright 2023, SAFRAN T4DS, ALL RIGHTS RESERVED */
9/* */
10/* @file utils.c */
11/* @author Antoine GAGNIERE */
12/* */
13/* ************************************************************************** */
14
15#include "o2s/string.h"
16
17#include <iso646.h> // not and or
18
24char* string_to_cstring(string_t* self)
25{
26 if (not string_reserve(self, 1))
27 return NULL;
28 *(self->count + (char*)self->start) = '\0';
29 return self->start;
30}
31
33bool string_reserve(string_t* self, size_t count)
34{
35 return array_reserve(self, count);
36}
37
39bool string_trim(string_t* self)
40{
41 return array_trim(self);
42}
43
45bool string_is_equal(const string_t* one, const string_t* two)
46{
47 return (one->count == two->count) and (one->count == 0 or (memcmp(one->start, two->start, one->count) == 0));
48}
49
51bool string_contains(const string_t* self, char character)
52{
53 if (self->count == 0)
54 return false;
55 return memchr(self->start, character, self->count);
56}
57
59bool is_char_in_cstring(char character, const char* cstring)
60{
61 return strchr(cstring, character);
62}
bool array_trim(array_t *self)
Resizes the underlying storage to fit exactly the current elements count.
Definition memory.c:65
bool array_reserve(array_t *self, size_t count)
Ensures the array has enough capacity to fit count new elements, reallocating if needed.
Definition memory.c:47
Dynamic string implementation.
array_t string_t
A string is an array of char.
Definition string.h:30
void * start
Underlying storage.
Definition array.h:25
size_t count
Number of elements currently stored.
Definition array.h:27