﻿id	summary	reporter	owner	description	type	status	milestone	component	version	severity	resolution	keywords	cc
2117	Improved _FileCountLines()	Spiff59	guinness	"The following version of _FileCountLines() is 60-70% faster than the production 3.3.8.0 version. It's MAIN improvement is that it uses roughly half the memory of the current version, enabling it to scan larger files and making it less susceptible to a memory allocation error.

Additionally, the helpfile does not document the existing @error = 2 condition, which is returned when scanning a 0-byte file.

Test script included. Remove the (extra) leading underslash on the function name if you intend to place it in production.
Thanks

{{{
#include <File.au3>

$timer = TimerInit()
;$Lines = _FileCountLines(@ScriptDir & ""\DataFile3.txt"")
$timer = TimerDiff($timer) / 1000
$timer2 = TimerInit()
$Lines2 = __FileCountLines(@ScriptDir & ""\DataFile3.txt"")
$timer2 = TimerDiff($timer2) / 1000
;MsgBox(0,"""", $Lines & "" lines"" & @CRLF & $timer & "" seconds"" & @CRLF & $Lines2 & "" lines"" & @CRLF & $timer2 & "" seconds"" )


Func __FileCountLines($sFilePath)
	Local $hFile = FileOpen($sFilePath, $FO_READ)
	If $hFile = -1 Then Return SetError(1, 0, 0)
	Local $sFileContent = StringStripWS(FileRead($hFile), 2)
	FileClose($hFile)
	StringRegExpReplace($sFileContent, ""\n"", """") ; terminated with linefeed terminated
	If Not @extended Then StringRegExpReplace($sFileContent, ""\r"", """") ; terminated with carriage return
	If Not @extended Then
		If StringLen($sFileContent) Then
			Return 1 ; single-line file
		Else
			Return SetError(2, 0, 0) ; empty file
		EndIf
	EndIf
	Return @extended
EndFunc   ;==>_FileCountLines
}}}"	Feature Request	closed	3.3.9.5	Standard UDFs		None	Completed		
