Jump to content

_StringStartsWith, _StringEndsWith


cppman
 Share

Recommended Posts

These two functions can be found in the string class in C#. I found them very useful for parsing things like constants, single line(and multiline) comments, etc. I noticed nobody has written these two functions yet, so I decided to. These two functions simply check if the input string starts with the search string and can also check if the input string ends with the search string.

You can download the main header file here:

StringStart.au3

and an example for each of the functions here:

_StringStartsWith.au3

_StringEndsWith.au3

Also, here is a "real world" example of analysing basic C styled code using these functions.

#include "StringStart.au3"

Local $sCode = "#include <iostream> " & @CRLF & "#include <windows.h>" & @CRLF & @CRLF & "int main(int argc, char* argv[])" & @CRLF & "{" & @CRLF
$sCode &= "    //This is a comment," & @CRLF & "    //that runs onto another line, that is also a comment." & @CRLF
$sCode &= "}"

Local $aResults = _ParseLines($sCode)

MsgBox(0, "Results:", "Bracket Lines: " & $aResults[0] & @CRLF & "Comment Lines: " & $aResults[1])



;; FUNCTION DECLS
Func _ParseLines($szData)
    Local $aiCounts[2] = [0, 0]
    Local $asLines = StringSplit($szData, @CRLF)
    For $i =  1 to $asLines[0]
        $asLines[$i] = StringStripWS($asLines[$i], BitOR(1, 2))
        if ( (_StringStartsWith($asLines[$i], "{") or _StringStartsWith($asLines[$i], "}")) and (_StringEndsWith($asLines[$i], "{") or _StringEndsWith($asLines[$i], "}")) ) Then
            $aiCounts[0] += 1
        Elseif (_StringStartsWith($asLines[$i], "//")) Then
            $aiCounts[1] += 1
        EndIf
    Next
    return $aiCounts
EndFunc
Edited by chris95219
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...