Browse Source

Added startIndex to StartsWith. Resolves #288.

Brucey 1 year ago
parent
commit
bcef5a9567

+ 2 - 2
stringbuilder.mod/common.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2018-2022 Bruce A Henderson
+' Copyright (c) 2018-2023 Bruce A Henderson
 ' 
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages
 ' warranty. In no event will the authors be held liable for any damages
@@ -34,7 +34,7 @@ Extern
 	Function bmx_stringbuilder_reverse(buffer:Byte Ptr)
 	Function bmx_stringbuilder_reverse(buffer:Byte Ptr)
 	Function bmx_stringbuilder_substring:String(buffer:Byte Ptr, beginIndex:Int, endIndex:Int)
 	Function bmx_stringbuilder_substring:String(buffer:Byte Ptr, beginIndex:Int, endIndex:Int)
 	Function bmx_stringbuilder_append_stringbuffer(buffer:Byte Ptr, buffer2:Byte Ptr)
 	Function bmx_stringbuilder_append_stringbuffer(buffer:Byte Ptr, buffer2:Byte Ptr)
-	Function bmx_stringbuilder_startswith:Int(buffer:Byte Ptr, subString:String)
+	Function bmx_stringbuilder_startswith:Int(buffer:Byte Ptr, subString:String, startIndex:Int)
 	Function bmx_stringbuilder_endswith:Int(buffer:Byte Ptr, subString:String)
 	Function bmx_stringbuilder_endswith:Int(buffer:Byte Ptr, subString:String)
 	Function bmx_stringbuilder_find:Int(buffer:Byte Ptr, subString:String, startIndex:Int)
 	Function bmx_stringbuilder_find:Int(buffer:Byte Ptr, subString:String, startIndex:Int)
 	Function bmx_stringbuilder_findlast:Int(buffer:Byte Ptr, subString:String, startIndex:Int)
 	Function bmx_stringbuilder_findlast:Int(buffer:Byte Ptr, subString:String, startIndex:Int)

+ 8 - 4
stringbuilder.mod/glue.c

