Last active
January 12, 2016 19:33
-
-
Save jesobreira/4640d6786891e6297e86 to your computer and use it in GitHub Desktop.
Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#cs | |
Range parser | |
This function parser printing-like intervals (like "1,2,3-5") and returns an array with every page number as item. | |
@author Jefrey S. Santos <jefrey[at]jefrey.ml> | |
#ce | |
Func rangeparser($interval) | |
$interval = StringReplace($interval, " ", "") | |
If Not StringRegExp($interval, "([0-9\-\,])") Then Return SetError(1, 0, False) | |
$interval = StringSplit($interval, ",") | |
Dim $return[1] | |
For $i = 1 To $interval[0] | |
If StringInStr($interval[$i], "-") Then | |
$split = StringSplit($interval[$i], "-") | |
If $split[0] <> 2 Then Return SetError(1, 0, False) | |
If $split[1] = $split[2] Then | |
ReDim $return[UBound($return)+1] | |
$return[UBound($return)-1] = $split[1] | |
Else | |
If $split[1] < $split[2] Then | |
$step = +1 | |
Else | |
$step = -1 | |
EndIf | |
For $j = $split[1] To $split[2] Step $step | |
ReDim $return[UBound($return)+1] | |
$return[UBound($return)-1] = $j | |
Next | |
EndIf | |
Else | |
ReDim $return[UBound($return)+1] | |
$return[UBound($return)-1] = $interval[$i] | |
EndIf | |
Next | |
$return[0] = UBound($return)-1 | |
Return $return | |
EndFunc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment