Sfoglia il codice sorgente

Added LeftAlign() and RightAlign() methods.

woollybah 6 anni fa
parent
commit
e6171badca

+ 2 - 0
stringbuilder.mod/common.bmx

@@ -63,6 +63,8 @@ Extern
 	Function bmx_stringbuilder_left:String(buffer:Byte Ptr, length:Int)
 	Function bmx_stringbuilder_right:String(buffer:Byte Ptr, length:Int)
 	Function bmx_stringbuilder_compare:Int(buffer:Byte Ptr, buffer2:Byte Ptr)
+	Function bmx_stringbuilder_leftalign(buffer:Byte Ptr, length:Int)
+	Function bmx_stringbuilder_rightalign(buffer:Byte Ptr, length:Int)
 	
 	Function bmx_stringbuilder_splitbuffer_length:Int(splitPtr:Byte Ptr)
 	Function bmx_stringbuilder_splitbuffer_text:String(splitPtr:Byte Ptr, index:Int)

+ 45 - 0
stringbuilder.mod/glue.c

@@ -604,6 +604,51 @@ int bmx_stringbuilder_compare(struct MaxStringBuilder * buf1, struct MaxStringBu
 	return buf1->count - buf2->count;
 }
 
+void bmx_stringbuilder_leftalign(struct MaxStringBuilder * buf, int length) {
+	bmx_stringbuilder_resize(buf, length);
+	
+	if (length == buf->count) {
+		return;
+	} else if (length > buf->count) {
+		int c = length - buf->count;
+
+		BBChar * p = buf->buffer + buf->count;
+
+		for (int i=0; i < c; ++i) {
+			*p++ = (BBChar)' ';
+		}
+	}
+	
+	buf->count = length;
+}
+
+void bmx_stringbuilder_rightalign(struct MaxStringBuilder * buf, int length) {
+	bmx_stringbuilder_resize(buf, length);
+
+	if (length == buf->count) {
+		return;
+	} else if (length < buf->count) {
+		int offset = buf->count - length;
+		memmove(buf->buffer, buf->buffer + offset, buf->count * sizeof(BBChar));
+	} else {
+		int offset = length - buf->count;
+		
+		if (offset == 0) {
+			return;
+		}
+		
+		memmove(buf->buffer + offset, buf->buffer, buf->count * sizeof(BBChar));
+
+		BBChar * p = buf->buffer;
+
+		for (int i=0; i < offset; ++i) {
+			*p++ = (BBChar)' ';
+		}
+	}
+
+	buf->count = length;
+}
+
 /* ----------------------------------------------------- */
 
 int bmx_stringbuilder_splitbuffer_length(struct MaxSplitBuffer * buf) {

+ 23 - 1
stringbuilder.mod/stringbuilder.bmx

@@ -23,10 +23,12 @@ bbdoc: A string builder.
 End Rem	
 Module BRL.StringBuilder
 
-ModuleInfo "Version: 1.07"
+ModuleInfo "Version: 1.08"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "Copyright: 2018-2019 Bruce A Henderson"
 
+ModuleInfo "History: 1.08"
+ModuleInfo "History: Added LeftAlign() and RightAlign() methods."
 ModuleInfo "History: 1.07"
 ModuleInfo "History: Fixed AppendByte and AppendShort not using unsigned variants."
 ModuleInfo "History: 1.06"
@@ -498,6 +500,26 @@ Public
 		bmx_stringbuilder_toupper(buffer)
 		Return Self
 	End Method
+	
+	Rem
+	bbdoc: Left aligns the buffer, adjusted to the specified @length.
+	about: If buffer is longer than the specified length, the buffer is shortened to the specified length.
+	If the buffer is shorter than the specified length, spaces are added to the right end of the buffer to produce the appropriate length.
+	End Rem
+	Method LeftAlign:TStringBuilder(length:Int)
+		bmx_stringbuilder_leftalign(buffer, length)
+		Return Self
+	End Method
+	
+	Rem
+	bbdoc: Right aligns the buffer, adjusted to the specified @length.
+	about: If buffer is longer than the specified length, the buffer is shortened to the specified length.
+	If the buffer is shorter than the specified length, spaces are added to the left end of the buffer to produce the appropriate length.
+	End Rem
+	Method RightAlign:TStringBuilder(length:Int)
+		bmx_stringbuilder_rightalign(buffer, length)
+		Return Self
+	End Method
 
 	Rem
 	bbdoc: Removes a range of characters from the string builder.

+ 47 - 0
stringbuilder.mod/tests/test.bmx

@@ -0,0 +1,47 @@
+SuperStrict
+
+Framework brl.standardio
+Import brl.stringbuilder
+Import BRL.MaxUnit
+
+New TTestSuite.run()
+
+Type TStringBuilderTest Extends TTest
+
+	Field sb:TStringBuilder
+	
+	Method setup() { before }
+		sb = New TStringBuilder
+	End Method
+
+	Method testLeftAlign() { test }
+		sb.Append("12345")
+		sb.LeftAlign(10)
+		assertEquals("12345     ", sb.ToString())
+		
+		sb.SetLength(0)
+		sb.Append("123456789")
+		sb.LeftAlign(5)
+		assertEquals("12345", sb.ToString())
+		
+		sb.SetLength(0)
+		sb.LeftAlign(10)
+		assertEquals("          ", sb.ToString())
+	End Method
+	
+	Method testRightAlign() { test }
+		sb.Append("12345")
+		sb.RightAlign(10)
+		assertEquals("     12345", sb.ToString())
+
+		sb.SetLength(0)
+		sb.Append("123456789")
+		sb.RightAlign(5)
+		assertEquals("56789", sb.ToString())
+
+		sb.SetLength(0)
+		sb.RightAlign(10)
+		assertEquals("          ", sb.ToString())
+	End Method
+
+End Type