在 2025-12-31 02:29, Pali Rohár 写道:
Like we have helper function __mingw_filename_cp() for determing code page used by CRT library for filenames, add a new helper function named __mingw_isleadbyte_cp() to check if particular character in supplied code page is leading or not.Use WinAPI function IsDBCSLeadByteEx() for its implementation. When the IsDBCSLeadByteEx() function is not available then provide emulation code taken from mingw-w64-libraries/winstorecompat/src/IsDBCSLeadByteEx.c. API of the __mingw_isleadbyte_cp() function is similar to CRT function _isleadbyte_l(), first argment is character and second argument is code page. --- mingw-w64-crt/Makefile.am | 1 + mingw-w64-crt/misc/__mingw_isleadbyte_cp.c | 36 ++++++++++++++++++++++ mingw-w64-headers/crt/locale.h | 3 ++ 3 files changed, 40 insertions(+) create mode 100644 mingw-w64-crt/misc/__mingw_isleadbyte_cp.c diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am index ff032f953493..1364472c2ab1 100644 --- a/mingw-w64-crt/Makefile.am +++ b/mingw-w64-crt/Makefile.am @@ -1257,6 +1257,7 @@ src_libmingwex=\ misc/wmemset.c misc/mingw-access.c \ misc/ftw32.c misc/ftw32i64.c misc/ftw64.c misc/ftw64i32.c \ misc/__mingw_filename_cp.c \ + misc/__mingw_isleadbyte_cp.c \ \ ssp/chk_fail.c ssp/gets_chk.c ssp/memcpy_chk.c ssp/memmove_chk.c \ ssp/mempcpy_chk.c \ diff --git a/mingw-w64-crt/misc/__mingw_isleadbyte_cp.c b/mingw-w64-crt/misc/__mingw_isleadbyte_cp.c new file mode 100644 index 000000000000..54e645a646e4 --- /dev/null +++ b/mingw-w64-crt/misc/__mingw_isleadbyte_cp.c @@ -0,0 +1,36 @@ +/** + * This file has no copyright assigned and is placed in the Public Domain. + * This file is part of the mingw-w64 runtime package. + * No warranty is given; refer to the file DISCLAIMER.PD within this package. + */ + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include <windows.h> +#include <locale.h> + +static BOOL WINAPI fallback_IsDBCSLeadByteEx(UINT cp, BYTE c) +{ + int i; + CPINFO cp_info; + if (GetCPInfo(cp, &cp_info) && cp_info.MaxCharSize > 1) {
There's still an issue in this patch. The commit message says this code is taken from winstorecompat, but it isn't. In winstorecompat there's `lpCPInfo.MaxCharSize == 2` and this code is designed specifically for DBCS code pages (which makes sense, because of its name).
So this should probably be changed to `cp_info.MaxCharSize == 2`.
I also tested something like this:
```
#include <windows.h>
#include <stdio.h>
void
test_cp(UINT cp)
{
BYTE t[] = { 0xC3, 0xA8 }; // '猫'; cat (animal)
printf("IsDBCSLeadByteEx(%d, %.2x) => %d\n", cp, t[0],
IsDBCSLeadByteEx(cp, t[0]));
CPINFO cpinfo;
if(!GetCPInfo(cp, &cpinfo))
printf("CP %d unavailable: %ld\n", cp, GetLastError());
else {
printf("CP %d MaxCharSize = %d\n", cp, cpinfo.MaxCharSize);
for(BYTE* p = cpinfo.LeadByte; *p && (p < cpinfo.LeadByte +
MAX_LEADBYTES); p += 2)
printf(" LeadByte [%.2x,%.2x]%s\n", p[0], p[1],
(t[0] >= p[0]) && (t[0] <= p[1]) ? " (match)" : "");
}
printf("\n");
}
int
main(void)
{
test_cp(936); // GB2312
test_cp(20936); // GB2312-80
test_cp(54936); // GB 18030
}
```
which gives
```
IsDBCSLeadByteEx(936, c3) => 1
CP 936 MaxCharSize = 2
LeadByte [81,fe] (match)
IsDBCSLeadByteEx(20936, c3) => 1
CP 20936 MaxCharSize = 2
LeadByte [a1,a9]
LeadByte [b0,f7] (match)
IsDBCSLeadByteEx(54936, c3) => 0
CP 54936 MaxCharSize = 4
```
--
Best regards,
LIU Hao
OpenPGP_signature.asc
Description: OpenPGP digital signature
_______________________________________________ Mingw-w64-public mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/mingw-w64-public
