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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top