• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[System] String Utilities

Level 7
Joined
Apr 30, 2011
Messages
359
JASS:
//========================================================================================
//      
//      String Utilities
//      -*- overcold_ice -*-
//      
//     -[*] Requirements:
//          - JNGP
//          - latest version of JassHelper
//      
//          For your string-manipulating needs
//      This system provides functions to Find, Insert, Remove, Multiply, and Divide
//      strings.
//      
//     -[*] API:
//      
//      function FinStr takes string s, string f, integer i returns integer
//          - string  s: base string
//          - string  f: string to find
//          - integer i: start order
//          -> integer = found string start order
//      
//      Ex:
//      
//      String: ABCDabcd | call FinStr("ABCDabcd", "abcd", 1) -> 5
//              -------- | call FinStr("ABCDabcd", "abcd", 5) -> 0 (not found)
//      Order : 12345678 | call FinStr("ABCDabcd", "aaaa", 1) -> 0 (not found)
//      
//      function InsStr takes string s, string a, integer i returns string
//          - string  s: base string
//          - string  a: string to insert
//          - integer i: insert order
//          -> string = string result
//      
//      Ex:
//      
//      String: ABCDabcd | call InsStr("ABcd", "CDab", 3) -> "ABCDabcd"
//              -------- | call InsStr("ABCD", "abcd", 5) -> "ABCDabcd"
//      Order : 12345678 | call InsStr("abcd", "ABCD", 1) -> "ABCDabcd"
//      
//      function InsStrBeforeFound takes string s, string f, string a, integer i returns string
//          - string  s: base string
//          - string  f: string to find
//          - string  a: string to add
//          - integer i: start order
//          -> integer = string result
//      
//          - this will inserts a string [a] to each found string that match the criteria
//            [string f], starting from an order [i] to its end
//      
//      function RemStr takes string s, integer i, integer m returns string
//          - string  s: base string
//          - integer i: start order
//          - integer m: end order
//          -> string = string result
//      
//      Ex:
//      
//      String: ABCDabcd | call RemStr("ABCDabcd", 1, 4) -> "abcd"
//              -------- | call RemStr("ABCDabcd", 1, 2) -> "CDabcd"
//      Order : 12345678 | call InsStr("ABCDabcd", 6, 8) -> "ABCDa"
//      
//      function RemStrFound takes string s, string r, integer i, boolean w returns string
//          - string  s: base string
//          - string  r: string to remove
//          - integer i: start order
//          - boolean w: true  = removes whole words only
//                       false = removes each letters contained in string r
//      
//          - this will removes all found strings in string [s] with a criteria [string r]
//            starting from an order [i]. if it's a whole words removing, then this will
//            only removes any strings that can be found from an order [i] to its maximum
//            length [of s] that exactly match the whole words, otherwise this will
//            removes each letters contained in the criteria [string r]
//      
//      function MulStr takes string s, integer m returns string
//          - string  s: base string
//          - integer m: multiply count
//          -> string = string result
//      
//      Ex:
//      
//      * | call MulStr("00", 3) -> "000000"
//      * | call MulStr("10", 5) -> "1010101010"
//      * | call MulStr("Xx", 1) -> "Xx"
//      
//      function MulStrFound takes string s, string f, integer i, integer m returns string
//          - string  s: base string
//          - string  f: string to multiply
//          - integer i: start order
//          - integer m: multiply count
//          -> string = string result
//      
//      Ex:
//      
//      String: 10.00001 | call MulStrFound("10.00001",  "0", 1, 2) -> "100.000000001"
//              -------- | call MulStrFound("10.00001", "10", 1, 4) -> "10101010.00001"
//      Order : 12345678 | call MulStrFound("10.00001",  "1", 1, 5) -> "111110.000011111"
//      
//      struct DivStr extends array
//          static method divide takes string s, integer d returns DivStr
//              - string  s: base string
//              - integer d: numbers of result / parts
//              -> DivStr  = results list
//          
//          method operator list[] takes integer i returns string
//              returns divided string of number [i]
//          
//          method list.destroy takes nothing returns nothing
//              destroys a list
//      
//      Ex:
//      
//      String: abcdefgh | local DivStr l = DivStr.divide("abcdefgh", 3)
//              -------- | l.list[1] == "abc", l.list[2] == "def", l.list[3] == "gh"
//      Order : 12345678 | call l.list.destroy()
//      
//========================================================================================
library StringUtils
    
    function FinStr takes string s, string f, integer i returns integer
        local integer ls = StringLength(s)
        local integer lf = StringLength(f)
        
        loop
            exitwhen i > ls - lf
            
            if SubString(s, i, i + lf) == f then
                return i
            endif
            
            set i = i + 1
        endloop
        
        return 0
    endfunction
    
    function InsStr takes string s, string a, integer i returns string
        local integer l = StringLength(s)
        
        return SubString(s, 0, i - 1) + a + SubString(s, i, l)
    endfunction
    
    function InsStrBeforeFound takes string s, string f, string a, integer i returns string
        local integer ls = StringLength(s)
        local integer la = StringLength(a)
        
        loop
            set i = FinStr(s, f, i)
            exitwhen i == 0
            
            set s = InsStr(s, a, i)
            
            set i = i + la + 1
        endloop
        
        return s
    endfunction
    
    function RemStr takes string s, integer i, integer m returns string
        local integer l = StringLength(s)
        
        return SubString(s, 0, i - 1) + SubString(s, m + 1, l)
    endfunction
    
    function RemStrFound takes string s, string r, integer i, boolean w returns string
        local integer l  = StringLength(r)
        local integer ec = 1
        local integer em = l
        local string  er = SubString(r, ec, ec)
        
        if not w then
            set l = 1
        endif
        
        loop
            if w then
                set i = FinStr(s, r, i)
                
                exitwhen i == 0
            else
                loop
                    set i = FinStr(s, r, i)
                    
                    if i == 0 then
                        set ec = ec + 1
                        set er = SubString(r, ec, ec)
                    endif
                    
                    exitwhen i != 0 or ec > em
                endloop
                
                exitwhen ec > em
            endif
            
            set s = RemStr(s, i, i + l - 1)
            
            set i = i + 1
        endloop
        
        return s
    endfunction
    
    function MulStr takes string s, integer m returns string
        loop
            exitwhen m == 0
            
            set s = s + s
            
            set m = m - 1
        endloop
        
        return s
    endfunction
    
    function MulStrFound takes string s, string f, integer i, integer m returns string
        local integer c = i
        
        loop
            exitwhen c == 0
            
            set s = InsStrBeforeFound(s, f, f, i)
            
            set c = c - 1
        endloop
        
        return s
    endfunction
    
    globals
        private hashtable       ht = InitHashtable()
        private integer         c  = 0
        private integer   array r
    endglobals
    
    private struct List extends array
        method operator [] takes integer i returns string
            return LoadStr(ht, this, i)
        endmethod
        
        method destroy takes nothing returns nothing
            set r [this] = r [0]
            set r [0]    = this
            
            call FlushChildHashtable(ht, this)
        endmethod
    endstruct
    
    struct DivStr extends array        
        method operator list takes nothing returns List
            return this
        endmethod
        
        private static method create takes nothing returns thistype
            local integer this = r [0]
            
            if this == 0 then
                set c    = c + 1
                set this = c
            else
                set r [0] = r [this]
            endif
            
            return this
        endmethod
        
        static method divide takes string s, integer d returns thistype
            local integer this = .create()
            local integer l    = StringLength(s)
            local integer pl   = l / d
            local integer pc   = 1
            local string  ps
            
            if pl < I2R(l) / I2R(d) then
                set pl = pl + 1
            endif
            
            loop
                set ps = SubString(s, (pc - 1) * pl, pl)
                exitwhen ps == ""
                
                call SaveStr(ht, this, pc, ps)
                
                set pc = pc + 1
            endloop
            
            return this
        endmethod
    endstruct
endlibrary

need something to add/remove?
 
Top