瀏覽代碼

Added bool->string.

Mark Sibly 7 年之前
父節點
當前提交
fa97e33235

+ 13 - 0
modules/monkey/native/bbstring.cpp

@@ -195,49 +195,62 @@ int bbString::utf8Length()const{
 	return n;
 }
 
+bbString::bbString( bool b ){
+
+	_rep=Rep::create( b ? "True" : "False" );
+}
+
 bbString::bbString( int n ){
+
 	char data[64];
 	sprintf( data,"%d",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( unsigned int n ){
+
 	char data[64];
 	sprintf( data,"%u",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( long n ){
+
 	char data[64];
 	sprintf( data,"%ld",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( unsigned long n ){
+
 	char data[64];
 	sprintf( data,"%lu",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( long long n ){
+
 	char data[64];
 	sprintf( data,"%lld",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( unsigned long long n ){
+
 	char data[64];
 	sprintf( data,"%llu",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( float n ){
+
 	char data[64];
 	sprintf( data,"%.9g",n );
 	_rep=Rep::create( data );
 }
 
 bbString::bbString( double n ){
+
 	char data[64];
 	sprintf( data,"%.17g",n );
 	_rep=Rep::create( data );

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

@@ -78,6 +78,8 @@ class bbString{
 	bbString( const NSString *str );
 #endif
 
+	explicit bbString( bool b );
+
 	explicit bbString( int n );
 	
 	explicit bbString( unsigned int n );

+ 1 - 1
modules/monkey/native/bbstring.mm

@@ -1,7 +1,7 @@
 
 #include "bbstring.h"
 
-bbString::bbString( const NSString *str ):_rep( Rep::create( str.UTF8String ) ){
+bbString::bbString( const NSString *str ):_rep( Rep::create( str ? str.UTF8String : "" ) ){
 }
 
 NSString *bbString::ToNSString()const{

+ 3 - 1
modules/monkey/newdocs/language/types.md

@@ -45,7 +45,7 @@ These type conversions are performed automatically:
 | String or array type 			| `Bool`
 | Class or interface type	 	| `Bool`
 | Any numeric type				| Any numeric type
-| Any numeric type				| `String`
+| Any numeric type or `Bool`	| `String`
 | Any pointer type				| `Void Ptr`
 | Any enum type					| Any integral type
 | Class or interface type		| Base class type or implemented interface type
@@ -61,6 +61,8 @@ When struct values are converted, the result will be true if the struct value is
 
 When floating point values are converted to integral values, the fractional part of the floating point value is simply chopped off - no rounding is performed.
 
+When Bools are converted to strings, the result will be either "True" or "False".
+
 
 @#### Explicit type conversions
 

+ 14 - 0
modules/std/misc/stringio.monkey2

@@ -119,23 +119,37 @@ End
 #rem monkeydoc Converts a ulong value to a binary string.
 #end
 Function Bin:String( value:ULong )
+	
 	Return ULongToString( value,2 )
 End
 
 #rem monkeydoc Converts a binary string to a ulong value.
 #end
 Function ParseBin:ULong( str:String )
+	
 	Return StringToULong( str,2 )
 End
 
 #rem monkeydoc Converts a ulong value to a hexadecimal string.
 #end
 Function Hex:String( value:ULong )
+	
 	Return ULongToString( value,16 )
 End
 
 #rem monkeydoc Converts a hexadecimal string to a ulong value.
 #end
 Function ParseHex:ULong( str:String )
+	
 	Return StringToULong( str,16 )
 End
+
+#rem monkeydoc Parse a boolean string.
+
+Returns true if `str` equals "True", ignoring case. Otherwise, returns false.
+
+#end
+Function ParseBool:Bool( str:String )
+	
+	Return str.ToLower()="true"
+End