Archeus 0.0.0
A C library and game engine that focuses on documentation
Loading...
Searching...
No Matches
string.c File Reference
#include "arc/std/string.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

void ARC_String_Create (ARC_String **string, char *data, uint64_t length)
 creates ARC_String type
 
void ARC_String_CreateWithStrlen (ARC_String **string, char *data)
 creates ARC_String type with strinlen
 
void ARC_String_CreateEmpty (ARC_String **string, uint64_t length)
 creates an empty ARC_String type, useful if you want to add to the string over time without having to keep adding to the size
 
void ARC_String_Destroy (ARC_String *string)
 destroys ARC_String type
 
void ARC_String_Copy (ARC_String **copy, ARC_String *original)
 copy a ARC_String
 
void ARC_String_CopySubstring (ARC_String **substring, ARC_String *original, uint64_t start, uint64_t length)
 copy a subtring from a givin ARC_String
 
void ARC_String_ReplaceWithSubstring (ARC_String **string, uint64_t start, uint64_t length)
 replaces a string with a section of itself
 
void ARC_String_RemoveSubstring (ARC_String **newString, ARC_String *original, ARC_String *substring)
 copy a subtring from a givin ARC_String
 
void ARC_String_Append (ARC_String **string, ARC_String *append)
 appends to an ARC_String with an ARC_String
 
void ARC_String_AppendCString (ARC_String **string, const char *cstring, uint64_t length)
 appends to an ARC_String with an ARC_String
 
void ARC_String_AppendCStringWithStrlen (ARC_String **string, const char *cstring)
 appends to an ARC_String with an ARC_String
 
ARC_Bool ARC_String_Equals (ARC_String *first, ARC_String *second)
 checks if two strings are the same
 
ARC_Bool ARC_String_EqualsCString (ARC_String *string, const char *cstring, uint64_t length)
 check if ARC_String and cstring match
 
ARC_Bool ARC_String_EqualsCStringWithStrlen (ARC_String *string, const char *cstring)
 check if ARC_String and cstring match
 
ARC_Bool ARC_String_SubstringEquals (ARC_String *first, uint64_t offset, ARC_String *second)
 check if substring of first equals second string
 
ARC_Bool ARC_String_SubstringEqualsCString (ARC_String *string, uint64_t offset, const char *cstring, uint64_t length)
 check if ARC_String and cstring match
 
ARC_Bool ARC_String_IsAlpha (ARC_String *string)
 checks if string is alphabetic
 
ARC_Bool ARC_String_IsNumeric (ARC_String *string)
 checks if string is made out of only numbers
 
uint64_t ARC_String_ToUint64_t (ARC_String *string)
 converst substring from string to uint64_t
 
int64_t ARC_String_ToInt64_t (ARC_String *string)
 converst substring from string to int64_t
 
double ARC_String_ToDouble (ARC_String *string)
 converst substring from string to double
 
uint64_t ARC_String_Find (ARC_String *string, ARC_String *substring)
 takes a given string, and assigns index and length for position of first matching substring
 
uint64_t ARC_String_FindCString (ARC_String *string, const char *cstring, uint64_t length)
 takes given cstring and gives position of first matching
 
uint64_t ARC_String_FindCStringWithStrlen (ARC_String *string, const char *cstring)
 takes given cstring and gives position of first matching
 
uint64_t ARC_String_FindBack (ARC_String *string, ARC_String *substring)
 takes a given string, and assigns index and length for position of last matching substring
 
uint64_t ARC_String_FindBackCString (ARC_String *string, const char *cstring, uint64_t length)
 takes a given cstring and give position of last matching
 
uint64_t ARC_String_FindBackCStringWithStrlen (ARC_String *string, const char *cstring)
 takes a given cstring and give position of last matching
 
void ARC_String_StripEnds (ARC_String **stripped, ARC_String *original, char charToStrip)
 strips the ends based on a given char
 
void ARC_String_StripWhitespace (ARC_String **stripped, ARC_String *original)
 strips whitespace from a ARC_String
 
void ARC_String_StripEndsWhitespace (ARC_String **stripped, ARC_String *original)
 strips the whitespace from the ends of a string
 
void ARC_String_Merge (ARC_String **combined, ARC_String *first, ARC_String *second)
 merges two strings together
 
void ARC_String_RemoveSection (ARC_String **newString, ARC_String *original, uint64_t removeIndex, uint64_t removeLength)
 copy a subtring from a givin ARC_String
 
void ARC_String_ReplaceMatching (ARC_String **string, ARC_String *pattern, ARC_String *replacement)
 replaces characters in string matching the given pattern
 
void ARC_String_CopyReplaceMatching (ARC_String **newString, ARC_String *original, ARC_String *pattern, ARC_String *replacement)
 replaces characters in a copy of a string matching the given pattern
 
void ARC_String_ReplaceMatchingCString (ARC_String **string, char *patternCString, uint64_t patternLength, char *replacementCString, uint64_t replacementLength)
 replaces characters in string matching the given pattern
 
void ARC_String_ReplaceMatchingCStringWithStrlen (ARC_String **string, char *patternCString, char *replacementCString)
 replaces characters in string matching the given pattern
 

Function Documentation

◆ ARC_String_Append()

void ARC_String_Append ( ARC_String ** string,
ARC_String * append )

appends to an ARC_String with an ARC_String

Note
this uses ARC_String_AppendCString, so debug logs will be thrown in that function not this one
Parameters
stringthe string to add to, will not change on error
appendthe string that will be added to the back of string

Definition at line 126 of file string.c.

126 {
127 ARC_String_AppendCString(string, append->data, append->length);
128}
void ARC_String_AppendCString(ARC_String **string, const char *cstring, uint64_t length)
appends to an ARC_String with an ARC_String
Definition string.c:130
uint64_t length
Definition string.h:16
char * data
Definition string.h:15

