Pelles C forum

C language => Beginner questions => Topic started by: dimmed on June 28, 2025, 05:56:18 AM

Title: wcslen on MinGW-w64
Post by: dimmed on June 28, 2025, 05:56:18 AM
I'm the groggily user on Github. I tried to use a library named libutf in the past:

https://github.com/holepunchto/libutf/issues/1

https://github.com/holepunchto/libutf/issues/2

The developer was not easy to work with. In the end the thread closed without anything settled.

I still think I was right about wcslen on MinGW-w64.
Title: Re: wcslen on MinGW-w64
Post by: Vortex on June 28, 2025, 10:24:49 AM
Hi dimmed,

Could you be more specific as your question is not directly related to Pelles C ?
Title: Re: wcslen on MinGW-w64
Post by: TimoVJL on June 28, 2025, 12:27:48 PM
#include <stdio.h>
#include <wchar.h>

int main(void)
{
    const char* str = "\x41\x00\xa9\x03\x03\x26\x2d\x4e\x3d\xd8\x02\xde";
    printf("wcslen(str) = %zu\n", wcslen((wchar_t*)str));
    return 0;
}

x86
wcslen(str) = 15x64
wcslen(str) = 9
This version using -Ze gives 6 in both cases
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include <wchar.h>

#pragma comment(lib, "user32.lib")

int main(void)
{
    const char* str = "\x41\x00\xa9\x03\x03\x26\x2d\x4e\x3d\xd8\x02\xde";
    printf("wcslen(str) = %zu\n", wcslen((wchar_t*)str));
MessageBoxW(0, (wchar_t*)str, L"test", MB_OK);
    return 0;
}
Title: Re: wcslen on MinGW-w64
Post by: Pelle on June 28, 2025, 03:20:34 PM
When using:
wcslen((wchar_t*)str))
at the very least, make sure str is a properly terminated wide string (ignoring all other problems with this approach for now).

Append one (or two) '\0' to str, like so:
const char* str = "\x41\x00\xa9\x03\x03\x26\x2d\x4e\x3d\xd8\x02\xde\0";
Title: Re: wcslen on MinGW-w64
Post by: Vortex on June 29, 2025, 01:03:34 PM
Hi dimmed,

Any chance to learn why specifically you selected this string?