Prechádzať zdrojové kódy

Added Format() methods.

woollybah 6 rokov pred
rodič
commit
d05508669c

+ 11 - 1
stringbuilder.mod/common.bmx

@@ -68,7 +68,17 @@ Extern
 	Function bmx_stringbuilder_toutf8string:Byte Ptr(buffer:Byte Ptr)
 	Function bmx_stringbuilder_towstring:Short Ptr(buffer:Byte Ptr)
 	Function bmx_stringbuilder_join_strings(buffer:Byte Ptr, bits:String[], joiner:String)
-	
+	Function bmx_stringbuilder_format_string(buffer:Byte Ptr, formatText:String, value:String)
+	Function bmx_stringbuilder_format_byte(buffer:Byte Ptr, formatText:String, value:Byte)
+	Function bmx_stringbuilder_format_short(buffer:Byte Ptr, formatText:String, value:Short)
+	Function bmx_stringbuilder_format_int(buffer:Byte Ptr, formatText:String, value:Int)
+	Function bmx_stringbuilder_format_uint(buffer:Byte Ptr, formatText:String, value:UInt)
+	Function bmx_stringbuilder_format_long(buffer:Byte Ptr, formatText:String, value:Long)
+	Function bmx_stringbuilder_format_ulong(buffer:Byte Ptr, formatText:String, value:ULong)
+	Function bmx_stringbuilder_format_sizet(buffer:Byte Ptr, formatText:String, value:Size_T)
+	Function bmx_stringbuilder_format_float(buffer:Byte Ptr, formatText:String, value:Float)
+	Function bmx_stringbuilder_format_double(buffer:Byte Ptr, formatText:String, value:Double)
+
 	Function bmx_stringbuilder_splitbuffer_length:Int(splitPtr:Byte Ptr)
 	Function bmx_stringbuilder_splitbuffer_text:String(splitPtr:Byte Ptr, index:Int)
 	Function bmx_stringbuilder_splitbuffer_free(splitPtr:Byte Ptr)

+ 134 - 0
stringbuilder.mod/glue.c

@@ -722,6 +722,140 @@ BBChar * bmx_stringbuilder_towstring(struct MaxStringBuilder * buf) {
 	return p;
 }
 
+void bmx_stringbuilder_toutf8_buffer(BBString *str, char * buf, size_t length) {
+	int i=0,len=str->length;
+	int out=0;
+	char *q=buf;
+	unsigned short *p=str->buf;
+	while (i < len && out < length) {
+		unsigned int c=*p++;
+		if(0xd800 <= c && c <= 0xdbff && i < len - 1) {
+			/* surrogate pair */
+			unsigned int c2 = *p;
+			if(0xdc00 <= c2 && c2 <= 0xdfff) {
+				/* valid second surrogate */
+				c = ((c - 0xd800) << 10) + (c2 - 0xdc00) + 0x10000;
+				++p;
+				++i;
+			}
+		}
+		if( c<0x80 ){
+			*q++=c;
+			out++;
+		}else if( c<0x800 ){
+			if (out > length - 2) {
+				break;
+			}
+			*q++=0xc0|(c>>6);
+			*q++=0x80|(c&0x3f);
+			out += 2;
+		}else if(c < 0x10000) { 
+			if (out > length - 3) {
+				break;
+			}
+			*q++=0xe0|(c>>12);
+			*q++=0x80|((c>>6)&0x3f);
+			*q++=0x80|(c&0x3f);
+			out += 3;
+		}else if(c <= 0x10ffff) {
+			if (out > length - 4) {
+				break;
+			}
+			*q++ = 0xf0|(c>>18);
+			*q++ = 0x80|((c>>12)&0x3f);
+			*q++ = 0x80|((c>>6)&0x3f);
+			*q++ = 0x80|((c&0x3f));
+			out += 4;
+		}else{
+			bbExThrowCString( "Unicode character out of UTF-8 range" );
+		}
+		++i;
+	}
+	*q=0;
+}
+
+void bmx_stringbuilder_format_string(struct MaxStringBuilder * buf, BBString * formatText, BBString * value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char vbuffer[2048];
+	bmx_stringbuilder_toutf8_buffer(value, vbuffer, sizeof(vbuffer));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, vbuffer);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_byte(struct MaxStringBuilder * buf, BBString * formatText, BBBYTE value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_short(struct MaxStringBuilder * buf, BBString * formatText, BBSHORT value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_int(struct MaxStringBuilder * buf, BBString * formatText, BBINT value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_uint(struct MaxStringBuilder * buf, BBString * formatText, BBUINT value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_long(struct MaxStringBuilder * buf, BBString * formatText, BBLONG value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_ulong(struct MaxStringBuilder * buf, BBString * formatText, BBULONG value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_sizet(struct MaxStringBuilder * buf, BBString * formatText, BBSIZET value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_float(struct MaxStringBuilder * buf, BBString * formatText, float value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
+void bmx_stringbuilder_format_double(struct MaxStringBuilder * buf, BBString * formatText, double value) {
+	char formatBuf[256];
+	bmx_stringbuilder_toutf8_buffer(formatText, formatBuf, sizeof(formatBuf));
+	char buffer[2048];
+	snprintf(buffer, sizeof(buffer), formatBuf, value);
+	bmx_stringbuilder_append_utf8string(buf, buffer);
+}
+
 /* ----------------------------------------------------- */
 
 int bmx_stringbuilder_splitbuffer_length(struct MaxSplitBuffer * buf) {

+ 175 - 2
stringbuilder.mod/stringbuilder.bmx

@@ -23,10 +23,12 @@ bbdoc: A string builder.
 End Rem	
 Module BRL.StringBuilder
 
-ModuleInfo "Version: 1.10"
+ModuleInfo "Version: 1.11"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: 2018-2019 Bruce A Henderson"
 
+ModuleInfo "History: 1.11"
+ModuleInfo "History: Added Format() methods."
 ModuleInfo "History: 1.10"
 ModuleInfo "History: Added JoinStrings() method."
 ModuleInfo "History: 1.09"
@@ -411,7 +413,178 @@ Public
 	Method FindLast:Int(subString:String, startIndex:Int = 0)
 		Return bmx_stringbuilder_findlast(buffer, subString, startIndex)
 	End Method
-	
+
+	Rem
+	bbdoc: Appends a #String value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:String)
+		bmx_stringbuilder_format_string(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Byte value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Byte)
+		bmx_stringbuilder_format_byte(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Short value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Short)
+		bmx_stringbuilder_format_short(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Int value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Int)
+		bmx_stringbuilder_format_int(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #UInt value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:UInt)
+		bmx_stringbuilder_format_uint(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Long value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Long)
+		bmx_stringbuilder_format_long(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #ULong value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:ULong)
+		bmx_stringbuilder_format_ulong(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Size_T value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Size_T)
+		bmx_stringbuilder_format_sizet(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Float value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Float)
+		bmx_stringbuilder_format_float(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Double value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method Format:TStringBuilder(formatText:String, value:Double)
+		bmx_stringbuilder_format_double(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Byte value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatByte:TStringBuilder(formatText:String, value:Byte)
+		bmx_stringbuilder_format_byte(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Short value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatShort:TStringBuilder(formatText:String, value:Short)
+		bmx_stringbuilder_format_short(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Int value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatInt:TStringBuilder(formatText:String, value:Int)
+		bmx_stringbuilder_format_int(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #UInt value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatUInt:TStringBuilder(formatText:String, value:UInt)
+		bmx_stringbuilder_format_uint(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Long value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatLong:TStringBuilder(formatText:String, value:Long)
+		bmx_stringbuilder_format_long(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #ULong value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatULong:TStringBuilder(formatText:String, value:ULong)
+		bmx_stringbuilder_format_ulong(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Size_T value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatSizeT:TStringBuilder(formatText:String, value:Size_T)
+		bmx_stringbuilder_format_sizet(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Float value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatFloat:TStringBuilder(formatText:String, value:Float)
+		bmx_stringbuilder_format_float(buffer, formatText, value)
+		Return Self
+	End Method
+
+	Rem
+	bbdoc: Appends a #Double value to the string builder using the specified printf style @formatText.
+	about: @formatText is limited to 256 character bytes. Formatted text is limited to 2048 character bytes.
+	End Rem
+	Method FormatDouble:TStringBuilder(formatText:String, value:Double)
+		bmx_stringbuilder_format_double(buffer, formatText, value)
+		Return Self
+	End Method
+
 	Rem
 	bbdoc: Extracts the leftmost characters from the string builder.
 	about: This method extracts the left @length characters from the builder. If this many characters are not available, the whole builder is returned.