Răsfoiți Sursa

fix in ucs2 to utf8 (close #40)

Nicolas Cannasse 8 ani în urmă
părinte
comite
c84511b3a1
2 a modificat fișierele cu 25 adăugiri și 7 ștergeri
  1. 5 2
      src/hl.h
  2. 20 5
      src/std/ucs2.c

+ 5 - 2
src/hl.h

@@ -148,7 +148,7 @@ typedef long long int64;
 #	include <wchar.h>
 typedef wchar_t	uchar;
 #	define USTR(str)	L##str
-#	define HL_NATIVE_WCHAR_FUN
+#	define HL_NATIVE_UCHAR_FUN
 #	define usprintf		swprintf
 #	define uprintf		wprintf
 #	define ustrlen		wcslen
@@ -157,12 +157,15 @@ typedef wchar_t	uchar;
 #	define utod(s,end)	wcstod(s,end)
 #	define utoi(s,end)	wcstol(s,end,10)
 #	define ucmp(a,b)	wcscmp(a,b)
-#	define utostr(out,size,str) wcstombs(out,str,size)	
+#	define utostr(out,size,str) wcstombs(out,str,size)
 #else
 #	include <stdarg.h>
 typedef unsigned short uchar;
 #	undef USTR
 #	define USTR(str)	u##str
+#endif
+
+#ifndef HL_NATIVE_UCHAR_FUN
 C_FUNCTION_BEGIN
 HL_API int ustrlen( const uchar *str );
 HL_API uchar *ustrdup( const uchar *str );

+ 20 - 5
src/std/ucs2.c

@@ -20,7 +20,7 @@
  * DEALINGS IN THE SOFTWARE.
  */
 #include <hl.h>
-#ifndef HL_NATIVE_WCHAR_FUN
+#ifndef HL_NATIVE_UCHAR_FUN
 #include <stdarg.h>
 
 int ustrlen( const uchar *str ) {
@@ -29,6 +29,21 @@ int ustrlen( const uchar *str ) {
 	return (int)(p - str);
 }
 
+int ustrlen_utf8( const uchar *str ) {
+	int size = 0;
+	while(1) {
+		uchar c = *str++;
+		if( c == 0 ) break;
+		if( c < 0x80 )
+			size++;
+		else if( c < 0x800 )
+			size += 2;
+		else
+			size += 3;
+	}
+	return size;
+}
+
 uchar *ustrdup( const uchar *str ) {
 	int len = ustrlen(str);
 	int size = (len + 1) << 1;
@@ -182,14 +197,14 @@ int utostr( char *out, int out_size, const uchar *str ) {
 		unsigned int c = *str++;
 		if( c == 0 ) break;
 		if( c < 0x80 )
-			*out++ = c;
+			*out++ = (char)c;
 		else if( c < 0x800 ) {
 			if( out + 2 > end ) break;
-			*out++ = 0xC0|(c>>6);
+			*out++ = (char)(0xC0|(c>>6));
 			*out++ = 0x80|(c&63);
 		} else {
 			if( out + 3 > end ) break;
-			*out++ = 0xE0|(c>>12);
+			*out++ = (char)(0xE0|(c>>12));
 			*out++ = 0x80|((c>>6)&63);
 			*out++ = 0x80|(c&63);
 		}
@@ -199,7 +214,7 @@ int utostr( char *out, int out_size, const uchar *str ) {
 }
 
 static char *utos( const uchar *s ) {
-	int len = ustrlen(s);
+	int len = ustrlen_utf8(s);
 	char *out = (char*)malloc(len + 1);
 	if( utostr(out,len+1,s) < 0 )
 		*out = 0;