System String Plode System

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
Introduction:
This system is designed to handle string reading/parsing data easier.
It can also shorten down your code when you're looping through a string.
It also makes your script easier to read.
However, if you want to use this you must use the NewGen editor.

The Actual System:
JASS:
library Plode
    
    globals
        constant integer PLODE_MAX_SIZE = 256
    endglobals
    
    struct plode
        integer       size
        integer       count      = 0
        integer       pos        = 0
        string        chr
        string        original
        string  array char       [PLODE_MAX_SIZE]
        
        method next takes nothing returns boolean            
            set .chr      = .char[.count]
            set .pos      = .count
            set .count    = .count + 1
            
            if .count > .size then
                return true
            endif
            
            return false
        endmethod
              
        method implode takes nothing returns string 
            return .original
        endmethod
        
        static method explode takes string s returns plode
            local plode   p = plode.create()
            local integer i = 0
            local integer l = StringLength( s )
            
            set p.size      = l
            set p.original  = s
            
            loop
                exitwhen i > l
                set p.char<i> = SubString( s, i, i + 1 )      
                set i = i + 1
            endloop
            
            return p
        endmethod
        
    endstruct
    
endlibrary</i>


Explanation:
This struct has a couple of methods/variables which you can use.
  1. size - The stringlength of the string exploded.
  2. pos - The position of the current character in the string.
  3. chr - The current character.
  4. next() - This method sets the above variables to their next in line new value.
  5. explode( str ) - Explode a string by every character which enables you to scan a string quickly.
  6. implode() - This method is the original exploded string. It isn't really "imploding" but I meanly added it because its a part of "im/ex" plode.
JASS:
scope PlodeExample initializer init
    
    globals
        constant string ALPHABET = &quot;abcdefghijklmnopqrstuvwxyz&quot;
    endglobals
    
    private function init takes nothing returns nothing
        local plode p = plode.explode( ALPHABET )
        
        loop
            exitwhen p.next()
            
            call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, p.chr + &quot; is the &quot; + I2S( p.pos ) + &quot;&#039;th character in the string.&quot; )
        endloop
        
        call DisplayTextToPlayer( GetLocalPlayer(), 0, 0, &quot;And the original string is: &quot; + p.implode() ) 
        
        call p.destroy() // Important if you&#039;re not using it anymore.
    endfunction
    
endscope


Summary:
This might perhaps be a simple script, but its sure as hell easy to use.
I find this much simpler then the normal looping of strings.
 

Xorifelse

I'd love to elaborate about discussions...........
Reaction score
87
Inspired by PHP? :rolleyes:
I believe with PHP you can explode with an optional delimiter.
However, Jass has its bugs with "huge" loops so sometimes it exits a function mid-execution.
To avoid that, I mainly kept it simple.

Not inspired by PHP, but I noticed I kept scanning a string character by character and I was annoyed because it could be done much simpler.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top