References ARC_String_AppendCString(), ARC_String::data, and ARC_String::length.

Referenced by ARC_ParserData_HelperRecurseStringAdd(), and ARC_ParserLangParsedData_RecurseStringAdd().

◆ ARC_String_AppendCString()

void ARC_String_AppendCString ( ARC_String ** string,
const char * cstring,
uint64_t length )

appends to an ARC_String with an ARC_String

Parameters
stringthe string to add to, will not change on error
cstringthe cstring that will be added to the back of string
lengththe length of the cstring that is being added

Definition at line 130 of file string.c.

130 {
131 char *data = (char *)malloc(sizeof(char) * ((*string)->length + length + 1));
132
133 strncpy(data, (*string)->data, (*string)->length);
134 strncpy(data + (*string)->length, cstring, length);
135 data[(*string)->length + length] = '\0';
136
137 free((*string)->data);
138
139 (*string)->data = data;
140 (*string)->length = (*string)->length + length;
141}

Referenced by ARC_ParserLangParsedData_RecurseStringAdd(), ARC_Ssh_ExecStrInNewSessionAndGetResponse(), ARC_String_Append(), and ARC_String_AppendCStringWithStrlen().

◆ ARC_String_AppendCStringWithStrlen()

void ARC_String_AppendCStringWithStrlen ( ARC_String ** string,
const char * cstring )

appends to an ARC_String with an ARC_String

Note
this uses ARC_String_AppendCString, so debug logs will be thrown in that function not this one
Parameters
stringthe string to add to, will not change on error
cstringthe cstring that will be added to the back of string

Definition at line 143 of file string.c.

143 {
144 ARC_String_AppendCString(string, cstring, strlen(cstring));
145}

References ARC_String_AppendCString().

Referenced by ARC_Config_FileIO().

◆ ARC_String_Copy()

void ARC_String_Copy ( ARC_String ** copy,
ARC_String * original )

copy a ARC_String

Parameters
copycopy of oldString, will be set to NULL on error
originaloriginal string that is being copied

Definition at line 62 of file string.c.

62 {
63 if(!original){
65 *copy = NULL;
66 return;
67 }
68
69 ARC_String_Create(copy, original->data, original->length);
70}
int32_t arc_errno
Definition errno.c:5
#define ARC_ERRNO_NULL
Definition errno.h:6
void ARC_String_Create(ARC_String **string, char *data, uint64_t length)
creates ARC_String type
Definition string.c:9

References arc_errno, ARC_ERRNO_NULL, ARC_String_Create(), ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_RemoveAndRunCommands(), ARC_Config_StripComment(), ARC_LexerTokenRule_CreateAndReturnMatchCharInStringRule(), ARC_LexerTokenRule_CreateAndReturnMatchStringRule(), ARC_Parser_CreateFromString(), ARC_String_CopyReplaceMatching(), ARC_String_RemoveSection(), and ARC_Text_Create().

◆ ARC_String_CopyReplaceMatching()

void ARC_String_CopyReplaceMatching ( ARC_String ** newString,
ARC_String * original,
ARC_String * pattern,
ARC_String * replacement )

replaces characters in a copy of a string matching the given pattern

Note
original will not be modified
newString will need to be destroyed if it is not set to NULL
Parameters
newStringan empty string that this function will fill with a copy with replacements, will be set to NULL and arc_errno set on fail
originalthe original string that will be copied
patternthe pattern to replace in the string on match
replacementthe string that will replace the matched pattern

Definition at line 537 of file string.c.

537 {
538 //TODO: probs want to check if the replacement goes over a uint64_t size
539 if(original == NULL || pattern == NULL || replacement == NULL){
541 ARC_DEBUG_LOG_ERROR("ARC_String_CopyReplaceMatching(newString, original, pattern, replacement), original, pattern, or replacement was null");
542 return;
543 }
544
545 uint64_t numberOfMatches = 0;
546 for(uint64_t originalIndex = 0; originalIndex < original->length; originalIndex++){
547 if(ARC_String_SubstringEquals(original, originalIndex, pattern)){
548 numberOfMatches++;
549 originalIndex += pattern->length - 1;
550 }
551 }
552
553 //no matches were found, but that isn't an error, so copy and return
554 if(numberOfMatches == 0){
555 ARC_String_Copy(newString, original);
556 return;
557 }
558
559 (*newString) = (ARC_String *)malloc(sizeof(ARC_String));
560 (*newString)->length = original->length + (replacement->length - pattern->length);
561 (*newString)->data = (char *)malloc(sizeof(char *) * original->length + 1);
562
563 for(uint64_t originalIndex = 0, newIndex = 0; originalIndex < original->length; originalIndex++, newIndex++){
564 if(ARC_String_SubstringEquals(original, originalIndex, pattern)){
565 for(uint64_t replacementIndex = 0; replacementIndex < replacement->length; replacementIndex++){
566 (*newString)->data[newIndex + replacementIndex] = replacement->data[replacementIndex];
567 }
568
569 originalIndex += pattern->length - 1;
570 newIndex += replacement->length - 1;
571 continue;
572 }
573
574 (*newString)->data[newIndex] = original->data[originalIndex];
575 }
576
577 (*newString)->data[(*newString)->length] = '\0';
578}
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
void ARC_String_Copy(ARC_String **copy, ARC_String *original)
copy a ARC_String
Definition string.c:62
ARC_Bool ARC_String_SubstringEquals(ARC_String *first, uint64_t offset, ARC_String *second)
check if substring of first equals second string
Definition string.c:175
substring position within a string
Definition string.h:14

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String_Copy(), ARC_String_SubstringEquals(), ARC_String::data, and ARC_String::length.

