Added string comparison and measurement utilities and did some formatting

This commit is contained in:
Eric-Paul Ickhorn 2023-12-02 15:57:43 +01:00
parent 98a33df03c
commit 602cc942b0
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
4 changed files with 79 additions and 7 deletions

View File

@ -1,10 +1,9 @@
#ifndef RR_RUNES_H #ifndef RR_MEMORY_H
#define RR_RUNES_H #define RR_MEMORY_H
#include <librr/types.h> #include <librr/types.h>
/// @brief Tests if two memory regions overlap partially or completely. /// @brief Tests if two memory regions overlap partially or completely.
/// @param block1 Block to check for overlapping with block2. /// @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. // 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); usz_t rr_memrep(void *destination, usz_t num_bytes, void *sequence, usz_t len_sequence);
#endif // LIBRR_RUNES_H #endif // RR_MEMORY_H

View File

@ -0,0 +1,34 @@
#ifndef RR_STRING_UTILITY_H
#define RR_STRING_UTILITY_H
#include <librr/types.h>
/// @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

View File

@ -1,6 +1,6 @@
#ifndef RR_UTILITY_H #ifndef RR_TYPES_H
#define RR_UTILITY_H #define RR_TYPES_H
typedef signed char i8_t; typedef signed char i8_t;
typedef signed short i16_t; typedef signed short i16_t;
@ -33,4 +33,4 @@ typedef i64_t isz_t;
#define NULL ((void *) 0) #define NULL ((void *) 0)
#define ZERO ((rune_t) 0) #define ZERO ((rune_t) 0)
#endif // RR_UTILITY_H #endif // RR_TYPES_H

39
core/src-c/strutil.c Normal file
View File

@ -0,0 +1,39 @@
#include <librr/strutil.h>
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;
}