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
 
I thought so too.
Or at least a modular system. :p

JASS:
exitwhen a
exitwhen b

// ->
exitwhen a or b
 
.. 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.
 
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).
 
> 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.
 
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. :)
 
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?
 
static if's? looks prettier to change a constant
 
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.
 
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? :)
 
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.
  • tom_mai78101 tom_mai78101:
    Currently in the middle of getting the probate process going. We're doing the informal probate process.
    +3
  • Varine Varine:
    A probate is usually done with a will, yes? If so I am sorry for your loss
    +1
  • The Helper The Helper:
    Yeah Tom, me too sorry for your loss buddy my mom told me she finds out her olds friend died from Google searching them. She had not talked to one of her old friends in a year and found out she died from Google. Also another one in the same session. RIP all of them my sincere condolences Tom
    +1
  • Varine Varine:
    We have some elderly guests that regularly come hang out at the bar at the end of the night, and every once in a while we don't see someone for a few weeks and then someone shows up with their obituary.
  • Varine Varine:
    We usually let them do their memorials there in the morning if they want to and I'll make them some snacks and drinks. There was one guy named Tom that came in like every night and would sit by himself and get a bunch of soup and a glass of wine. idk why but he LOVED our fucking soup, like he would order a fucking quart of it at a time and would always get so sad when we stop doing it for the summer.
    +1
  • Varine Varine:
    But he also loved our calamari, which is another thing I hate but it sells super well so I can't change it. There was one day he came in and was asking me how to make it, because he tried to at home once in the off season when we stop running it and he really wanted it lol
  • Varine Varine:
    I think he's one of the only people I've made recipes for for free because he really wanted a broccoli cheddar, and it was like dude I don't have a recipe, it's just whatever I have, but here, this is how you do it
  • Varine Varine:
    I don't think he ever figured out how to do the calamari in a pan though, like idk how to do that either. He was afraid of the at home deep fryers though and it's like yeah, that's fair, I am too
  • Varine Varine:
    He was just such a sweet old man, we had two servers pregnant and they held a baby shower together, he was soooooo fucking excited to get to see a baby. Unfortunately he died a month or so before they were born
  • The Helper The Helper:
    So I decided to Google some people that I had not seen or heard from in a while and sure enough one of my old best friends, we had a falling out years ago but whatever, find out he died of Pancreatic Cancer in January. I have also lost a few of my closer acquaintances from growing up the last year. Getting old - people die - I kinda thought it was going to be this way a few years ago....
    +2
  • The Helper The Helper:
    Forum running super slow again
  • Ghan Ghan:
    Not really clear from the stats as to what is causing the slowness.
  • Ghan Ghan:
    We get a lot of guest traffic so it may just be the load is getting too high and not from any particular source.
  • Ghan Ghan:
    Looks like the server is maxed out on CPU.
  • Ghan Ghan:
    Oh it looks like a lot of the traffic is Silkroad Forums. That domain isn't protected by Cloudflare.
  • Ghan Ghan:
    But the old Silkroad site is still on its own server. I just had a test site set up on this server for it.
  • Ghan Ghan:
    I just disabled that test site. Let's see if that helps the load.
  • Ghan Ghan:
    Looks much better already.
  • The Helper The Helper:
    I had actually forgot about the Silkroad site. I had asked
  • The Helper The Helper:
    SD Ryoko about it and he said the couple of people left on there really like it, that was a few years ago, maybe I should check back
  • jonas jonas:
    I guess when you're getting old, and the last day of soup season draws near, you start wondering
  • jonas jonas:
    will I make it to the start of the next season? or was this the last time I'll ever have my favorite dish?
  • The Helper The Helper:
    I am doing my first Vibe Coding project. In installed the environment and tools according to instructions but it is all chat doing this for me at my direction. It is fun really and holy shit I might finish in 2 hours what it would have taken a day to in my Access and this would be an electron app complete new
  • Ghan Ghan:
    Good stuff.
  • Ghan Ghan:
    Just make sure it is secure. :)

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials
      Top