Browse Source

Cleaned up strings a bit.

Mark Sibly 9 years ago
parent
commit
cb19d3c935

+ 46 - 17
modules/monkey/native/bbstring.cpp

@@ -25,7 +25,31 @@ int bbString::utf8Length()const{
 	return n;
 	return n;
 }
 }
 
 
-int bbString::toUtf8( void *buf,int size )const{
+void bbString::toCString( void *buf,int size )const{
+	if( size<=0 ) return;
+	
+	int sz=length();
+	if( sz>size ) sz=size;
+	
+	for( int i=0;i<sz;++i ) ((char*)buf)[i]=data()[i];
+	
+	if( sz<size ) ((char*)buf)[sz]=0;
+}
+
+void bbString::toWString( void *buf,int size )const{
+
+	size=size/sizeof(wchar_t);
+	if( size<=0 ) return;
+	
+	int sz=length();
+	if( sz>size ) sz=size;
+	
+	for( int i=0;i<sz;++i ) ((wchar_t*)buf)[i]=data()[i];
+	
+	if( sz<size ) ((wchar_t*)buf)[sz]=0;
+}
+
+void bbString::toUtf8String( void *buf,int size )const{
 
 
 	char *dst=(char*)buf;
 	char *dst=(char*)buf;
 	char *end=dst+size;
 	char *end=dst+size;
@@ -48,12 +72,7 @@ int bbString::toUtf8( void *buf,int size )const{
 			*dst++=0x80 | (c & 0x3f);
 			*dst++=0x80 | (c & 0x3f);
 		}
 		}
 	}
 	}
-	
-	int n=dst-(char*)buf;
-
 	if( dst<end ) *dst++=0;
 	if( dst<end ) *dst++=0;