Referenced by ARC_String_ReplaceMatching().

◆ ARC_String_CopySubstring()

void ARC_String_CopySubstring ( ARC_String ** substring,
ARC_String * original,
uint64_t start,
uint64_t length )

copy a subtring from a givin ARC_String

Parameters
substringnew coppied substring, will be null on error
originalstring to copy substring from
startstarting index in relation on original
lengthlength of substring that is being created

Definition at line 72 of file string.c.

72 {
73 if(!original){
75 *substring = NULL;
76 return;
77 }
78
79 if(length == 0){
80 *substring = NULL;
81 return;
82 }
83
84 if(start + length > original->length){
86 *substring = NULL;
87 return;
88 }
89
90 char data[length];
91 for(uint32_t i = 0; i < length; i++){
92 data[i] = 0;
93 }
94
95 strncpy(data, original->data + start, length);
96
97 ARC_String_Create(substring, data, length);
98}
#define ARC_ERRNO_DATA
Definition errno.h:7

References arc_errno, ARC_ERRNO_DATA, ARC_ERRNO_NULL, ARC_String_Create(), ARC_String::data, and ARC_String::length.

Referenced by ARC_Audio_Read(), ARC_Config_Get(), ARC_Config_GetNameAndValue(), ARC_Config_Recurse(), ARC_Config_RemoveAndRunCommands(), ARC_Config_RunCommand(), ARC_Config_SetKeyGroup(), ARC_Config_StripComment(), ARC_Point_Read(), ARC_Rect_Read(), ARC_RectArray_Read(), ARC_RectArray_ReadRect(), ARC_SDL_Texture_Read(), ARC_Sprite_Delete(), ARC_Sprite_Read(), ARC_Spritesheet_Delete(), ARC_Spritesheet_Read(), ARC_Spritesheet_ReadTexture(), ARC_String_RemoveSection(), ARC_String_ReplaceWithSubstring(), and ARC_String_StripEndsWhitespace().

◆ ARC_String_Create()

void ARC_String_Create ( ARC_String ** string,
char * data,
uint64_t length )

creates ARC_String type

Parameters
stringARC_String to create
datacstring that will be stored in ARC_String
lengthlength of ARC_String

Definition at line 9 of file string.c.

9 {
10 //check if the size is too big to create and error if so
11 if(length == ~(uint64_t)0){
13 ARC_DEBUG_LOG_ERROR("ARC_String_Create(string, data, length), length was max uint64_t which is bigger than allowed");
14 *string = NULL;
15 return;
16 }
17
18 //create the string container and malloc the char array (this will allways be bigger than zero so there will be no issue if zero is passed in)
19 *string = (ARC_String *)malloc(sizeof(ARC_String));
20 (*string)->data = (char *)malloc(sizeof(char) * (length + 1));
21 (*string)->length = length;
22
23 //if the string has a size, copy the string
24 if(length > 0){
25 strncpy((*string)->data, data, length);
26 }
27
28 //set the end of the string to \0 (to mirror how cstrings work)
29 (*string)->data[length] = '\0';
30}
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10

References ARC_DEBUG_LOG_ERROR, arc_errno, and ARC_ERRNO_OVERFLOW.

Referenced by ARC_Config_RemoveAndRunCommands(), ARC_Config_RemoveComments(), ARC_Config_RunCommand(), ARC_ConsoleBuffer_AddChar(), ARC_ConsoleView_GetStringInput(), ARC_IO_FileToStr(), ARC_Lexer_AutomataMatchCharOrBetweenFn(), ARC_ParserCSVData_GetDataTag(), ARC_ParserLangParsedData_CreateTagString(), ARC_ParserLangParsedData_GetArgumentTag(), ARC_Ssh_ExecStrInNewSessionAndGetResponse(), ARC_String_Copy(), ARC_String_CopySubstring(), ARC_String_CreateWithStrlen(), ARC_String_Merge(), ARC_String_StripEnds(), ARC_String_StripWhitespace(), and TEST_ParserData_CreateStringFn().

◆ ARC_String_CreateEmpty()

void ARC_String_CreateEmpty ( ARC_String ** string,
uint64_t length )

creates an empty ARC_String type, useful if you want to add to the string over time without having to keep adding to the size

Parameters
stringARC_String to create
lengthlength of ARC_String

Definition at line 37 of file string.c.

37 {
38 //check if the size is too big to create and error if so
39 if(length == ~(uint64_t)0){
41 ARC_DEBUG_LOG_ERROR("ARC_String_Create(string, data, length), length was max uint64_t which is bigger than allowed");
42 *string = NULL;
43 return;
44 }
45
46 //create the string container and initialize data with all 0s for the size
47 *string = (ARC_String *)malloc(sizeof(ARC_String));
48 (*string)->data = (char *)calloc(sizeof(char), length + 1);
49 (*string)->length = length;
50}

References ARC_DEBUG_LOG_ERROR, arc_errno, and ARC_ERRNO_OVERFLOW.

◆ ARC_String_CreateWithStrlen()

void ARC_String_CreateWithStrlen ( ARC_String ** string,
char * data )

creates ARC_String type with strinlen

Parameters
stringARC_String to create
datacstring that will be stored in ARC_String

Definition at line 32 of file string.c.

32 {
33 //create the string passing in the strlen of data for the length
34 ARC_String_Create(string, data, strlen(data));
35}

References ARC_String_Create().

Referenced by ARC_Chemical_Create(), ARC_Lexer_InitBasicTokenRules(), ARC_ParserCSV_CreateAsParser(), ARC_ParserLang_InitLexerRulesFn(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), ARC_TEST(), and ARC_TEST().

◆ ARC_String_Destroy()

void ARC_String_Destroy ( ARC_String * string)

