Snippet stringFilter

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
stringFilter
Created by Darthfett

Description:
A small collection of functions used to filter or replace specified things out of a string, such as metacharacters.

Requirements:
N/A

The rest of the documentation can be found in the system code, below:

JASS:
library stringFilter
/*
__________________________________________________________________________________

        stringFilter library, created by Darthfett - version 1.0
        http://www.thehelper.net/forums/showthread.php?t=143589
        
                                Requirements
                                
-vJass compiler (such as JASSHelper)
    -If you remove the library and multi-line comment(s), you can make this JASS compatable.

                                Documentation
                                
-All functions are standalone.  Feel free to copy an individual function.

-Credit for this library is not necessary.  Feel free to use it in your map.
If you feel obligated to credit me, I won't object.  I only ask that you do 
not simply copy and paste the library as your own.

                                    API

function ReplaceString takes string s, string find, string replace, checkCase returns string
    searches through the entirety of s, looking for find.  If it finds 'find', it 
    replaces 'find' in the string with 'replace'.
    checkCase determines whether case is checked
    
function Strip takes string s returns string
    Removes all leading and trailing spaces from the string
    
function LStrip takes string s returns string
    Removes all leading spaces from the string
    
function RStrip takes string s returns string
    Removes all trailing spaces from the string
    
function StripMeta takes string s, boolean strip returns string
    searches through the entirety of s, looking for (!@#$%...) values. The boolean 
    'strip' determines if these values are stripped from the string, or if
    all characters other than these are stripped.
    Using Strip as false will strip AlphaNumeric characters.
    
function StripNumeric takes string s, boolean strip returns string
    searches through the entirety of s, looking for (0-9) values. The boolean 
    'strip' determines if these values are stripped from the string, or if
    all characters other than these are stripped.
    Using strip as false will strip AlphaMeta Characters
    
function StripAlpha takes string s, boolean strip returns string
    searches through the entirety of s, looking for (a-z,A-Z) values. The boolean 
    'strip' determines if these values are stripped from the string, or if
    all characters other than these are stripped.
    Using strip as false will Strip MetaNumeric Characters
    
function StripUpper takes string str, boolean strip returns string
    searches through the entirety of str, looking for (A-Z) values. The boolean 
    'strip' determines if these values are stripped from the string, or if
    all characters other than these are stripped.
    Using Strip as false will strip Lowercase Characters
__________________________________________________________________________________
*/

function ReplaceString takes string s, string find, string replace, boolean checkCase returns string
    local integer i = 0
    local string c
    local integer sLen = StringLength(s)
    local integer findLen = StringLength(find)
    local string str = ""
    if find == "" or find == null then
        return s //prevent infinite loop
    endif
    if not checkCase then
        set s = StringCase(s,false)
        set find = StringCase(find,false)
    endif
    loop
        exitwhen i + findLen > sLen
        set c = SubString(s,i,i+findLen)
        if c == find then
            set str = str + replace
            set i = i + findLen
        else
            set str = str + SubString(s,i,i+1)
            set i = i + 1
        endif
    endloop
    return str + SubString(s,i,sLen)
endfunction

function Strip takes string s returns string
    local integer i = 0
    local integer start = 0
    local integer end = StringLength(s)
    loop
        exitwhen i == end or SubString(s,i,i+1) != " "
        set i = i + 1
    endloop
    set start = i
    set i = end
    loop
        exitwhen i == start or SubString(s,i-1,i) != " "
        set i = i - 1
    endloop
    return SubString(s,start,i)
endfunction

function LStrip takes string s returns string
    local integer i = 0
    local integer start = 0
    loop
        exitwhen SubString(s,i,i+1) != " "
        set i = i + 1
    endloop
    return SubString(s,i,StringLength(s))
endfunction

function RStrip takes string s returns string
    local integer end = StringLength(s)
    local integer i = end
    loop
        exitwhen i == 0 or SubString(s,i-1,i) != " "
        set i = i + 1
    endloop
    return SubString(s,0,i)
endfunction

function StripMeta takes string s, boolean strip returns string
    local integer i = 0
    local string str = ""
    local string c
    loop
        set c = SubString(s,i,i+1)   
        exitwhen c == ""
        if strip then
            if I2S(S2I(c)) == c or StringCase(c,true) != StringCase(c,false) then
                set str = str + c
            endif
        elseif I2S(S2I(c)) != c and StringCase(c,true) == StringCase(c,false) then
            set str = str + c
        endif
        set i = i + 1
    endloop
    return str
endfunction

