Skip to content

Commit

Permalink
prefer uint16 over char16_t for portability
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamer-coding committed Jul 28, 2024
1 parent bc6bad1 commit cf51559
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 35 deletions.
12 changes: 2 additions & 10 deletions code/logic/fossil/tofu/tofu.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@
#include <stdint.h>
#include <wchar.h>

#if defined(__APPLE__)
#include <wchar.h> // Use wchar.h on macOS
typedef wchar_t char16_t;
typedef wchar_t char32_t;
#else
#include <uchar.h> // Use uchar.h on other systems
#endif

/**
In the realm of quantum physics, our understanding of space, time, reality, and the observable universe takes
on a fascinating and intricate character. Quantum physics delves into the fundamental nature of matter and
Expand Down Expand Up @@ -94,12 +86,12 @@ typedef union {
uint64_t uint_val; // for unsigned int types
double double_val; // for double types
float float_val; // for float types
char16_t *uchar_string_val; // for byte string types
uint16_t *uchar_string_val; // for byte string types
wchar_t *wchar_string_val; // for wide string types
char *cchar_string_val; // for C string type
char cchar_val; // for char types
wchar_t wchar_val; // for wide char types
char16_t uchar_val; // for byte types
uint16_t uchar_val; // for byte types
size_t size_val; // for size types
uint8_t bool_val; // for bool types
} fossil_tofu_value_t;
Expand Down
28 changes: 3 additions & 25 deletions code/logic/tofu.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,6 @@ wchar_t *custom_wcsdup(const wchar_t *src) {
return dup;
}

// Function to convert a multibyte string to a UTF-16 string
static char16_t *mbstoc16(const char *mbstr) {
size_t length = strlen(mbstr) + 1; // +1 for the null terminator
char16_t *utf16str = malloc(length * sizeof(char16_t));
if (!utf16str) {
return NULL; // Handle allocation failure
}

mbstate_t state = {0};
const char *src = mbstr;
char16_t *dest = utf16str;

size_t ret;
while ((ret = mbrtoc16(dest, src, MB_CUR_MAX, &state)) > 0) {
src += ret;
dest++;
}

*dest = u'\0'; // Null-terminate the UTF-16 string
return utf16str;
}

// Helper function to convert hexadecimal string to uint64_t
static uint64_t parse_hexadecimal(const char *value) {
uint64_t result;
Expand Down Expand Up @@ -117,7 +95,7 @@ fossil_tofu_t fossil_tofu_create(char *type, char *value) {
tofu.value.double_val = strtod(value, NULL);
break;
case FOSSIL_TOFU_TYPE_BSTR:
tofu.value.uchar_string_val = (char16_t *)fossil_tofu_alloc((strlen(value) + 1) * sizeof(char16_t));
tofu.value.uchar_string_val = (uint16_t *)fossil_tofu_alloc((strlen(value) + 1) * sizeof(uint16_t));
strcpy((char *)tofu.value.uchar_string_val, value);
break;
case FOSSIL_TOFU_TYPE_WSTR:
Expand All @@ -129,7 +107,7 @@ fossil_tofu_t fossil_tofu_create(char *type, char *value) {
tofu.value.cchar_string_val = fossil_tofu_strdup(value);
break;
case FOSSIL_TOFU_TYPE_BCHAR:
tofu.value.uchar_val = value[0];
tofu.value.uchar_val = (uint16_t)value[0];
break;
case FOSSIL_TOFU_TYPE_CCHAR:
tofu.value.cchar_val = value[0];
Expand Down Expand Up @@ -324,7 +302,7 @@ fossil_tofu_t fossil_tofu_copy(fossil_tofu_t tofu) {

switch (tofu.type) {
case FOSSIL_TOFU_TYPE_BSTR:
copy.value.uchar_string_val = mbstoc16((char*)tofu.value.uchar_string_val);
copy.value.uchar_string_val = (uint16_t *)fossil_tofu_strdup((char*)tofu.value.uchar_string_val);
break;
case FOSSIL_TOFU_TYPE_WSTR:
copy.value.wchar_string_val = custom_wcsdup(tofu.value.wchar_string_val);
Expand Down

0 comments on commit cf51559

Please sign in to comment.