@@ -1,5 +1,5 @@
 /*
 /*
-  Copyright (c) 2018-2022 Bruce A Henderson
+  Copyright (c) 2018-2023 Bruce A Henderson
   
   
   This software is provided 'as-is', without any express or implied
   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any damages
   warranty. In no event will the authors be held liable for any damages
@@ -185,9 +185,13 @@ int bmx_stringbuilder_matches(struct MaxStringBuilder * buf, int offset, BBStrin
 	return 1;
 	return 1;
 }
 }
 
 
-int bmx_stringbuilder_startswith(struct MaxStringBuilder * buf, BBString * subString) {
-	if (subString->length <= buf->count) {
-		return bmx_stringbuilder_matches(buf, 0, subString);
+int bmx_stringbuilder_startswith(struct MaxStringBuilder * buf, BBString * subString, int startIndex) {
+	if (startIndex < 0) {
+		startIndex = 0;
+	}
+
+	if ((startIndex + subString->length) <= buf->count) {
+		return bmx_stringbuilder_matches(buf, startIndex, subString);
 	}
 	}
 	return 0;
 	return 0;
 }
 }

+ 2 - 2
stringbuilder.mod/glue.h

@@ -1,5 +1,5 @@
 /*
 /*
-  Copyright (c) 2018-2022 Bruce A Henderson
+  Copyright (c) 2018-2023 Bruce A Henderson
   
   
   This software is provided 'as-is', without any express or implied
   This software is provided 'as-is', without any express or implied
   warranty. In no event will the authors be held liable for any damages
   warranty. In no event will the authors be held liable for any damages
@@ -51,7 +51,7 @@ void bmx_stringbuilder_reverse(struct MaxStringBuilder * buf);
 BBString * bmx_stringbuilder_substring(struct MaxStringBuilder * buf, int beginIndex, int endIndex);
 BBString * bmx_stringbuilder_substring(struct MaxStringBuilder * buf, int beginIndex, int endIndex);
 void bmx_stringbuilder_append_stringbuffer(struct MaxStringBuilder * buf, struct MaxStringBuilder * other);
 void bmx_stringbuilder_append_stringbuffer(struct MaxStringBuilder * buf, struct MaxStringBuilder * other);
 int bmx_stringbuilder_matches(struct MaxStringBuilder * buf, int offset, BBString * subString);
 int bmx_stringbuilder_matches(struct MaxStringBuilder * buf, int offset, BBString * subString);
-int bmx_stringbuilder_startswith(struct MaxStringBuilder * buf, BBString * subString);
+int bmx_stringbuilder_startswith(struct MaxStringBuilder * buf, BBString * subString, int startIndex);
 int bmx_stringbuilder_endswith(struct MaxStringBuilder * buf, BBString * subString);
 int bmx_stringbuilder_endswith(struct MaxStringBuilder * buf, BBString * subString);
 int bmx_stringbuilder_find(struct MaxStringBuilder * buf, BBString * subString, int startIndex);
 int bmx_stringbuilder_find(struct MaxStringBuilder * buf, BBString * subString, int startIndex);
 int bmx_stringbuilder_findlast(struct MaxStringBuilder * buf, BBString * subString, int startIndex);
 int bmx_stringbuilder_findlast(struct MaxStringBuilder * buf, BBString * subString, int startIndex);

+ 8 - 6
stringbuilder.mod/stringbuilder.bmx

@@ -1,4 +1,4 @@
-' Copyright (c) 2018-2022 Bruce A Henderson
+' Copyright (c) 2018-2023 Bruce A Henderson
 ' 
 ' 
 ' This software is provided 'as-is', without any express or implied
 ' This software is provided 'as-is', without any express or implied
 ' warranty. In no event will the authors be held liable for any damages
 ' warranty. In no event will the authors be held liable for any damages
@@ -23,10 +23,12 @@ bbdoc: A string builder.
 End Rem	
 End Rem	
 Module BRL.StringBuilder
 Module BRL.StringBuilder
 
 
-ModuleInfo "Version: 1.17"
+ModuleInfo "Version: 1.18"
 ModuleInfo "License: zlib/libpng"
 ModuleInfo "License: zlib/libpng"
-ModuleInfo "Copyright: 2018-2022 Bruce A Henderson"
+ModuleInfo "Copyright: 2018-2023 Bruce A Henderson"
 
 
+ModuleInfo "History: 1.18"
+ModuleInfo "History: Added optional startIndex to StartsWith()."
 ModuleInfo "History: 1.17"
 ModuleInfo "History: 1.17"
 ModuleInfo "History: Added AppendCStringBytes() method."
 ModuleInfo "History: Added AppendCStringBytes() method."
 ModuleInfo "History: 1.16"
 ModuleInfo "History: 1.16"
@@ -680,10 +682,10 @@ Public
 	End Method
 	End Method
 	
 	
 	Rem
 	Rem
-	bbdoc: Returns true if string starts with @subString.
+	bbdoc: Returns #True if string starts with @prefix.
 	End Rem
 	End Rem
-	Method StartsWith:Int(subString:String)
-		Return bmx_stringbuilder_startswith(buffer, subString)
+	Method StartsWith:Int(prefix:String, startIndex:Int = 0)
+		Return bmx_stringbuilder_startswith(buffer, prefix, startIndex)
 	End Method
 	End Method
 	
 	
 	Rem
 	Rem

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

@@ -179,4 +179,12 @@ Type TStringBuilderTest Extends TTest
 		MemFree(buf)
 		MemFree(buf)
 	End Method
 	End Method
 
 
+	Method testStartsWith() { test }
+		sb.Append("Hello World")
+		assertTrue( sb.StartsWith("Hello") )
+		assertFalse( sb.StartsWith("hello") )
+		assertFalse( sb.StartsWith("World") )
+		assertTrue( sb.StartsWith("World", 6) )
+	End Method
+
 End Type
 End Type