Warcraft III resources & community, 2003–2006 · archived

StringFragment

not rated
Submitted by PepparText Parsing0 downloads
Divides string s into fragments, where cutPoints separate them, and returns fragment n, where fragment 0 is the first fragment.

Example:
StringFragment(" The JASS .Vault", 0, ". ") = "The"
StringFragment(" The JASS .Vault", 1, ". ") = "JASS"
StringFragment(" The JASS .Vault", 2, ". ") = "Vault"
StringFragment(" The JASS .Vault", 3, ". ") = ""
In this example "." and " " (space) are cutPoints.

This function loops through every character in string s. If the character isn't a cutPoint, then it is added to the buffer. If the character is a cutPoint or the end of the string is reached, and the buffer isn't empty, then the fragment counter is compared to the wanted fragment number. If they were equal then the function returns the buffer. If not the function increases the fragment counter and continues, if it hadn't reached the end of the string.

Source

function StringFragment takes string s, integer sectionNum, string cutPoints returns string
    local integer n = sectionNum //wanted fragment
    local integer i = 0 //character iterator
    local integer w = 0 //fragment counter
    local integer t //token iterator
    local string u = "" //buffer
    local string c //current character
    local integer numTokens  = 0 //token counter
    local string array token //token array, for easier access later on
    if s == "" or s == null then
        return s
    endif
    if n < 0 then
        return null
    endif
    loop
        set c = SubString(cutPoints, i, i + 1)
        if c == "" then
            exitwhen true
        else
            set numTokens = numTokens + 1
            set token[i] = c
        endif
        set i = i + 1
    endloop
    set i = 0
    loop
        set c = SubString(s, i, i + 1)
        if c == "" then
            if w == n then
                return u
            endif
            return ""
        endif
        set t = 0
        loop
            if t >= numTokens then
                set u = u + c
                exitwhen true
            elseif token[t] == c then
                if u == "" then
                    exitwhen true
                elseif w == n then
                    return u
                else
                    set w = w + 1
                    set u = ""
                    exitwhen true
                endif
            endif
            set t = t + 1
        endloop
        set i = i + 1
    endloop
    if w == n then
        return u
    endif
    return ""
endfunction

Comments

0

No comments.