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\

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

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

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

// 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

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.


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!