destroys ARC_String type

Parameters
stringstring that will be destroyed

Definition at line 52 of file string.c.

52 {
53 //check if the string's data exists and if so free it
54 if(string->data != NULL){
55 free(string->data);
56 }
57
58 //free the string itself
59 free(string);
60}

References ARC_String::data.

Referenced by ARC_Chemical_Create(), ARC_Config_FileIO(), ARC_Config_Get(), ARC_Config_GetNameAndValue(), ARC_Config_Recurse(), ARC_Config_RemoveAndRunCommands(), ARC_Config_RemoveComments(), ARC_Config_RunCommand(), ARC_Config_SetKeyGroup(), ARC_Config_StripComment(), ARC_ConsoleBuffer_AddChar(), ARC_ConsoleBuffer_Clear(), ARC_ConsoleBuffer_Destroy(), ARC_ConsoleShell_Destroy(), ARC_Lexer_InitBasicTokenRules(), ARC_Lexer_LexFile(), ARC_Lexer_LexString(), ARC_LexerToken_VectorDestroyDataFn(), ARC_LexerTokenRule_DestroyStringAutomataDataFn(), ARC_ParserCSV_CreateAsParser(), ARC_ParserCSVData_DestroyFn(), ARC_ParserCSVData_GetDataTag(), ARC_ParserLang_InitLexerRulesFn(), ARC_ParserLangParsedData_CreateBodyTag(), ARC_ParserLangParsedData_GetArgumentTag(), ARC_Point_Read(), ARC_Rect_Read(), ARC_RectArray_Read(), ARC_RectArray_ReadRect(), ARC_SDL_Texture_Read(), ARC_Sprite_Delete(), ARC_Sprite_Read(), ARC_Spritesheet_Delete(), ARC_Spritesheet_Read(), ARC_Spritesheet_ReadTexture(), ARC_Ssh_ExecStrInNewSessionAndGetResponse(), ARC_String_RemoveSection(), ARC_String_ReplaceMatching(), ARC_String_ReplaceMatchingCString(), ARC_String_ReplaceWithSubstring(), ARC_TEST(), ARC_Text_Destroy(), and TEST_ParserData_DestroyStringFn().

◆ ARC_String_Equals()

ARC_Bool ARC_String_Equals ( ARC_String * first,
ARC_String * second )

checks if two strings are the same

Parameters
firststring to check against second
secondstring to check against first
Returns
ARC_True if match, ARC_False if they don't match

Definition at line 147 of file string.c.

147 {
148 if(first->length != second->length){
149 return ARC_False;
150 }
151
152 if(strncmp(first->data, second->data, first->length)){
153 return ARC_False;
154 }
155
156 return ARC_True;
157}
#define ARC_False
Definition bool.h:12
#define ARC_True
Definition bool.h:11

References ARC_False, ARC_True, ARC_String::data, and ARC_String::length.

◆ ARC_String_EqualsCString()

ARC_Bool ARC_String_EqualsCString ( ARC_String * string,
const char * cstring,
uint64_t length )

check if ARC_String and cstring match

Parameters
stringARC_string to check
cstringcstring to check
lengthlength of cstring
Returns
ARC_True if match, ARC_False if they don't match

Definition at line 159 of file string.c.

159 {
160 if(string->length != length){
161 return ARC_False;
162 }
163
164 if(strncmp(string->data, cstring, string->length)){
165 return ARC_False;
166 }
167
168 return ARC_True;
169}

References ARC_False, ARC_True, ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_Recurse(), ARC_Config_RunCommand(), and ARC_String_EqualsCStringWithStrlen().

◆ ARC_String_EqualsCStringWithStrlen()

ARC_Bool ARC_String_EqualsCStringWithStrlen ( ARC_String * string,
const char * cstring )

check if ARC_String and cstring match

Note
will use strlen to get the length of the cstring
Parameters
stringARC_string to check
cstringcstring to check
Returns
ARC_True if match, ARC_False if they don't match

Definition at line 171 of file string.c.

171 {
172 return ARC_String_EqualsCString(string, cstring, strlen(cstring));
173}
ARC_Bool ARC_String_EqualsCString(ARC_String *string, const char *cstring, uint64_t length)
check if ARC_String and cstring match
Definition string.c:159

References ARC_String_EqualsCString().

Referenced by ARC_Chemical_GetStringIdFn(), ARC_ParserCSV_GetStringIdFn(), ARC_TEST(), TEST_Parser_GetStringIdFn(), and TEST_ParserLang_GetIdFn().

◆ ARC_String_Find()

uint64_t ARC_String_Find ( ARC_String * string,
ARC_String * substring )

takes a given string, and assigns index and length for position of first matching substring

Parameters
stringthe string that will be searched
substrsubstring to find within string
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 231 of file string.c.

231 {
232 if(!string || !substring){
233 ARC_DEBUG_LOG_ERROR("ARC_String_Find(string, substring), string or substring was null");
235 return ~(uint64_t)0;
236 }
237
238 if(substring->length > string->length){
239 return ~(uint64_t)0;
240 }
241
242 uint64_t max = string->length - (substring->length - 1);
243 for(uint64_t i = 0; max; i++, max--){
244 if(!strncmp(string->data + i, substring->data, substring->length)){
245 return i;
246 }
247 }
248
249 return ~(uint64_t)0;
250}

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_RemoveAndRunCommands(), ARC_Config_RunCommand(), ARC_Config_StripComment(), and ARC_String_RemoveSubstring().

◆ ARC_String_FindBack()

uint64_t ARC_String_FindBack ( ARC_String * string,
ARC_String * substring )

takes a given string, and assigns index and length for position of last matching substring

Parameters
stringthe string that will be searched
substrsubstring to find within string
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 277 of file string.c.