-	
-	return n;
 }
 }
 
 
 const char *bbString::c_str()const{
 const char *bbString::c_str()const{
@@ -77,39 +96,49 @@ bbString bbString::fromChar( int chr ){
 	return buf;
 	return buf;
 }
 }
 
 
-bbString bbString::fromCString( const void *data ){
-	return (const char*)data;
+bbString bbString::fromCString( const void *data,int size ){
+	const char *p=(const char*)data;
+	int sz=strlen( p );
+	if( sz>size ) sz=size;
+	return bbString( p,sz );
 }
 }
 
 
-bbString bbString::fromWString( const void *data ){
-	return (const wchar_t*)data;
-}
-
-bbString bbString::fromUtf8String( const void *data ){
-	const char *p=(const char*)data;
-	return fromUtf8( p,strlen( p ) );
+bbString bbString::fromWString( const void *data,int size ){
+	size/=sizeof(wchar_t);
+	const wchar_t *p=(const wchar_t*)data;
+	const wchar_t *e=p;
+	while( e<p+size && *e ) ++e;
+	return bbString( p,e-p );
 }
 }
 
 
 bbString bbString::fromUtf8String( const void *data,int size ){
 bbString bbString::fromUtf8String( const void *data,int size ){
-
 	const char *p=(const char*)data;
 	const char *p=(const char*)data;
-	const char *e=p+size;
+	int sz=strlen( p );
+	if( sz>size ) sz=size;
+	const char *e=p+sz;
 	
 	
 	bbChar *tmp=(bbChar*)malloc( size*sizeof(bbChar) );
 	bbChar *tmp=(bbChar*)malloc( size*sizeof(bbChar) );
 	bbChar *put=tmp;
 	bbChar *put=tmp;
 	
 	
 	while( p<e ){
 	while( p<e ){
 		int c=*p++;
 		int c=*p++;
+		
 		if( c & 0x80 ){
 		if( c & 0x80 ){
+		
 			if( (c & 0xe0)==0xc0 ){
 			if( (c & 0xe0)==0xc0 ){
+			
 				if( p>=e || (p[0] & 0xc0)!=0x80 ) break;
 				if( p>=e || (p[0] & 0xc0)!=0x80 ) break;
 				c=((c & 0x1f)<<6) | (p[0] & 0x3f);
 				c=((c & 0x1f)<<6) | (p[0] & 0x3f);
 				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+1>=e || (p[0] & 0xc0)!=0x80 || (p[1] & 0xc0)!=0x80 ) break;
 				c=((c & 0x0f)<<12) | ((p[0] & 0x3f)<<6) | (p[1] & 0x3f);
 				c=((c & 0x0f)<<12) | ((p[0] & 0x3f)<<6) | (p[1] & 0x3f);
 				p+=2;
 				p+=2;
+				
 			}else{
 			}else{
+			
 				break;
 				break;
 			}
 			}
 		}
 		}

+ 47 - 41
modules/monkey/native/bbstring.h

@@ -411,33 +411,43 @@ class bbString{
 	
 	
 	int utf8Length()const;
 	int utf8Length()const;
 	
 	
-	int toUtf8( void *buf,int size )const;
+	void toCString( void *buf,int size )const;
+
+	void toWString( void *buf,int size )const;
 	
 	
-	inline bbCString toCString()const;
+	void toUtf8String( void *buf,int size )const;
 	
 	
 	static bbString fromChar( int chr );
 	static bbString fromChar( int chr );
 	
 	
-	static bbString fromCString( const void *data );
-	
-	static bbString fromWString( const void *data );
-	
-	static bbString fromUtf8String( const void *data );
+	static bbString fromCString( const void *data,int size );
 
 
+	static bbString fromWString( const void *data,int size );
+	
 	static bbString fromUtf8String( const void *data,int size );
 	static bbString fromUtf8String( const void *data,int size );
-
-	static bbString fromUtf8( const void *data,int size ){
-		return fromUtf8String( data,size );
-	}
-
 	
 	
-	static bbString fromTString( const void *data ){
+	static bbString fromTString( const void *data,int size ){
 #if _WIN32
 #if _WIN32
-		return fromCString( data );
+		return fromCString( data,size );
 #else
 #else
-		return fromUtf8String( data );
+		return fromUtf8String( data,size );
 #endif
 #endif
 	}
 	}
-
+	
+	static bbString fromCString( const void *data ){
+		return fromCString( data,0x7fffffff );
+	}
+	
+	static bbString fromWString( const void *data ){
+		return fromWString( data,0x7fffffff );
+	}
+	
+	static bbString fromUtf8String( const void *data ){
+		return fromUtf8String( data,0x7fffffff );
+	}
+	
+	static bbString fromTString( const void *data ){
+		return fromTString( data,0x7fffffff );
+	}
 };
 };
 
 
 class bbCString{
 class bbCString{
@@ -456,6 +466,10 @@ class bbCString{
 		free( _data );
 		free( _data );
 	}
 	}
 	
 	
+	operator bbString()const{
+		return _str;
+	}
+	
 	bbCString &operator=( const bbCString &str ){
 	bbCString &operator=( const bbCString &str ){
 		free( _data );
 		free( _data );
 		_data=nullptr;
 		_data=nullptr;
@@ -463,15 +477,11 @@ class bbCString{
 		return *this;
 		return *this;
 	}
 	}
 	
 	
-	operator bbString()const{
-		return _str;
-	}
-	
 	char *data()const{
 	char *data()const{
 		if( _data ) return _data;
 		if( _data ) return _data;
-		_data=(char*)malloc( _str.length()+1 );
-		for( int i=0;i<_str.length();++i ) _data[i]=_str.data()[i];
-		_data[_str.length()]=0;
+		int size=_str.length()+1;
+		_data=(char*)malloc( size );
+		_str.toCString( _data,size );
 		return _data;
 		return _data;
 	}
 	}
 	
 	
@@ -496,6 +506,10 @@ class bbWString{
 		free( _data );
 		free( _data );
 	}
 	}
 	
 	
+	operator bbString()const{
+		return _str;
+	}
+	
 	bbWString &operator=( const bbWString &str ){
 	bbWString &operator=( const bbWString &str ){
 		free( _data );
 		free( _data );
 		_data=nullptr;
 		_data=nullptr;
@@ -503,15 +517,11 @@ class bbWString{
 		return *this;
 		return *this;
 	}
 	}
 	
 	
-	operator bbString()const{
-		return _str;
-	}
-	
 	wchar_t *data()const{
 	wchar_t *data()const{
 		if( _data ) return _data;
 		if( _data ) return _data;
-		_data=(wchar_t*)malloc( (_str.length()+1)*sizeof( wchar_t ) );
-		for( int i=0;i<_str.length();++i ) _data[i]=_str.data()[i];
-		_data[_str.length()]=0;
+		int size=(_str.length()+1)*sizeof(wchar_t);
+		_data=(wchar_t*)malloc( size );
+		_str.toWString( _data,size );
 		return _data;
 		return _data;
 	}
 	}
 	
 	
@@ -549,19 +559,19 @@ class bbUtf8String{
 	
 	
 	unsigned char *data()const{
 	unsigned char *data()const{
 		if( _data ) return _data;
 		if( _data ) return _data;
-		int n=_str.utf8Length()+1;
-		_data=(unsigned char*)malloc( n );
-		_str.toUtf8( _data,n );
+		int size=_str.utf8Length()+1;
+		_data=(unsigned char*)malloc( size );
+		_str.toUtf8String( _data,size );
 		return _data;
 		return _data;
 	}
 	}
 	
 	
-	operator unsigned char*()const{
-		return data();
-	}
-	
 	operator char*()const{
 	operator char*()const{
 		return (char*)data();
 		return (char*)data();
 	}
 	}
+	
+	operator unsigned char*()const{
+		return data();
+	}
 };
 };
 
 
 #if _WIN32
 #if _WIN32
@@ -570,10 +580,6 @@ typedef bbCString bbTString;
 typedef bbUtf8String bbTString;
 typedef bbUtf8String bbTString;
 #endif
 #endif
 
 
-bbCString bbString::toCString()const{
-	return bbCString( *this );
-}
-
 template<class C> bbString operator+( const C *str,const bbString &str2 ){
 template<class C> bbString operator+( const C *str,const bbString &str2 ){
 	return bbString( str )+str2;
 	return bbString( str )+str2;
 }
 }

+ 93 - 6
modules/monkey/types.monkey2

@@ -205,19 +205,106 @@ Struct @String ="bbString"
 	
 	
 	Method Join:String( bits:String[] )="join"
 	Method Join:String( bits:String[] )="join"
 	
 	
-	Method ToUtf8:Int( buf:Void Ptr,size:Int )="toUtf8"
+	#rem monkeydoc Converts the string to a CString.
 	
 	
-	Method ToCString:CString()="bbString::toCString"
+	If there is enough room in the memory buffer, a null terminating '0' is appended to the CString.
 	
 	
+	@param buf Memory buffer to write the CString to.
+	
+	@param bufSize Size of the memory buffer in bytes.
+	
+	#end
+	Method ToCString( buf:Void Ptr )="toCString"
+
+	#rem monkeydoc Converts the string to a WString
+	
+	If there is enough room in the memory buffer, a null terminating '0' is appended to the WString.
+	
+	@param buf Memory buffer to write the WString to.
+	
+	@param bufSize Size of the memory buffer in bytes.
+	
+	#end
+	Method ToWString( buf:Void Ptr,bufSize:Int )="toWString"
+	
+	#rem monkeydoc Converts the string to a Utf8String
+	
+	If there is enough room in the memory buffer, a null terminating '0' is appended to the Utf8String.
+	
+	@param buf Memory buffer to write the Utf8String to.
+	
+	@param bufSize Size of the memory buffer in bytes.
+	
+	#end
+	Method ToUtf8String( buf:Void Ptr,bufSize:Int )="toUtf8String"
+	
+	#rem monkeydoc Creates a string containing a single character.
+	
+	@param chr The character.
+	
+	#end
 	Function FromChar:String( chr:Int )="bbString::fromChar"
 	Function FromChar:String( chr:Int )="bbString::fromChar"
 	
 	
-	Function FromCString:String( data:Void Ptr )="bbString::fromCString"
+	#rem monkeydoc Creates a string from a null terminated CString.
+	
+	Note: Only `bufSize` bytes at most will be used to create the string, so this method is safe to call even if the string is not null terminated.
+	
+	@param buf The memory buffer containing the CString.
+	
+	@param bufSize The size of the memory buffer in bytes.
+	
+	#end	
+	Function FromCString:String( buf:Void Ptr,bufSize:Int )="bbString::fromCString"
+	
+	#rem monkeydoc Creates a string from a null terminated WString.
 	
 	
-	Function FromWString:String( data:Void Ptr )="bbString::fromWString"
+	Note: Only `bufSize` bytes at most will be used to create the string, so this method is safe to call even if the string is not null terminated.
 	
 	
-	Function FromUtf8String:String( data:Void Ptr )="bbString::fromUtf8String"
+	@param buf The memory buffer containing the WString.
 	
 	
-	Function FromUtf8:String( data:Void Ptr,size:Int )="bbString::fromUtf8"
+	@param bufSize The size of the memory buffer in bytes.
+	
+	#end	
+	Function FromWString:String( buf:Void Ptr,bufSize:Int )="bbString::fromWString"
+
+	#rem monkeydoc Creates a string from a null terminated Utf8String.
+	
+	Note: Only `bufSize` bytes at most will be used to create the string, so this method is safe to call even if the string is not null terminated.
+	
+	@param buf The memory buffer containing the Utf8String.
+	
+	@param bufSize The size of the memory buffer in bytes.
+	
+	#end	
+	Function FromUtf8String:String( buf:Void Ptr,bufSize:Int )="bbString::fromUtf8String"
+	
+	
+	#rem monkeydoc Creates a string from a null terminated CString.
+	
+	Note: The CString must be correctly null terminated, or Bad Things Will Happen.
+	
+	@param buf The memory buffer containing the CString.
+	
+	#end	
+	Function FromCString:String( buf:Void Ptr )="bbString::fromCString"
+
+	#rem monkeydoc Creates a string from a null terminated WString.
+	
+	Note: The WString must be correctly null terminated, or Bad Things Will Happen.
+	
+	@param buf The memory buffer containing the WString.
+	
+	#end	
+	Function FromWString:String( buf:Void Ptr )="bbString::fromWString"
+
+	#rem monkeydoc Creates a string from a null terminated Utf8String.
+	
+	Note: The Utf8String must be correctly null terminated, or Bad Things Will Happen.
+	
+	@param buf The memory buffer containing the Utf8String.
+	
+	#end	
+	Function FromUtf8String:String( buf:Void Ptr )="bbString::fromUtf8String"
 	
 	
 End
 End
 
 

+ 3 - 3
modules/std/misc/stringio.monkey2

@@ -19,7 +19,7 @@ Function LoadString:String( path:String )
 	Local data:=DataBuffer.Load( path )
 	Local data:=DataBuffer.Load( path )
 	If Not data Return ""
 	If Not data Return ""
 
 
-	Local str:=String.FromUtf8( data.Data,data.Length )
+	Local str:=String.FromUtf8String( data.Data,data.Length )
 
 
 	data.Discard()
 	data.Discard()
 	
 	
@@ -37,9 +37,9 @@ End
 #end
 #end
 Function SaveString:Bool( str:String,path:String )
 Function SaveString:Bool( str:String,path:String )
 
 
-	Local data:=New DataBuffer( str.Utf8Length )
+	Local data:=New DataBuffer( str.Utf8Length+1 )
 
 
-	str.ToUtf8( data.Data,data.Length )
+	str.ToUtf8String( data.Data,data.Length )
 
 
 	Local ok:=data.Save( path )
 	Local ok:=data.Save( path )