function StripNumeric takes string s, boolean strip returns string
    local integer i = 0
    local string str = ""
    local string c
    loop
        set c = SubString(s,i,i+1)
        exitwhen c == ""
        if strip then
            if I2S(S2I(c)) != c then
                set str = str + c
            endif
        elseif I2S(S2I(c)) == c then
            set str = str + c
        endif
        set i = i + 1
    endloop
    return str
endfunction

function StripAlpha takes string s, boolean strip returns string
    local integer i = 0
    local string str = ""
    local string c
    loop
        set c = SubString(s,i,i+1)
        exitwhen c == ""
        if strip then
            if StringCase(c,true) == StringCase(c,false) then
                set str = str + c
            endif
        elseif StringCase(c,true) != StringCase(c,false) then
            set str = str + c
        endif
        set i = i + 1
    endloop
    return str
endfunction

function StripUpper takes string str, boolean strip returns string
    local integer i = 0
    local string s = ""
    local string c
    loop
        set c = SubString(str,i,i+1)
        exitwhen c == ""
        if strip then
            if StringCase(c,false) == c then
                set s = s + c
            endif
        elseif StringCase(c,true) == c then
            set s = s + c
        endif
        set i = i + 1
    endloop
    return s
endfunction

endlibrary
 

Romek

Super Moderator
Reaction score
963
I thought so too.
Or at least a modular system. :p

JASS:
exitwhen a
exitwhen b

// ->
exitwhen a or b
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
.. and have a 700+ line long library with tons of functions not everyone will need, with a gigantic documentation that no one will read through? If anything, this splits it up into much more manageable code. I have 3 other systems that I'm close to finishing that actually only use 2 of these systems.

I thought so too.
Or at least a modular system. :p

Too much documentation and code for one place.

JASS:
exitwhen a
exitwhen b

// ->
exitwhen a or b

I heard somewhere that JASS checks the entire condition (unlike some other real languages), so if a is false, it would still continue to evaluate b. Evaluating a to false would prevent b from being evaluated. There's a tiny increase in speed for the last iteration of the function. If the user supplies an empty string, those functions will go through with only one StringCase and one SubString call.
 
Reaction score
341
There are better ways than submitting 5 string libraries.
Create a core library with some basic functions then have the rest be addons and submit them all at once (in one thread).
 

Romek

Super Moderator
Reaction score
963
> I heard somewhere that JASS checks the entire condition (unlike some other real languages)
I don't think it does.
In '[ljass]a or b[/ljass]', if 'a' is true, it'll short circuit and 'b' won't be checked.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I don't agree that it should be all one library. Most of the functions are unrelated. It was needed to split it all up, in order to make it easy to find documentation, and to search through the long list of functions.

As for a modular library, it might work if this was a struct (but it's not), or if modules worked outside of structs.

Anyways, I merged all the exitwhen conditions, and also added the boolean checkCase to the ReplaceString function. Thanks for confirming that. :)
 

Jesus4Lyf

Good Idea™
Reaction score
397
As for a modular library, it might work if this was a struct (but it's not), or if modules worked outside of structs.
JASS:
//! runtextmacro optional ...

That's essentially library modules. :)

I'd appreciate this being merged, otherwise the disagreement on the structure of the library makes it difficult to approve.

Do you think 5 posts in a thread using optional textmacros could solve this dilemma?
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
static if's? looks prettier to change a constant
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
static if's? looks prettier to change a constant

Hmm? There are no constants in this library. :confused:

JASS:
//! runtextmacro optional ...

That's essentially library modules. :)

I'd appreciate this being merged, otherwise the disagreement on the structure of the library makes it difficult to approve.

Do you think 5 posts in a thread using optional textmacros could solve this dilemma?

The problem with module libraries like this one, is that none of them interact with each other. If I need the Strip function for something, I would require the stringFilter library, not the theoretical "string" library which may or may not have the stringFilter library's functions. I wouldn't want people getting the undeclared function error because they don't understand how a module library works.

I'll admit it might be nice to have sub-libraries that you can import with a line like this:

JASS:
import string.*


but with the current capabilities, you can't do something like this. It thus makes sense to leave each library separate, so that syntax errors will be proper for those using the libraries.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I decided to look back at some of my older resources, and saw this wasn't approved, nor graveyarded. Shameless self-bump! :p

I think this is approval-worthy, but I'll not approve it myself. Could another mod take a look, please? :)
 

Sim

Forum Administrator
Staff member
Reaction score
534
Looks good!

Approved. It's nice the many little things you can do with that. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top