added get_utf16_length and to_utf16 functions.

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2022-06-14 16:12:50 +08:00
parent 8a490f203e
commit fe59a592c8

View File

@ -4,6 +4,17 @@
namespace hgl namespace hgl
{ {
int get_utf16_length(const CharSet &cs,const void *src,const int src_size)
{
const int src_str_size=(src_size==-1)?strlen((char *)src):src_size;
const int len=MultiByteToWideChar((UINT)cs.codepage,0,(char *)src,src_str_size,0,0);
if(len<=0)return(len);
return len;
}
int to_utf16(const CharSet &cs,u16char **dst,const void *src,const int src_size) int to_utf16(const CharSet &cs,u16char **dst,const void *src,const int src_size)
{ {
const int src_str_size=(src_size==-1)?strlen((char *)src):src_size; const int src_str_size=(src_size==-1)?strlen((char *)src):src_size;
@ -12,9 +23,26 @@ namespace hgl
if(len<=0)return(len); if(len<=0)return(len);
*dst=new u16char[len]; *dst=new u16char[len+1];
return MultiByteToWideChar((UINT)cs.codepage,0,(char *)src,src_str_size,*dst,len); MultiByteToWideChar((UINT)cs.codepage,0,(char *)src,src_str_size,*dst,len);
*dst[len]=0;
return len;
}
int to_utf16(const CharSet &cs,u16char *dst,const int dst_size,const void *src,const int src_size)
{
if(dst_size<=0)return dst_size;
const int src_str_size=(src_size==-1)?strlen((char *)src):src_size;
int len=MultiByteToWideChar((UINT)cs.codepage,0,(char *)src,src_str_size,dst,dst_size);
if(len<dst_size)
dst[len]=0;
return len;
} }
int to_utf8(const CharSet &cs,u8char **dst,const void *src,const int src_size) int to_utf8(const CharSet &cs,u8char **dst,const void *src,const int src_size)