diff --git a/core/exports/librr/memory.h b/core/exports/librr/memory.h index a105bb8..cb56160 100644 --- a/core/exports/librr/memory.h +++ b/core/exports/librr/memory.h @@ -1,10 +1,9 @@ -#ifndef RR_RUNES_H -#define RR_RUNES_H +#ifndef RR_MEMORY_H +#define RR_MEMORY_H #include - /// @brief Tests if two memory regions overlap partially or completely. /// @param block1 Block to check for overlapping with block2. // If the address of this block is higher than that of block2, the blocks will be swapped internally. @@ -38,4 +37,4 @@ void rr_memset(void *destination, usz_t count, u8_t value); usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_sequence); -#endif // LIBRR_RUNES_H +#endif // RR_MEMORY_H diff --git a/core/exports/librr/strutil.h b/core/exports/librr/strutil.h new file mode 100644 index 0000000..c45e5cb --- /dev/null +++ b/core/exports/librr/strutil.h @@ -0,0 +1,34 @@ + +#ifndef RR_STRING_UTILITY_H +#define RR_STRING_UTILITY_H + +#include + +/// @brief Gets the length of a null-terminated string. +/// @param string A null-terminated string of which to get the length. +/// @return The length of the given string. +usz_t rr_measure_string(const char *string); + +/// @brief Compares two strings completely until the end. +/// @param string1 First string to compare. +/// @param string2 Second string to compare. If there's a clear correct way for the +/// first string to be, this should be the correct string to be tested against. +/// @return Whether the two strings' contents are equal. +bool_t rr_strings_equal(const char *string1, const char *string2); + +/// @brief Compares two strings up to a maximum given length, returning TRUE if they end +/// before or if they are equal up to the maximum length. +/// @param string1 First string to compare. +/// @param string2 Second string to compare. If there's a clear correct way for the +/// first string to be, this should be the correct string to be tested against. +/// @param max_length How many bytes to test at most; it can be less nonetheless. +/// @return Whether the two strings' contents are equal up to the given length. +bool_t rr_strings_equal_up_to(const char *string1, const char *string2, usz_t max_length); + +/// @brief Tests if 'base' starts with 'prefix'. +/// @param base Base string to be tested for starting with 'prefix'. +/// @param prefix The prefix which 'base' should have. +/// @return Whether the string 'base' starts with the string 'prefix'. +bool_t rr_string_is_prefixed(const char *base, const char *prefix); + +#endif // RR_STRING_UTILITY_H diff --git a/core/exports/librr/types.h b/core/exports/librr/types.h index fd86780..481448c 100644 --- a/core/exports/librr/types.h +++ b/core/exports/librr/types.h @@ -1,6 +1,6 @@ -#ifndef RR_UTILITY_H -#define RR_UTILITY_H +#ifndef RR_TYPES_H +#define RR_TYPES_H typedef signed char i8_t; typedef signed short i16_t; @@ -33,4 +33,4 @@ typedef i64_t isz_t; #define NULL ((void *) 0) #define ZERO ((rune_t) 0) -#endif // RR_UTILITY_H \ No newline at end of file +#endif // RR_TYPES_H diff --git a/core/src-c/strutil.c b/core/src-c/strutil.c new file mode 100644 index 0000000..06be599 --- /dev/null +++ b/core/src-c/strutil.c @@ -0,0 +1,39 @@ +#include + +usz_t rr_measure_string(const char *string) +{ + usz_t index = 0; + while(string[index] != ZERO) + ++index; + return index; +} + +bool_t rr_strings_equal(const char *string1, const char *string2) +{ + usz_t index = 0; + while(string1[index] == string2[index]) + if(string1[index] == ZERO) return TRUE; + return FALSE; +} + +bool_t rr_strings_equal_up_to(const char *string1, const char *string2, usz_t max_length) +{ + usz_t index = 0; + while(string1[index] == string2[index]) + { + if(string1[index] == ZERO) return TRUE; + if(index >= max_length) return TRUE; + } + return FALSE; +} + +bool_t rr_string_is_prefixed(const char *base, const char *prefix) +{ + usz_t index = 0; + while(base[index] == prefix[index]) + { + if(prefix[index] == ZERO) return TRUE; + ++index; + } + return FALSE; +}