277 {
278 if(!string || !substring){
280 ARC_DEBUG_LOG_ERROR("ARC_String_FindBack(string, substring), string or substring was null");
281 return ~(uint64_t)0;
282 }
283
284 if(substring->length > string->length){
285 return ~(uint64_t)0;
286 }
287
288 uint64_t max = string->length - (substring->length - 1);
289 for(; max; max--){
290 if(!strncmp(string->data + (max - 1), substring->data, substring->length)){
291 return max;
292 }
293 }
294
295 return ~(uint64_t)0;
296}

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String::data, and ARC_String::length.

◆ ARC_String_FindBackCString()

uint64_t ARC_String_FindBackCString ( ARC_String * string,
const char * cstring,
uint64_t length )

takes a given cstring and give position of last matching

Parameters
stringthe string that will be searched
cstringthe cstring to find within string
lengththe length of cstring
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 298 of file string.c.

298 {
299 if(!string || !cstring){
301 ARC_DEBUG_LOG_ERROR("ARC_String_FindBack(string, substring), string or substring was null");
302 return ~(uint64_t)0;
303 }
304
305 if(length > string->length){
306 return ~(uint64_t)0;
307 }
308
309 uint64_t max = string->length - (length - 1);
310 for(; max; max--){
311 if(!strncmp(string->data + (max - 1), cstring, length)){
312 return max;
313 }
314 }
315
316 return ~(uint64_t)0;
317}

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String::data, and ARC_String::length.

Referenced by ARC_String_FindBackCStringWithStrlen().

◆ ARC_String_FindBackCStringWithStrlen()

uint64_t ARC_String_FindBackCStringWithStrlen ( ARC_String * string,
const char * cstring )

takes a given cstring and give position of last matching

Note
will use strlen to get the length of the cstring
Parameters
stringthe string that will be searched
cstringthe cstring to find within string
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 319 of file string.c.

319 {
320 return ARC_String_FindBackCString(string, cstring, strlen(cstring));
321}
uint64_t ARC_String_FindBackCString(ARC_String *string, const char *cstring, uint64_t length)
takes a given cstring and give position of last matching
Definition string.c:298

References ARC_String_FindBackCString().

◆ ARC_String_FindCString()

uint64_t ARC_String_FindCString ( ARC_String * string,
const char * cstring,
uint64_t length )

takes given cstring and gives position of first matching

Parameters
stringthe string that will be searched
cstringthe cstring to find within string
lengththe length of cstring
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 252 of file string.c.

252 {
253 if(!string || !cstring){
255 ARC_DEBUG_LOG_ERROR("ARC_String_FindCString(string, cstring, length), string or cstring was null");
256 return ~(uint64_t)0;
257 }
258
259 if(string->length < length){
260 return ~(uint64_t)0;
261 }
262
263 uint64_t max = string->length - (length - 1);
264 for(uint64_t i = 0; i < max; i++){
265 if(!strncmp(string->data + i, cstring, length)){
266 return i + 1;
267 }
268 }
269
270 return ~(uint64_t)0;
271}

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_Get(), ARC_Config_GetNameAndValue(), ARC_Config_Recurse(), ARC_Config_SetKeyGroup(), ARC_GraphicsConfig_GetIndexAndErrorCheck(), ARC_MathConfig_GetIndexAndErrorCheck(), and ARC_String_FindCStringWithStrlen().

◆ ARC_String_FindCStringWithStrlen()

uint64_t ARC_String_FindCStringWithStrlen ( ARC_String * string,
const char * cstring )

takes given cstring and gives position of first matching

Note
will use strlen to get the length of the cstring
Parameters
stringthe string that will be searched
cstringthe cstring to find within string
Returns
~(uint64_t)0 on error, anything else on success

Definition at line 273 of file string.c.

273 {
274 return ARC_String_FindCString(string, cstring, strlen(cstring));
275}
uint64_t ARC_String_FindCString(ARC_String *string, const char *cstring, uint64_t length)
takes given cstring and gives position of first matching
Definition string.c:252

References ARC_String_FindCString().

◆ ARC_String_IsAlpha()

ARC_Bool ARC_String_IsAlpha ( ARC_String * string)

checks if string is alphabetic

Parameters
stringstring to check
Returns
ARC_True if alphabetic, ARC_False if not alphabetic

Definition at line 192 of file string.c.

192 {
193 for(uint64_t length = string->length; length; length--){
194 if(string->data[length - 1] >= 'a' && string->data[length - 1] <= 'z'){
195 continue;
196 }
197
198 if(string->data[length - 1] >= 'A' && string->data[length - 1] <= 'Z'){
199 continue;
200 }
201
202 return ARC_True;
203 }
204
205 return ARC_False;
206}

References ARC_False, ARC_True, ARC_String::data, and ARC_String::length.

◆ ARC_String_IsNumeric()

ARC_Bool ARC_String_IsNumeric ( ARC_String * string)

checks if string is made out of only numbers

Parameters
stringstring to check
Returns
ARC_True if it is numeric, ARC_False if it is not numeric

Definition at line 208 of file string.c.

208 {
209 for(uint64_t index = 0; index < string->length; index++){
210 if(string->data[index] < '0' || string->data[index] > '9'){
211 return ARC_False;
212 }
213 }
214
215 return ARC_True;
216}

References ARC_False, ARC_True, and ARC_String::data.

◆ ARC_String_Merge()

void ARC_String_Merge ( ARC_String ** combined,
ARC_String * first,
ARC_String * second )

merges two strings together

Parameters
combinednew ARC_String of combined strings frist + second
firstfirst part of string to combine
secondsecond part of string to combine

Definition at line 485 of file string.c.

485 {
486 char data[first->length + second->length];
487 for(uint32_t i = 0; i < first->length; i++){
488 data[i] = first->data[i];
489 }
490
491 for(uint32_t i = 0; i < second->length; i++){
492 data[i + first->length] = second->data[i];
493 }
494
495 ARC_String_Create(combined, data, first->length + second->length);
496}

References ARC_String_Create(), ARC_String::data, and ARC_String::length.

Referenced by ARC_ConsoleBuffer_AddChar(), and ARC_String_RemoveSection().

◆ ARC_String_RemoveSection()

void ARC_String_RemoveSection ( ARC_String ** newString,
ARC_String * original,
uint64_t removeIndex,
uint64_t removeLength )

copy a subtring from a givin ARC_String

Parameters
newStringnew string without specified section, will be NULL on error
originalstring to remove section from
removeIndexstarting index in relation on original of what is to be removed
removeLengthlength of section that is being removed

Definition at line 499 of file string.c.

499 {
500 if(removeIndex == 0 && removeIndex + removeLength >= original->length){
501 ARC_String_Copy(newString, original);
502 return;
503 }
504
505 if(removeIndex == 0){
506 ARC_String_CopySubstring(newString, original, removeLength, original->length - removeLength);
507 return;
508 }
509
510 if(removeIndex + removeLength >= original->length){
511 ARC_String_CopySubstring(newString, original, 0, removeIndex);
512 return;
513 }
514
515 ARC_String *first, *second;
516 ARC_String_CopySubstring(&first , original, 0 , removeIndex );
517 ARC_String_CopySubstring(&second, original, removeIndex + removeLength, original->length - (removeIndex + removeLength));
518
519 ARC_String_Merge(newString, first, second);
520
521 ARC_String_Destroy(first );
522 ARC_String_Destroy(second);
523}
void ARC_String_Merge(ARC_String **combined, ARC_String *first, ARC_String *second)
merges two strings together
Definition string.c:485
void ARC_String_CopySubstring(ARC_String **substring, ARC_String *original, uint64_t start, uint64_t length)
copy a subtring from a givin ARC_String
Definition string.c:72
void ARC_String_Destroy(ARC_String *string)
destroys ARC_String type
Definition string.c:52

References ARC_String_Copy(), ARC_String_CopySubstring(), ARC_String_Destroy(), ARC_String_Merge(), and ARC_String::length.

Referenced by ARC_Config_StripComment(), and ARC_String_RemoveSubstring().

◆ ARC_String_RemoveSubstring()

void ARC_String_RemoveSubstring ( ARC_String ** newString,
ARC_String * original,
ARC_String * substring )

copy a subtring from a givin ARC_String

Parameters
newStringstring that doesn't have substring in it, will be null on error
originalstring to remove substring from
substringsubstring to remove

Definition at line 116 of file string.c.

116 {
117 uint64_t index = ARC_String_Find(original, substring);
118 if(arc_errno){
119 newString = NULL;
120 return;
121 }
122
123 ARC_String_RemoveSection(newString, original, index, original->length);
124}
uint64_t ARC_String_Find(ARC_String *string, ARC_String *substring)
takes a given string, and assigns index and length for position of first matching substring
Definition string.c:231
void ARC_String_RemoveSection(ARC_String **newString, ARC_String *original, uint64_t removeIndex, uint64_t removeLength)
copy a subtring from a givin ARC_String
Definition string.c:499

References arc_errno, ARC_String_Find(), ARC_String_RemoveSection(), and ARC_String::length.

Referenced by ARC_Config_RemoveAndRunCommands().

◆ ARC_String_ReplaceMatching()

void ARC_String_ReplaceMatching ( ARC_String ** string,
ARC_String * pattern,
ARC_String * replacement )

replaces characters in string matching the given pattern

Note
this uses ARC_String_CopyReplaceMatching, so debug logs will be thrown in that function not this one
Parameters
stringthe string that will be modified, will discard changes and set arc_errno on fail
patternthe pattern to replace in the string on match
replacementthe string that will replace the matched pattern

Definition at line 525 of file string.c.

525 {
526 ARC_String *copyReplaced;
527 ARC_String_CopyReplaceMatching(&copyReplaced, *string, pattern, replacement);
528
529 if(arc_errno != 0 || copyReplaced == NULL){
530 return;
531 }
532
533 ARC_String_Destroy(*string);
534 *string = copyReplaced;
535}
void ARC_String_CopyReplaceMatching(ARC_String **newString, ARC_String *original, ARC_String *pattern, ARC_String *replacement)
replaces characters in a copy of a string matching the given pattern
Definition string.c:537

References arc_errno, ARC_String_CopyReplaceMatching(), and ARC_String_Destroy().

◆ ARC_String_ReplaceMatchingCString()

void ARC_String_ReplaceMatchingCString ( ARC_String ** string,
char * patternCString,
uint64_t patternLength,
char * replacementCString,
uint64_t replacementLength )

replaces characters in string matching the given pattern

Parameters
stringthe string that will be modified, will discard changes and set arc_errno on fail
patternCStringthe cstring pattern to replace in the string on match
patternLengththe lenght of the cstring pattern
replacementCstringthe cstring that will replace the matched pattern
replacementLengththe length of the cstring replacement

Definition at line 580 of file string.c.

580 {
581 //TODO: probs want to check if the replacement goes over a uint64_t size
582 if(*string == NULL || patternCString == NULL || replacementCString == NULL){
584 ARC_DEBUG_LOG_ERROR("ARC_String_ReplaceMatchingCString(string, patternCString, patternLength, replacementCString, replacementLength), *string, patternCString, or replacementCString was null");
585 return;
586 }
587
588 ARC_String *original = *string;
589
590 uint64_t numberOfMatches = 0;
591 for(uint64_t originalIndex = 0; originalIndex < original->length; originalIndex++){
592 if(ARC_String_SubstringEqualsCString(original, originalIndex, patternCString, patternLength)){
593 numberOfMatches++;
594 originalIndex += patternLength - 1;
595 }
596 }
597
598 //no matches were found, but that isn't an error, so nothing to do, return
599 if(numberOfMatches == 0){
600 return;
601 }
602
603 (*string) = (ARC_String *)malloc(sizeof(ARC_String));
604 (*string)->length = original->length + (replacementLength - patternLength);
605 (*string)->data = (char *)malloc(sizeof(char *) * original->length + 1);
606
607 for(uint64_t originalIndex = 0, newIndex = 0; originalIndex < original->length; originalIndex++, newIndex++){
608 if(ARC_String_SubstringEqualsCString(original, originalIndex, patternCString, patternLength)){
609 for(uint64_t replacementIndex = 0; replacementIndex < replacementLength; replacementIndex++){
610 (*string)->data[newIndex + replacementIndex] = replacementCString[replacementIndex];
611 }
612
613 originalIndex += patternLength - 1;
614 newIndex += replacementLength - 1;
615 continue;
616 }
617
618 (*string)->data[newIndex] = original->data[originalIndex];
619 }
620
621 (*string)->data[(*string)->length] = '\0';
622
623 //cleanup
624 ARC_String_Destroy(original);
625}
ARC_Bool ARC_String_SubstringEqualsCString(ARC_String *string, uint64_t offset, const char *cstring, uint64_t length)
check if ARC_String and cstring match
Definition string.c:179

References ARC_DEBUG_LOG_ERROR, arc_errno, ARC_ERRNO_NULL, ARC_String_Destroy(), ARC_String_SubstringEqualsCString(), ARC_String::data, and ARC_String::length.

Referenced by ARC_String_ReplaceMatchingCStringWithStrlen().

◆ ARC_String_ReplaceMatchingCStringWithStrlen()

void ARC_String_ReplaceMatchingCStringWithStrlen ( ARC_String ** string,
char * patternCString,
char * replacement )

replaces characters in string matching the given pattern

Note
this uses ARC_String_ReplaceMatchingCString, so debug logs will be thrown in that function not this one
Parameters
stringthe string that will be modified, will discard changes and set arc_errno on fail
patternCStringthe cstring pattern to replace in the string on match
replacementCstringthe cstring that will replace the matched pattern

Definition at line 627 of file string.c.

627 {
628 ARC_String_ReplaceMatchingCString(string, patternCString, strlen(patternCString), replacementCString, strlen(replacementCString));
629}
void ARC_String_ReplaceMatchingCString(ARC_String **string, char *patternCString, uint64_t patternLength, char *replacementCString, uint64_t replacementLength)
replaces characters in string matching the given pattern
Definition string.c:580

References ARC_String_ReplaceMatchingCString().

◆ ARC_String_ReplaceWithSubstring()

void ARC_String_ReplaceWithSubstring ( ARC_String ** string,
uint64_t start,
uint64_t length )

replaces a string with a section of itself

Note
this uses ARC_String_CopySubstring so errors logs will be thrown in that function not this one
Parameters
stringthe string to replace, will not change on error
startthe starting index of the substring
lengththe length of the substring

Definition at line 100 of file string.c.

100 {
101 ARC_String *substring;
102 ARC_String_CopySubstring(&substring, *string, start, length);
103
104 //if error or substring is null free memory and return
105 if(arc_errno || substring == NULL){
106 if(substring != NULL){
107 ARC_String_Destroy(substring);
108 }
109 return;
110 }
111
112 ARC_String_Destroy(*string);
113 *string = substring;
114}

References arc_errno, ARC_String_CopySubstring(), and ARC_String_Destroy().

Referenced by ARC_Lexer_LexString().

◆ ARC_String_StripEnds()

void ARC_String_StripEnds ( ARC_String ** stripped,
ARC_String * original,
char charToStrip )

strips the ends based on a given char

Parameters
strippedwhere to store the string which has witespace stripped will be null if there is an error
originalthe string which whill have the matching char stripped from
charToStripthe char that will be stripped from the ends

Definition at line 323 of file string.c.

323 {
324 if(!original){
326 *stripped = NULL;
327 return;
328 }
329
330 if(!original->length){
332 *stripped = NULL;
333 return;
334 }
335
336 uint64_t length = original->length - 1;
337 for(; length; length--){
338 if(strncmp(original->data + (length - 1), &charToStrip, 1)){
339 break;
340 }
341 }
342
343 if(!length){
345 *stripped = NULL;
346 return;
347 }
348
349 uint64_t start = 0;
350 for(; start <= length; start++){
351 if(strncmp(original->data + start, &charToStrip, 1)){
352 break;
353 }
354 }
355
356 if(start == length){
358 *stripped = NULL;
359 return;
360 }
361
362 length -= start;
363 ARC_String_Create(stripped, original->data + start, length);
364}

References arc_errno, ARC_ERRNO_DATA, ARC_ERRNO_NULL, ARC_String_Create(), ARC_String::data, and ARC_String::length.

◆ ARC_String_StripEndsWhitespace()

void ARC_String_StripEndsWhitespace ( ARC_String ** stripped,
ARC_String * original )

strips the whitespace from the ends of a string

Parameters
strippedwhere to store the string which has witespace stripped from the ends will be null if there is an error
originalthe string which whill have the whitespace stripped from its ends

Definition at line 437 of file string.c.

