Browse Source

Added some more string functions for creating strings that can contain NULLs.

Mark Sibly 9 years ago
parent
commit
abed8f00e8
2 changed files with 62 additions and 1 deletions
  1. 58 1
      modules/monkey/native/bbstring.cpp
  2. 4 0
      modules/monkey/native/bbstring.h

+ 58 - 1
modules/monkey/native/bbstring.cpp

@@ -129,7 +129,7 @@ bbString bbString::fromUtf8String( const void *data,int size ){
 				if( p==e || (p[0] & 0xc0)!=0x80 ) break;
 				if( p==e || (p[0] & 0xc0)!=0x80 ) break;
 				p+=1;
 				p+=1;
 			}else if( (c & 0xf0)==0xe0 ){
 			}else if( (c & 0xf0)==0xe0 ){
-				if( p+1==e || (p[0] & 0xc0)!=0x80 || (p[1] & 0xc0)!=0x80 ) break;
+				if( p==e || p+1==e || (p[0] & 0xc0)!=0x80 || (p[1] & 0xc0)!=0x80 ) break;
 				p+=2;
 				p+=2;
 			}else{
 			}else{
 				break;
 				break;
@@ -161,6 +161,63 @@ bbString bbString::fromUtf8String( const void *data,int size ){
 	return rep;
 	return rep;
 }
 }
 
 
+bbString bbString::fromAsciiData( const void *data,int size ){
+
+	if( !data || size<=0 ) return bbString();
+	
+	return bbString( (const char*)data,size );
+}
+
+bbString bbString::fromUtf8Data( const void *data,int size ){
+
+	if( !data || size<=0 ) return bbString();
+
+	const char *p=(const char*)data;
+	const char *e=p+size;
+
+	int len=0;
+		
+	while( p!=e ){
+		int c=*p++;
+		
+		if( c & 0x80 ){
+			if( (c & 0xe0)==0xc0 ){
+				if( p==e || (p[0] & 0xc0)!=0x80 ) break;
+				p+=1;
+			}else if( (c & 0xf0)==0xe0 ){
+				if( p==e || p+1==e || (p[0] & 0xc0)!=0x80 || (p[1] & 0xc0)!=0x80 ) break;
+				p+=2;
+			}else{
+				break;
+			}
+		}
+		len+=1;
+	}
+	
+	p=(const char*)data;
+	
+	Rep *rep=Rep::alloc( len );
+	bbChar *d=rep->data;
+	
+	while( len-- ){
+		int c=*p++;
+		
+		if( c & 0x80 ){
+			if( (c & 0xe0)==0xc0 ){
+				c=((c & 0x1f)<<6) | (p[0] & 0x3f);
+				p+=1;
+			}else if( (c & 0xf0)==0xe0 ){
+				c=((c & 0x0f)<<12) | ((p[0] & 0x3f)<<6) | (p[1] & 0x3f);
+				p+=2;
+			}
+		}
+		*d++=c;
+	}
+	
+	return rep;
+}
+
+
 bbArray<bbString> *bbString::split( bbString sep )const{
 bbArray<bbString> *bbString::split( bbString sep )const{
 
 
 	if( !sep.length() ){
 	if( !sep.length() ){

+ 4 - 0
modules/monkey/native/bbstring.h

@@ -450,6 +450,10 @@ class bbString{
 	
 	
 	static bbString fromUtf8String( const void *data,int size=-1 );
 	static bbString fromUtf8String( const void *data,int size=-1 );
 	
 	
+	static bbString fromAsciiData( const void *data,int size );
+
+	static bbString fromUtf8Data( const void *data,int size );
+	
 	static bbString fromTString( const void *data,int size=-1 ){
 	static bbString fromTString( const void *data,int size=-1 ){
 #if _WIN32
 #if _WIN32
 		return fromCString( data,size );
 		return fromCString( data,size );