strutil cleanup & fixes

This commit is contained in:
Luke Street 2025-09-26 12:01:15 -06:00
parent b27ca28c20
commit f83d228cc1
7 changed files with 161 additions and 132 deletions

View File

@ -1,3 +1,5 @@
#pragma once
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#include <cstdio> #include <cstdio>

View File

@ -1,3 +1,5 @@
#pragma once
#include <filesystem> #include <filesystem>
#include <string> #include <string>

View File

@ -1,4 +1,6 @@
#include <stdlib.h> #pragma once
#include <cstdlib>
namespace handles { namespace handles {
enum Type { enum Type {

View File

@ -1,3 +1,5 @@
#pragma once
#include <cstdint> #include <cstdint>
#include <sched.h> #include <sched.h>

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <cstdint>
namespace wibo { namespace wibo {
@ -11,4 +12,4 @@ bool resourceEntryBelongsToExecutable(const Executable &exe, const ImageResource
ResourceIdentifier resourceIdentifierFromAnsi(const char *id); ResourceIdentifier resourceIdentifierFromAnsi(const char *id);
ResourceIdentifier resourceIdentifierFromWide(const uint16_t *id); ResourceIdentifier resourceIdentifierFromWide(const uint16_t *id);
} } // namespace wibo

View File

@ -1,14 +1,13 @@
#include "strutil.h"
#include "common.h" #include "common.h"
#include "strings.h"
#include <cstdint> #include <cstdint>
#include <vector>
#include <string>
#include <locale>
#include <codecvt>
#include <sstream> #include <sstream>
#include <string>
#include <vector>
size_t wstrlen(const uint16_t *str) { size_t wstrlen(const uint16_t *str) {
if(!str) return 0; if (!str)
return 0;
size_t len = 0; size_t len = 0;
while (str[len] != 0) while (str[len] != 0)
@ -17,8 +16,10 @@ size_t wstrlen(const uint16_t *str) {
} }
size_t wstrnlen(const uint16_t *str, size_t numberOfElements) { size_t wstrnlen(const uint16_t *str, size_t numberOfElements) {
if (!str)
return 0;
size_t len = 0; size_t len = 0;
while (str[len] != 0 && len < numberOfElements) while (len < numberOfElements && str[len] != 0)
++len; ++len;
return len; return len;
} }
@ -37,7 +38,8 @@ int wstrncmp(const uint16_t *string1, const uint16_t *string2, size_t count){
} }
const uint16_t *wstrstr(const uint16_t *dest, const uint16_t *src) { const uint16_t *wstrstr(const uint16_t *dest, const uint16_t *src) {
if (!*src) return dest; if (!*src)
return dest;
for (; *dest != 0; dest++) { for (; *dest != 0; dest++) {
const uint16_t *d = dest; const uint16_t *d = dest;
@ -57,6 +59,8 @@ const uint16_t* wstrstr(const uint16_t *dest, const uint16_t *src){
} }
uint16_t *wstrchr(const uint16_t *str, uint16_t c) { uint16_t *wstrchr(const uint16_t *str, uint16_t c) {
if (!str)
return nullptr;
for (; *str != 0; str++) { for (; *str != 0; str++) {
if (*str == c) { if (*str == c) {
return (uint16_t *)str; return (uint16_t *)str;
@ -70,25 +74,33 @@ uint16_t* wstrchr(const uint16_t* str, uint16_t c) {
} }
uint16_t *wstrrchr(const uint16_t *str, uint16_t c) { uint16_t *wstrrchr(const uint16_t *str, uint16_t c) {
if (!str)
return nullptr;
const uint16_t *last = nullptr; const uint16_t *last = nullptr;
for (; *str != 0; str++) { const uint16_t *it = str;
if (*str == c) { for (; *it != 0; ++it) {
last = str; if (*it == c) {
last = it;
} }
} }
if (c == 0)
return (uint16_t *)it;
return (uint16_t *)last; return (uint16_t *)last;
} }
uint16_t *wstrcat(uint16_t *dest, const uint16_t *src) { uint16_t *wstrcat(uint16_t *dest, const uint16_t *src) {
uint16_t *d = dest; uint16_t *d = dest;
while (*d) d++; while (*d)
while ((*d++ = *src++) != 0); d++;
while ((*d++ = *src++) != 0)
;
return dest; return dest;
} }
uint16_t *wstrncat(uint16_t *dest, const uint16_t *src, size_t count) { uint16_t *wstrncat(uint16_t *dest, const uint16_t *src, size_t count) {
uint16_t *d = dest; uint16_t *d = dest;
while (*d) d++; while (*d)
d++;
for (size_t i = 0; i < count && src[i] != 0; i++) { for (size_t i = 0; i < count && src[i] != 0; i++) {
*d++ = src[i]; *d++ = src[i];
} }
@ -98,72 +110,78 @@ uint16_t* wstrncat(uint16_t* dest, const uint16_t* src, size_t count){
uint16_t *wstrcpy(uint16_t *dest, const uint16_t *src) { uint16_t *wstrcpy(uint16_t *dest, const uint16_t *src) {
uint16_t *d = dest; uint16_t *d = dest;
while ((*d++ = *src++) != 0); while ((*d++ = *src++) != 0)
;
return dest; return dest;
} }
size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n) { size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n) {
if (!dst || !src || n == 0)
return 0;
size_t i = 0; size_t i = 0;
while (i < n && src[i] != 0) { for (; i < n && src[i] != 0; ++i) {
dst[i] = src[i]; dst[i] = src[i];
++i;
} }
if (i < n) for (size_t j = i; j < n; ++j) {
dst[i] = 0; dst[j] = 0;
}
return i; return i;
} }
std::string wideStringToString(const uint16_t *src, int len = -1) { std::string wideStringToString(const uint16_t *src, int len) {
if(!src) return std::string(); if (!src)
if (len < 0) { return {};
len = src ? wstrlen(src) : 0;
size_t count;
if (len >= 0) {
count = static_cast<size_t>(len);
} else {
count = wstrlen(src);
} }
// std::u16string u16str; #ifndef NDEBUG
// for(const uint16_t* p = src; *p != 0; p++){ std::stringstream hexDump;
// u16str.push_back(*p); hexDump << std::hex;
// } bool sawWide = false;
#endif
// std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::string result(count, '\0');
// return convert.to_bytes(reinterpret_cast<const char16_t*>(u16str.data())); for (size_t i = 0; i < count; ++i) {
uint16_t value = src[i];
if (i > 0)
hexDump << ' ';
hexDump << "0x" << value;
if (value > 0xFF)
sawWide = true;
result[i] = static_cast<char>(value & 0xFF);
}
// the old implementation #ifndef NDEBUG
std::string res(len, '\0'); if (sawWide) {
std::string debug_wstr; size_t loggedLength = (len >= 0) ? count : wstrlen(src);
std::stringstream ss; DEBUG_LOG("wideString (%zu): %s\n", loggedLength, hexDump.str().c_str());
bool is_wide = false;
for (int i = 0; i < len; i++) {
ss << "0x" << std::hex << src[i] << " ";
// debug_wstr += std::format("0x%X ", src[i]);
if(src[i] > 255){
// DEBUG_LOG("Encountered wide char with value 0x%X!\n", src[i]);
// assert(src[i] <= 255);
is_wide = true;
} }
res[i] = src[i] & 0xFF; #endif
}
if(is_wide){ return result;
debug_wstr += ss.str();
DEBUG_LOG("wideString (%d): %s\n", wstrlen(src), debug_wstr.c_str());
}
return res;
} }
std::vector<uint16_t> stringToWideString(const char *src) { std::vector<uint16_t> stringToWideString(const char *src) {
int len = strlen(src); if (!src)
return std::vector<uint16_t>{0};
size_t len = strlen(src);
std::vector<uint16_t> res(len + 1); std::vector<uint16_t> res(len + 1);
for (size_t i = 0; i < res.size(); i++) { for (size_t i = 0; i < res.size(); i++) {
res[i] = src[i] & 0xFF; res[i] = static_cast<uint16_t>(src[i] & 0xFF);
} }
res[len] = 0; // NUL terminate res[len] = 0; // NUL terminate
return res; return res;
} }
long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base) { long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base) {
if (!string) { if (!string) {
if(end_ptr) *end_ptr = nullptr; if (end_ptr)
*end_ptr = nullptr;
return 0; return 0;
} }
@ -175,8 +193,7 @@ long wstrtol(const uint16_t* string, uint16_t** end_ptr, int base){
if (normal_end && *normal_end) { if (normal_end && *normal_end) {
size_t offset = normal_end - normal_str.c_str(); size_t offset = normal_end - normal_str.c_str();
*end_ptr = (uint16_t *)(string + offset); *end_ptr = (uint16_t *)(string + offset);
} } else {
else {
*end_ptr = (uint16_t *)(string + normal_str.size()); *end_ptr = (uint16_t *)(string + normal_str.size());
} }
} }
@ -186,7 +203,8 @@ long wstrtol(const uint16_t* string, uint16_t** end_ptr, int base){
unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base) { unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base) {
if (!string) { if (!string) {
if(end_ptr) *end_ptr = nullptr; if (end_ptr)
*end_ptr = nullptr;
return 0; return 0;
} }
@ -198,8 +216,7 @@ unsigned long wstrtoul(const uint16_t* string, uint16_t** end_ptr, int base){
if (normal_end && *normal_end) { if (normal_end && *normal_end) {
size_t offset = normal_end - normal_str.c_str(); size_t offset = normal_end - normal_str.c_str();
*end_ptr = (uint16_t *)(string + offset); *end_ptr = (uint16_t *)(string + offset);
} } else {
else {
*end_ptr = (uint16_t *)(string + normal_str.size()); *end_ptr = (uint16_t *)(string + normal_str.size());
} }
} }

View File

@ -1,3 +1,6 @@
#pragma once
#include <cstdint>
#include <string> #include <string>
#include <vector> #include <vector>
@ -14,4 +17,4 @@ size_t wstrncpy(uint16_t *dst, const uint16_t *src, size_t n);
std::string wideStringToString(const uint16_t *src, int len = -1); std::string wideStringToString(const uint16_t *src, int len = -1);
std::vector<uint16_t> stringToWideString(const char *src); std::vector<uint16_t> stringToWideString(const char *src);
long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base); long wstrtol(const uint16_t *string, uint16_t **end_ptr, int base);
unsigned long wstrtoul(const uint16_t* strSource, uint16_t** end_ptr, int base); unsigned long wstrtoul(const uint16_t *string, uint16_t **end_ptr, int base);