437 {
438 uint64_t index;
439 for(uint64_t i = 0; i < original->length; i++){
440 if(original->data[i] == ' '){
441 continue;
442 }
443
444 if(original->data[i] == '\n'){
445 continue;
446 }
447
448 if(original->data[i] == '\t'){
449 continue;
450 }
451
452 if(original->data[i] == '\r'){
453 continue;
454 }
455
456 index = i;
457 break;
458 }
459
460 uint64_t endIndex;
461 for(uint64_t i = original->length;; i--){
462 if(original->data[i - 1] == ' '){
463 continue;
464 }
465
466 if(original->data[i - 1] == '\n'){
467 continue;
468 }
469
470 if(original->data[i - 1] == '\t'){
471 continue;
472 }
473
474 if(original->data[i - 1] == '\r'){
475 continue;
476 }
477
478 endIndex = i;
479 break;
480 }
481
482 ARC_String_CopySubstring(stripped, original, index, endIndex - index);
483}

References ARC_String_CopySubstring(), ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_FileIO(), ARC_Config_GetNameAndValue(), ARC_Config_Recurse(), ARC_Config_SetKeyGroup(), ARC_Rect_Read(), ARC_RectArray_Read(), ARC_RectArray_ReadRect(), ARC_SDL_Texture_Read(), ARC_Sprite_Delete(), ARC_Sprite_Read(), ARC_Spritesheet_Delete(), ARC_Spritesheet_Read(), and ARC_Spritesheet_ReadTexture().

◆ ARC_String_StripWhitespace()

void ARC_String_StripWhitespace ( ARC_String ** stripped,
ARC_String * original )

strips whitespace from a ARC_String

Parameters
strippedwhere to store the string which has witespace stripped will be null if there is an error
originalthe string which whill have whitespace stripped from

Definition at line 366 of file string.c.

366 {
367 if(!original){
369 *stripped = NULL;
370 return;
371 }
372
373 if(!original->length){
375 *stripped = NULL;
376 return;
377 }
378
379 uint64_t length = 0;
380 for(uint64_t i = 0; i < original->length; i++){
381 if(original->data[i] == ' '){
382 continue;
383 }
384
385 if(original->data[i] == '\n'){
386 continue;
387 }
388
389 if(original->data[i] == '\t'){
390 continue;
391 }
392
393 if(original->data[i] == '\r'){
394 continue;
395 }
396
397 length++;
398 }
399
400 if(!length){
402 *stripped = NULL;
403 return;
404 }
405
406 length++;
407 char data[length];
408 for(uint32_t i = 0; i < length; i++){
409 data[i] = 0;
410 }
411
412 uint64_t start = 0;
413 for(uint64_t i = 0; i < length; i++){
414 if(original->data[i] == ' '){
415 continue;
416 }
417
418 if(original->data[i] == '\n'){
419 continue;
420 }
421
422 if(original->data[i] == '\t'){
423 continue;
424 }
425
426 if(original->data[i] == '\r'){
427 continue;
428 }
429
430 data[start] = original->data[i];
431 start++;
432 }
433
434 ARC_String_Create(stripped, data, length);
435}

References arc_errno, ARC_ERRNO_DATA, ARC_ERRNO_NULL, ARC_String_Create(), ARC_String::data, and ARC_String::length.

Referenced by ARC_Config_RunCommand().

◆ ARC_String_SubstringEquals()

ARC_Bool ARC_String_SubstringEquals ( ARC_String * first,
uint64_t offset,
ARC_String * second )

check if substring of first equals second string

Parameters
firststring to check against second
offsetpostion based on first to start comparing against second
secondstring to check against first
Returns
ARC_True if match, ARC_False if they don't match

Definition at line 175 of file string.c.

175 {
176 return ARC_String_SubstringEqualsCString(first, offset, second->data, second->length);
177}

References ARC_String_SubstringEqualsCString(), ARC_String::data, and ARC_String::length.

Referenced by ARC_Lexer_AutomataMatchStringFn(), and ARC_String_CopyReplaceMatching().

◆ ARC_String_SubstringEqualsCString()

ARC_Bool ARC_String_SubstringEqualsCString ( ARC_String * string,
uint64_t offset,
const char * cstring,
uint64_t length )

check if ARC_String and cstring match

Parameters
stringARC_string to check
offsetpostion based on string to start comparing against cstring
cstringcstring to check
lengthlength of cstring
Returns
ARC_True if match, ARC_False if they don't match

Definition at line 179 of file string.c.

179 {
180 if(string->length - offset < length){
181 return ARC_False;
182 }
183
184 if(strncmp(string->data + offset, cstring, length)){
185 return ARC_False;
186 }
187
188 return ARC_True;
189}

References ARC_False, ARC_True, ARC_String::data, and ARC_String::length.

Referenced by ARC_String_ReplaceMatchingCString(), and ARC_String_SubstringEquals().

◆ ARC_String_ToDouble()

double ARC_String_ToDouble ( ARC_String * string)

converst substring from string to double

Parameters
stringstring to convert to double
Returns
double converted number

Definition at line 226 of file string.c.

226 {
227 return strtod(string->data, NULL);
228}

References ARC_String::data.

◆ ARC_String_ToInt64_t()

int64_t ARC_String_ToInt64_t ( ARC_String * string)

converst substring from string to int64_t

Parameters
stringstring to convert to int64_t
Returns
int64_t converted number

Definition at line 222 of file string.c.

222 {
223 return (int64_t) strtol(string->data, NULL, 10);
224}

References ARC_String::data.

Referenced by ARC_Point_Read(), ARC_Rect_Read(), and TEST_ParserCSV_CastTypeFn().

◆ ARC_String_ToUint64_t()

uint64_t ARC_String_ToUint64_t ( ARC_String * string)

converst substring from string to uint64_t

Parameters
stringstring to convert to uint64_t
Returns
uint64_t converted number

Definition at line 218 of file string.c.

218 {
219 return (uint64_t) strtoul(string->data, NULL, 10);
220}

References ARC_String::data.