restrcuct
This commit is contained in:
62
include/String.h
Normal file
62
include/String.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#define CUSTOM_STRING_MAX_LEN 64
|
||||
|
||||
typedef struct {
|
||||
char buffer[CUSTOM_STRING_MAX_LEN];
|
||||
int length; // current string length, excluding null terminator
|
||||
} String;
|
||||
|
||||
// Initialize String to empty
|
||||
void cs_init(String* cs)
|
||||
{
|
||||
cs->buffer[0] = '\0';
|
||||
cs->length = 0;
|
||||
}
|
||||
|
||||
// Set content from a C-string (truncates if needed)
|
||||
void cs_set(String* cs, const char* src)
|
||||
{
|
||||
int i = 0;
|
||||
while (src[i] != '\0' && i < (CUSTOM_STRING_MAX_LEN - 1))
|
||||
{
|
||||
cs->buffer[i] = src[i];
|
||||
i++;
|
||||
}
|
||||
cs->buffer[i] = '\0';
|
||||
cs->length = i;
|
||||
}
|
||||
|
||||
// Compare two Strings for equality
|
||||
int cs_equal(const String* a, const String* b)
|
||||
{
|
||||
if (a->length != b->length)
|
||||
return 0;
|
||||
for (int i = 0; i < a->length; i++)
|
||||
{
|
||||
if (a->buffer[i] != b->buffer[i])
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check if String starts with another String prefix
|
||||
int cs_starts_with(const String* cs, const String* prefix)
|
||||
{
|
||||
if (prefix->length > cs->length)
|
||||
return 0;
|
||||
for (int i = 0; i < prefix->length; i++)
|
||||
{
|
||||
if (cs->buffer[i] != prefix->buffer[i])
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Append a char to String (ignores if full)
|
||||
void cs_append_char(String* cs, char c)
|
||||
{
|
||||
if (cs->length < (CUSTOM_STRING_MAX_LEN - 1))
|
||||
{
|
||||
cs->buffer[cs->length++] = c;
|
||||
cs->buffer[cs->length] = '\0';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user