zOs/REXX/RANGE

rangeTest:
    call rt1 '', 1
    call rt1 '5', 1
    call rt1 '5', 4
    call rt1 '5', 5
    call rt1 '5', 6
    call rt1 '5', 9
    call rt1 '4-6', 1
    call rt1 '4-6', 3
    call rt1 '4-6', 4
    call rt1 '4-6', 5
    call rt1 '4-6', 6
    call rt1 '4-6', 7
    call rt1 '4-6', 9
    call rt1 '0 4-6', 1
    call rt1 '0 4-6', 3
    call rt1 '0 4-6', 4
    call rt1 '0 4-6', 5
    call rt1 '0 4-6', 6
    call rt1 '0 4-6', 7
    call rt1 '0 4-6', 9
    call rt1 '0 4-6 11-12 15', 1
    call rt1 '0 4-6 11-12 15', 3
    call rt1 '* 4-6 11-12 15', 4
    call rt1 '* 4-6 11-12 15', 5
    call rt1 '* 4-6 11-12 15', 6
    call rt1 '* 4-6 11-12 15', 7
    call rt1 '* 4-6 11-12 15', 9
    return
endProcedure rangeTest

rt1:procedure
parse arg ra, nn
    res = rangeAdd(ra, nn)
    say 'rangeAdd' ra',' nn '->' res
    return res
endProcedure rt1

/*--- add a member to a range
      a range is a string of the form '7 6-9 11' ---------------------*/
rangeAdd: procedure expose m.
parse arg ra, nn
    do wx=1 to words(ra)
        parse value word(ra, wx) with von '-' bis
        if bis = '' then
            bis = von
        if nn-1 > bis then
            iterate
        else if nn-1 = bis then
            bis = nn
        else if nn >= von then
            return ra
        else if nn+1 = von then
            von = nn
        else
            return strip(subWord(ra, 1, wx-1) nn subWord(ra, wx))
        return strip(subWord(ra, 1, wx-1) von'-'bis subWord(ra, wx+1))
        end
    return strip(ra nn)
endProcedure rangeAdd

/*--- return true/false whether nn is in range ra --------------------*/
rangeIsIn: procedure expose m.
parse arg ra, nn
    do wx=1 to words(ra)
        parse value word(ra, wx) with von '-' bis
        if bis = '' then
            bis = von
        if nn < von then
            return 0
        if nn <= bis then
            return 1
        end
    return 0
endProcedure rangeIsIn

/*--- next ele in range ----------------------------------------------*/
rangeNext: procedure expose m.
parse arg ra, la
    do wx=1 to words(ra)
        parse value word(ra, wx) with von '-' bis
        if la < von then
            return von
        if bis = '' then
            bis = von
        if la < bis then
            return la+1
        end
    return ''
endProcedure rangeNext