From a164afcd564a498a9445cc072a205aea1e0f0b5e Mon Sep 17 00:00:00 2001 From: inspectredc Date: Wed, 4 Mar 2026 13:16:10 +0000 Subject: [PATCH 1/2] match string --- include/uv_string.h | 5 +- src/kernel/string.c | 225 ++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 211 insertions(+), 19 deletions(-) diff --git a/include/uv_string.h b/include/uv_string.h index 04e331f4..2b3add94 100644 --- a/include/uv_string.h +++ b/include/uv_string.h @@ -6,7 +6,10 @@ char* uvStrchr(const char* s, const char c); u32 uvStrlen(const char* s); int uvStrcmp(const char* s1, const char* s2); +void strFormatFloat(f32 number, s32 *bufPosPtr, char* strBuf); +void strFormatInt(s32 number, u16 base, s32 *bufPosPtr, char* strBuf, s32 padCount, s32 leftJustify, s32 hasZeroPadding); +char strDigitToChar(u16 digit); void uvSprintf(char* dest, const char* fmt, ...); -int uvAtoi(const char*); +int uvAtoi(const char* s); #endif // UV_STRING_H diff --git a/src/kernel/string.c b/src/kernel/string.c index 8d2da0cf..dd3780b7 100644 --- a/src/kernel/string.c +++ b/src/kernel/string.c @@ -3,10 +3,6 @@ #include #include -// forward declarations -void strFormatFloat(f32, s32*, char*, s32); -void strFormatInt(s32, s32 base, s32*, char*, s32, s32, s32); - char* uvStrchr(const char* s, const char c) { while (*s != '\0' && c != *s) { s++; @@ -17,17 +13,197 @@ char* uvStrchr(const char* s, const char c) { return NULL; } -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/uvStrlen.s") +u32 uvStrlen(const char *s) { + s32 len; + + len = 0; + while (*s++ != 0) { + len++; + } + return len; +} + +int uvStrcmp(const char *s1, const char *s2) { + s32 len1; + s32 len2; + s32 i; + + len1 = uvStrlen(s1); + len2 = uvStrlen(s2); + + if (len1 < len2) { + return -1; + } + if (len2 < len1) { + return 1; + } + + for (i = 0; i < len1; i++) { + if (*s1 < *s2) { + return -1; + } + if (*s1 > *s2) { + return 1; + } + + s1++; + s2++; + } + + return 0; +} + +void strFormatInt(s32 number, u16 base, s32 *bufPosPtr, char* strBuf, s32 padCount, s32 leftJustify, s32 hasZeroPadding) { + u16 digit; + u16 numDigits; + s32 outputLength; + s32 bufPos; + u16 isNegative; + s32 i; + s32 digitsRemaining; + s32 inputLengthRemaining; + s32 outputLengthRemaining; + + bufPos = *bufPosPtr; + if (number < 0) { + isNegative = TRUE; + number = -number; + } else { + isNegative = FALSE; + } + numDigits = 0; + i = 1; + if (number != 0) { + while (number >= i) { + numDigits++; + i *= base; + } + } else { + numDigits = 1; + } + + if (padCount > 0) { + outputLength = padCount; + } else { + outputLength = numDigits + isNegative; + } + bufPos += outputLength; + bufPos--; + outputLengthRemaining = outputLength; + digitsRemaining = numDigits; + inputLengthRemaining = numDigits + isNegative; + if (leftJustify) { + for (i = 0; i < outputLength; i++) { + if (inputLengthRemaining < outputLengthRemaining) { + strBuf[bufPos--] = ' '; + outputLengthRemaining--; + } else if (digitsRemaining > 0) { + digit = number % base; + strBuf[bufPos--] = strDigitToChar(digit); + number = (number - digit) / base; + digitsRemaining--; + outputLengthRemaining--; + inputLengthRemaining--; + } else if (inputLengthRemaining == 1) { + strBuf[bufPos--] = ' '; + } + } + } else { + for (i = 0; i < outputLength; i++) { + if (digitsRemaining > 0) { + digit = number % base; + strBuf[bufPos--] = strDigitToChar(digit); + number = (number - digit) / base; + } else { + if (hasZeroPadding) { + if ((i == outputLength - 1) && isNegative) { + strBuf[bufPos--] = '-'; + } else { + strBuf[bufPos--] = '0'; + } + } else { + if ((digitsRemaining == 0) && isNegative) { + strBuf[bufPos--] = '-'; + } else { + strBuf[bufPos--] = ' '; + } + } + + } + digitsRemaining--; + } + } + bufPos += outputLength; + bufPos++; + *bufPosPtr = bufPos; +} + +void strFormatFloat(f32 number, s32 *bufPosPtr, char* strBuf) { + f32 decimalPart; + u16 digit; + s32 i; + s32 integerPart; + u16 isNegative; + u16 numDigits; + s32 bufPos; -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/uvStrcmp.s") + bufPos = *bufPosPtr; + if (number < 0.0f) { + isNegative = TRUE; + number = -number; + } else { + isNegative = FALSE; + } + integerPart = (s32) number; + numDigits = 0; + i = 1; + if (integerPart != 0) { + while (integerPart >= i) { + numDigits++; + i *= 10; + } + } else { + numDigits = 1; + } + bufPos += numDigits + isNegative; + strBuf[bufPos++] = '.'; + decimalPart = number - integerPart; -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/strFormatInt.s") + for (i = 0; i < 2; i++) { + decimalPart *= 10.0f; + digit = (s32)decimalPart; + decimalPart -= digit; + strBuf[bufPos++] = strDigitToChar(digit); + } + bufPos -= 4; -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/strFormatFloat.s") + for (i = 0; i < numDigits; i++) { + digit = integerPart % 10; + strBuf[bufPos--] = strDigitToChar(digit); + integerPart = (integerPart - digit) / 10; + } + if (isNegative) { + strBuf[bufPos--] = '-'; + } + bufPos += numDigits + isNegative + 4; + *bufPosPtr = bufPos; +} + +char strDigitToChar(u16 digit) { + + if (digit < 10) { + return '0' + digit; + } + return ' '; +} -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/strDigitToChar.s") +s32 strCharToDigit(char digitChar) { -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/strCharToDigit.s") + if ((digitChar >= '0') && (digitChar <= '9')) { + return digitChar - '0'; + } + return 0; +} void uvSprintf(char* dest, const char* fmt, ...) { s32 srcBufPos; @@ -37,7 +213,7 @@ void uvSprintf(char* dest, const char* fmt, ...) { char* argStr; s32 arg; s32 padCount; - int hasPadding; + int hasZeroPadding; int leftJustify; u8 c; int parseSpecifier; @@ -53,7 +229,7 @@ void uvSprintf(char* dest, const char* fmt, ...) { if (c == '%') { padCount = -1; leftJustify = FALSE; - hasPadding = FALSE; + hasZeroPadding = FALSE; parseSpecifier = TRUE; } else { dest[destBufPos] = c; @@ -71,7 +247,7 @@ void uvSprintf(char* dest, const char* fmt, ...) { if (c == '0') { srcBufPos++; c = fmt[srcBufPos]; - hasPadding = TRUE; + hasZeroPadding = TRUE; } if (c == '\0') { break; @@ -107,18 +283,18 @@ void uvSprintf(char* dest, const char* fmt, ...) { } if (c == 'x') { arg = va_arg(args, s32); - strFormatInt(arg, 16, &destBufPos, dest, padCount, leftJustify, hasPadding); + strFormatInt(arg, 16, &destBufPos, dest, padCount, leftJustify, hasZeroPadding); parseSpecifier = FALSE; } else if (c == 'd') { arg = va_arg(args, s32); - strFormatInt(arg, 10, &destBufPos, dest, padCount, leftJustify, hasPadding); + strFormatInt(arg, 10, &destBufPos, dest, padCount, leftJustify, hasZeroPadding); parseSpecifier = FALSE; } else if (c == 'b') { arg = va_arg(args, s32); - strFormatInt(arg, 2, &destBufPos, dest, padCount, leftJustify, hasPadding); + strFormatInt(arg, 2, &destBufPos, dest, padCount, leftJustify, hasZeroPadding); parseSpecifier = FALSE; } else if (c == 'f') { - strFormatFloat(va_arg(args, f64), &destBufPos, dest, c); + strFormatFloat(va_arg(args, f64), &destBufPos, dest); parseSpecifier = FALSE; } else if (c == 's') { argStr = arg = va_arg(args, char*); @@ -136,4 +312,17 @@ void uvSprintf(char* dest, const char* fmt, ...) { va_end(args); } -#pragma GLOBAL_ASM("asm/nonmatchings/kernel/string/uvAtoi.s") +int uvAtoi(const char *s) { + s32 len; + s32 i; + s32 number; + + len = uvStrlen(s); + number = 0; + + for (i = 0; i < len; i++) { + number *= 10; + number += strCharToDigit(s[i]); + } + return number; +} From ed421f3a5a6e20b19c5c928c53f9bebd252d2156 Mon Sep 17 00:00:00 2001 From: inspectredc Date: Wed, 4 Mar 2026 13:17:32 +0000 Subject: [PATCH 2/2] format --- src/kernel/string.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/kernel/string.c b/src/kernel/string.c index dd3780b7..6b867dc4 100644 --- a/src/kernel/string.c +++ b/src/kernel/string.c @@ -13,7 +13,7 @@ char* uvStrchr(const char* s, const char c) { return NULL; } -u32 uvStrlen(const char *s) { +u32 uvStrlen(const char* s) { s32 len; len = 0; @@ -23,7 +23,7 @@ u32 uvStrlen(const char *s) { return len; } -int uvStrcmp(const char *s1, const char *s2) { +int uvStrcmp(const char* s1, const char* s2) { s32 len1; s32 len2; s32 i; @@ -53,7 +53,7 @@ int uvStrcmp(const char *s1, const char *s2) { return 0; } -void strFormatInt(s32 number, u16 base, s32 *bufPosPtr, char* strBuf, s32 padCount, s32 leftJustify, s32 hasZeroPadding) { +void strFormatInt(s32 number, u16 base, s32* bufPosPtr, char* strBuf, s32 padCount, s32 leftJustify, s32 hasZeroPadding) { u16 digit; u16 numDigits; s32 outputLength; @@ -128,7 +128,6 @@ void strFormatInt(s32 number, u16 base, s32 *bufPosPtr, char* strBuf, s32 padCou strBuf[bufPos--] = ' '; } } - } digitsRemaining--; } @@ -138,7 +137,7 @@ void strFormatInt(s32 number, u16 base, s32 *bufPosPtr, char* strBuf, s32 padCou *bufPosPtr = bufPos; } -void strFormatFloat(f32 number, s32 *bufPosPtr, char* strBuf) { +void strFormatFloat(f32 number, s32* bufPosPtr, char* strBuf) { f32 decimalPart; u16 digit; s32 i; @@ -154,7 +153,7 @@ void strFormatFloat(f32 number, s32 *bufPosPtr, char* strBuf) { } else { isNegative = FALSE; } - integerPart = (s32) number; + integerPart = (s32)number; numDigits = 0; i = 1; if (integerPart != 0) { @@ -190,7 +189,6 @@ void strFormatFloat(f32 number, s32 *bufPosPtr, char* strBuf) { } char strDigitToChar(u16 digit) { - if (digit < 10) { return '0' + digit; } @@ -198,7 +196,6 @@ char strDigitToChar(u16 digit) { } s32 strCharToDigit(char digitChar) { - if ((digitChar >= '0') && (digitChar <= '9')) { return digitChar - '0'; } @@ -312,7 +309,7 @@ void uvSprintf(char* dest, const char* fmt, ...) { va_end(args); } -int uvAtoi(const char *s) { +int uvAtoi(const char* s) { s32 len; s32 i; s32 number;