bmx-ng / brl.mod

BlitzMax Runtime Libraries, for BlitzMax NG.
12 stars 11 forks source link

brl.TStringbuilder; extended startsWith() #288

Closed Scaremonger closed 1 year ago

Scaremonger commented 1 year ago

In TStringBuilder there is a function bmx_stringbuilder_matches that is not exposed in common.bmx. This function is currently used internally for startsWith() but can be used to extend this function so we can perform quicker substring checks.

The code below extends TStringBuilder with a new startsWith() that allows you to begin the search at any point within the buffer; but the method could easily be added into TStringBuilder.

SuperStrict

Extern
    Function bmx_stringbuilder_matches:Int(buffer:Byte Ptr, beginIndex:Int, subString:String)
End Extern

Type TStringBuilderPlus Extends TStringBuilder

    Rem
    bbdoc: Returns true if string starts with @subString.
    End Rem
    Method StartsWith:Int( subString:String, start:Int )
        Return bmx_stringbuilder_matches( buffer, start, subString )
    End Method
End Type

Local sb:TStringBuilderPlus = New TStringBuilderPlus()

sb.append( "Hello World, what a great day" )

If sb.startswith( "World", 6 ); Print "YES"

Please could you consider this to be included?

Regards, Si... [Scaremonger]

GWRon commented 1 year ago

I would prefer to name it "contains" or "find" (as reminiscence to the brl command)