Count number of lines of code?

indeed :slight_smile:

?

I open the XML file with TextEdit and I search ā€œā€, it answered 8409 lines. Same as your code Michel (of course).

61420 lines of my own code, 290211 lines including other peopleā€™s code.

Programmed and debugged on the Mac, and first Win compile runs without a hitch :slight_smile:

Iā€™m getting good at this :slight_smile: [/exuberance]

If you want to count actual lines of code, ignoring blank lines and lines that contain solely comments, this RegEx pattern in a loop will do it:

^ *<sourceline>(?![ \\t]*(?://|'|REM))[^\\r\
]*[A-Z][^\\r\
]+</sourceline>$

Assuming you read the text in the variable source, this is the code:

dim rx as new RegEx
rx.SearchPattern = "^ *<sourceline>(?![ \\t]*(?://|'|REM))[^\\r\
]*[A-Z][^\\r\
]+</sourceline>$"

dim lineCount as integer
dim match as RegExMatch = rx.Search( source )
while match <> nil
  lineCount = lineCount + 1
  match = rx.Search()
wend

// lineCount has what you need

Oh, I made that way too complicated. This is the simplified pattern:

^ *<sourceline>[a-z#]

And if you want to ignore the lines that Xojo inserts, e.g., ā€œSub Ā…ā€, ā€œFunction Ā…ā€, ā€œEnd Subā€, and ā€œEnd Functionā€, use this:

^ *<sourceline>(?!End Sub|End Function|Sub |Function )[a-z#]

And of course you can stick with the single line grep and feed it the regex Kem came up with:

$ grep -cie "^ *<sourceline>[a-z#]" myProj.xojo_xml_project
21

You could pipe it through wc if you want to get word and character counts:

$ grep -ie "^ *<sourceline>[a-z#]" myProj.xojo_xml_project | wc
      21      63    1086

Without testing, Iā€™d guess grep would be significantly faster than pure Xojo code. It would also be faster to use RegExMBS instead of the native RegEx, if you can, and Iā€™d expect that speed to approach that of grep.

131ms across 62kloc for grep, did not try Xojo solution.

Beauty!

Thanks everyone :slight_smile:

You should have said you wanted nothing but a RegEx solution :confused:

I didnā€™t.

Iā€™m grateful for all solutions, however you can only check one post as the answer post, and RegEx gives the best possible speed.

Our of curiosity, what is the speed comparison so we can benefit as well? i.e. time grep ā€¦ vs. timing the regex?

Thanks Markus!