Advertisement
mdelatorre

Extract a line from a string based on the specified line number

Dec 7th, 2024
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 2.60 KB | Source Code | 0 0
  1. Global Enum Step *2 $EXTRACTLINE_EMPTY, $EXTRACTLINE_INVALIDFORMAT, $EXTRACTLINE_LESSTHANONE, $EXTRACTLINE_OUTOFBOUNDS
  2.  
  3. Example()
  4.  
  5. Func Example()
  6.     Local $sText = ''
  7.     For $i = 1 To 10
  8.         $sText &= 'Line ' & $i & ': With some extra characters at the end.' & @CRLF
  9.     Next
  10.  
  11.     ; Valid examples:
  12.     PrintLine($sText, 10) ; Second to last line
  13.     PrintLine($sText, 1) ; First line
  14.     PrintLine($sText, 11) ; Last line
  15.     PrintLine($sText, Random(2, 9, 1)) ; Random line
  16.  
  17.     ; @error examples:
  18.     PrintLine(Null, 9) ; Null string
  19.     PrintLine($sText, 'Nine') ; Invalid number
  20.     PrintLine($sText, -100) ; Invalid line number
  21.     PrintLine($sText, 100) ; Out of bounds
  22. EndFunc   ;==>Example
  23.  
  24. ; Not part of the ExtractLine() function
  25. Func PrintLine($sValue, $iLine)
  26.     Local $bIsIncludeEOL = Random(0, 1, 1) = 1
  27.     Local $sLine = ExtractLine($sValue, $iLine, $bIsIncludeEOL)
  28.     ConsoleWrite('Line: ' & $iLine & ', IncludeEOL: ' & $bIsIncludeEOL & ', ' & '@error: ' & @error & ', Extracted: ' & $sLine & @CRLF)
  29. EndFunc   ;==>PrintLine
  30.  
  31. Func ExtractLine($sValue, $iLine, $bIncludeEOL = False) ; NOTE: This assumes line endings are \r\n, especially if you're using windows.
  32.     If $sValue = Null Or $sValue == '' Then
  33.         Return SetError($EXTRACTLINE_EMPTY, 0, '')
  34.     EndIf
  35.     If IsString($iLine) Then ; Check if the line number is a string
  36.         If Not StringIsDigit($sValue) Then ; The user didn't pass a line number
  37.             Return SetError($EXTRACTLINE_INVALIDFORMAT, 0, '')
  38.         EndIf
  39.         $iLine = Int($iLine) ; Parse as an integer datatype
  40.     EndIf
  41.    
  42.     Local Const $eSTART_LINE = 1
  43.     If $iLine < $eSTART_LINE Then ; Only line numbers 1 and above are supported
  44.         Return SetError($EXTRACTLINE_LESSTHANONE, 0, '')
  45.     EndIf
  46.     If $bIncludeEOL = Default Or Not IsBool($bIncludeEOL) Then ; If include the EOL is default or not a boolean datatype then set as false
  47.         $bIncludeEOL = False
  48.     EndIf
  49.  
  50.     Local Const $eEOLLENGTH = StringLen(@CRLF) ; Constant for the length of \r\n (which should be 2)
  51.  
  52.     Local $iBefore = $eSTART_LINE
  53.     If $iLine > $eSTART_LINE Then
  54.         $iBefore = StringInStr($sValue, @CRLF, Default, $iLine - 1) ; Find the @CRLF before
  55.         $iBefore += $eEOLLENGTH ; Optionally include the EOL
  56.     EndIf
  57.  
  58.     Local $iAfter = StringInStr($sValue, @CRLF, Default, $iLine) ; Find the @CRLF after
  59.     If $iAfter >= $eSTART_LINE Then
  60.         $iAfter += ($bIncludeEOL ? $eEOLLENGTH : 0) ; Optionally include the EOL
  61.     EndIf
  62.  
  63.     If $iAfter = 0 And $iBefore = $eEOLLENGTH Then
  64.         Return SetError($EXTRACTLINE_OUTOFBOUNDS, 0, '')
  65.     EndIf
  66.  
  67.     Return StringMid($sValue, $iBefore, $iAfter - $iBefore) ; Get the line using the before index and then calculating the length
  68. EndFunc   ;==>ExtractLine
Tags: AutoIT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement