2
0
Эх сурвалжийг харах

removed strtou, use utf8 compatible function instead

Nicolas Cannasse 9 жил өмнө
parent
commit
4567f66946
9 өөрчлөгдсөн 29 нэмэгдсэн , 36 устгасан
  1. 1 1
      src/code.c
  2. 4 5
      src/hl.h
  3. 1 1
      src/jit.c
  4. 1 1
      src/module.c
  5. 2 2
      src/std/buffer.c
  6. 1 1
      src/std/bytes.c
  7. 1 1
      src/std/date.c
  8. 18 13
      src/std/string.c
  9. 0 11
      src/std/ucs2.c

+ 1 - 1
src/code.c

@@ -141,7 +141,7 @@ static const uchar *hl_get_ustring( hl_reader *r ) {
 	if( str == NULL ) {
 		int size = hl_utf8_length((vbyte*)r->code->strings[i],0);
 		str = hl_malloc(&r->code->alloc,(size+1)<<1);
-		strtou(str,size+1,r->code->strings[i]);
+		hl_from_utf8(str,size+1,r->code->strings[i]);
 		r->code->ustrings[i] = str;
 	}
 	return str;

+ 4 - 5
src/hl.h

@@ -155,7 +155,6 @@ 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 strtou(out,size,str) mbstowcs(out,str,size)	
 #	define utostr(out,size,str) wcstombs(out,str,size)	
 #else
 #	include <stdarg.h>
@@ -167,7 +166,6 @@ HL_API uchar *ustrdup( const uchar *str );
 HL_API double utod( const uchar *str, uchar **end );
 HL_API int utoi( const uchar *str, uchar **end );
 HL_API int ucmp( const uchar *a, const uchar *b );
-HL_API int strtou( uchar *out, int out_size, const char *str );
 HL_API int utostr( char *out, int out_size, const uchar *str );
 HL_API int usprintf( uchar *out, int out_size, const uchar *fmt, ... );
 HL_API int uvsprintf( uchar *out, const uchar *fmt, va_list arglist );
@@ -426,9 +424,10 @@ HL_API vdynamic *hl_alloc_obj( hl_type *t );
 HL_API vvirtual *hl_alloc_virtual( hl_type *t );
 HL_API vdynobj *hl_alloc_dynobj();
 HL_API vbyte *hl_alloc_bytes( int size );
-HL_API vbyte *hl_copy_bytes( vbyte *byte, int size );
-HL_API int hl_utf8_length( vbyte *s, int pos );
-HL_API char *hl_to_utf8( uchar *bytes );
+HL_API vbyte *hl_copy_bytes( const vbyte *byte, int size );
+HL_API int hl_utf8_length( const vbyte *s, int pos );
+HL_API int hl_from_utf8( uchar *out, int outLen, const char *str );
+HL_API char *hl_to_utf8( const uchar *bytes );
 HL_API vdynamic *hl_virtual_make_value( vvirtual *v );
 
 HL_API int hl_hash( vbyte *name );

+ 1 - 1
src/jit.c

@@ -2298,7 +2298,7 @@ int hl_jit_function( jit_ctx *ctx, hl_module *m, hl_function *f ) {
 					int i = o->p2;
 					int size = hl_utf8_length((vbyte*)m->code->strings[i],0);
 					str = hl_malloc(&m->code->alloc,(size+1)<<1);
-					strtou(str,size+1,m->code->strings[i]);
+					hl_from_utf8(str,size+1,m->code->strings[i]);
 					m->code->ustrings[i] = str;
 				}
 				op64(ctx,MOV,alloc_cpu(ctx, dst, false),pconst64(&p,(int_val)str));

+ 1 - 1
src/module.c

@@ -43,7 +43,7 @@ static uchar *module_resolve_symbol( void *addr, uchar *out, int *outSize ) {
 		return NULL;
 	file = debug_addr[0];
 	line = debug_addr[1];
-	*outSize = strtou(out,*outSize,cur_module->code->debugfiles[file]);
+	*outSize = hl_from_utf8(out,*outSize,cur_module->code->debugfiles[file]);
 	*outSize += usprintf(out + *outSize, size - *outSize, USTR(" line %d"), line);
 	return out;
 }

+ 2 - 2
src/std/buffer.c

@@ -85,9 +85,9 @@ HL_PRIM void hl_buffer_str( hl_buffer *b, const uchar *s ) {
 
 HL_PRIM void hl_buffer_cstr( hl_buffer *b, const char *s ) {
 	if( s ) {
-		int len = (int)strlen(s);
+		int len = (int)hl_utf8_length((vbyte*)s,0);
 		uchar *out = (uchar*)malloc(sizeof(uchar)*(len+1));
-		strtou(out,len,s);
+		hl_from_utf8(out,len,s);
 		hl_buffer_str_sub(b,out,len);
 		free(out);
 	} else hl_buffer_str_sub(b,USTR("NULL"),4);

+ 1 - 1
src/std/bytes.c

@@ -25,7 +25,7 @@ HL_PRIM vbyte *hl_alloc_bytes( int size ) {
 	return (vbyte*)hl_gc_alloc_noptr(size);
 }
 
-HL_PRIM vbyte *hl_copy_bytes( vbyte *ptr, int size ) {
+HL_PRIM vbyte *hl_copy_bytes( const vbyte *ptr, int size ) {
 	vbyte *b = hl_alloc_bytes(size);
 	memcpy(b,ptr,size);
 	return b;

+ 1 - 1
src/std/date.c

@@ -54,7 +54,7 @@ HL_PRIM vbyte *hl_date_to_string( int date, int *len ) {
 		hl_error("invalid date");
 	size = (int)strftime(buf,127,"%Y-%m-%d %H:%M:%S",&t);
 	out = (uchar*)hl_gc_alloc_noptr((size + 1) << 1);
-	strtou(out,size,buf);
+	hl_from_utf8(out,size,buf);
 	out[size] = 0;
 	*len = size;
 	return (vbyte*)out;

+ 18 - 13
src/std/string.c

@@ -72,7 +72,7 @@ HL_PRIM int hl_ucs2length( vbyte *str, int pos ) {
 	return (int)ustrlen((uchar*)(str + pos));
 }
 
-HL_PRIM int hl_utf8_length( vbyte *s, int pos ) {
+HL_PRIM int hl_utf8_length( const vbyte *s, int pos ) {
 	int len = 0;
 	s += pos;
 	while( true ) {
@@ -102,16 +102,13 @@ HL_PRIM int hl_utf8_length( vbyte *s, int pos ) {
 	return len;
 }
 
-HL_PRIM vbyte* hl_utf8_to_utf16( vbyte *str, int pos, int *size ) {
-	int ulen = hl_utf8_length(str, pos);
-	uchar *s = (uchar*)hl_gc_alloc_noptr((ulen + 1)*sizeof(uchar));
-	uchar *cur = s;
+HL_PRIM int hl_from_utf8( uchar *out, int outLen, const char *str ) {
 	int p = 0;
 	unsigned int c, c2, c3;
-	str += pos;
-	while( p++ < ulen ) {
-		c = (unsigned)*str++;
+	while( p++ < outLen ) {
+		c = *(unsigned char *)str++;
 		if( c < 0x80 ) {
+			if( c == 0 ) break;
 			// nothing
 		} else if( c < 0xE0 ) {
 			c = ((c & 0x3F) << 6) | ((*str++)&0x7F);
@@ -123,13 +120,21 @@ HL_PRIM vbyte* hl_utf8_to_utf16( vbyte *str, int pos, int *size ) {
 			c3 = (unsigned)*str++;
 			c = ((c & 0x0F) << 18) | ((c2 & 0x7F) << 12) | ((c3 & 0x7F) << 6) | ((*str++) & 0x7F);
 			// surrogate pair
-			*cur++ = (uchar)((c >> 10) + 0xD7C0);
-			*cur++ = (uchar)((c & 0x3FF) | 0xDC00);
+			if( p++ == outLen ) break;
+			*out++ = (uchar)((c >> 10) + 0xD7C0);
+			*out++ = (uchar)((c & 0x3FF) | 0xDC00);
 			continue;
 		}
-		*cur++ = (uchar)c;
+		*out++ = (uchar)c;
 	}
-	*cur = 0;
+	*out = 0;
+	return --p;
+}
+
+HL_PRIM vbyte* hl_utf8_to_utf16( vbyte *str, int pos, int *size ) {
+	int ulen = hl_utf8_length(str, pos);
+	uchar *s = (uchar*)hl_gc_alloc_noptr((ulen + 1)*sizeof(uchar));
+	hl_from_utf8(s,ulen+1,(char*)(str+pos));
 	*size = ulen << 1;
 	return (vbyte*)s;
 }
@@ -220,7 +225,7 @@ HL_PRIM vbyte *hl_utf16_to_utf8( vbyte *str, int pos, int *size ) {
 	return out;
 }
 
-HL_PRIM char *hl_to_utf8( uchar *bytes ) {
+HL_PRIM char *hl_to_utf8( const uchar *bytes ) {
 	int size;
 	return (char*)hl_utf16_to_utf8((vbyte*)bytes, 0, &size);
 }

+ 0 - 11
src/std/ucs2.c

@@ -83,17 +83,6 @@ int ucmp( const uchar *a, const uchar *b ) {
 	}
 }
 
-int strtou( uchar *out, int out_size, const char *str ) {
-	uchar *start = out;
-	if( out_size <= 0 ) return -1;
-	while( out_size-- ) {
-		if( *str == 0 ) break;
-		*out++ = *str++;
-	}
-	*out = 0;
-	return (int)(out - start);
-}
-
 int uvsprintf( uchar *out, const uchar *fmt, va_list arglist ) {
 	uchar *start = out;
 	char cfmt[20];