Files
Operator-system/string_utils.c

85 lines
1.8 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* string_utils.c Wide-character (CHAR16) string helpers.
*
* Provides basic string operations used throughout the kernel,
* particularly by the command parser in commands.c.
*/
#include "string_utils.h"
/* ----------------------------------------------------------------
* Character classification / conversion
* ---------------------------------------------------------------- */
BOOLEAN is_space16(CHAR16 Ch)
{
return (Ch == L' ' || Ch == L'\t');
}
CHAR16 ascii_lower16(CHAR16 Ch)
{
if (Ch >= L'A' && Ch <= L'Z') {
return (CHAR16)(Ch + 32);
}
return Ch;
}
/* ----------------------------------------------------------------
* String comparison
* ---------------------------------------------------------------- */
BOOLEAN ascii_streq_ci(const CHAR16 *A, const CHAR16 *B)
{
if (A == NULL || B == NULL) {
return FALSE;
}
while (*A != L'\0' && *B != L'\0') {
if (ascii_lower16(*A) != ascii_lower16(*B)) {
return FALSE;
}
A++;
B++;
}
return (*A == L'\0' && *B == L'\0');
}
/* ----------------------------------------------------------------
* In-place trimming
* ---------------------------------------------------------------- */
void trim_spaces_inplace(CHAR16 *Cmd)
{
UINTN start = 0;
UINTN end = 0;
UINTN i = 0;
if (Cmd == NULL) {
return;
}
/* Find first non-space character */
while (Cmd[start] != L'\0' && is_space16(Cmd[start])) {
start++;
}
/* Find end of string */
end = start;
while (Cmd[end] != L'\0') {
end++;
}
/* Trim trailing spaces */
while (end > start && is_space16(Cmd[end - 1])) {
end--;
}
/* Shift trimmed content to the beginning */
while (i < (end - start)) {
Cmd[i] = Cmd[start + i];
i++;
}
Cmd[i] = L'\0';
}