Snippet More String Functions

quraji

zap
Reaction score
144
Here are some more string functions I whipped up. I wrote them as separate libraries because it was easier, and now I'm too lazy to repackage it as one library..
Before I add more or repackage this in a better way, I need to see if people are at all interested in these things (who uses strings anyways?).

Here they are (a couple require others):

Int2HexString
JASS:
library Int2HexString initializer init
    // converts an integer into a hex string (255 -> "FF")

globals
    private string array Hex_Values
endglobals

// see any decimal -> hex conversion guide online to see what's done here
function Int2HexString takes integer i returns string
    local string hex = ""
    local integer r = 0
    local string neg = ""
    if (i<0) then
        set neg = "-"
        set i = -i
    endif
    loop
        set r = ModuloInteger(i,16)
        set i = i/16
        set hex = Hex_Values[r] + hex
        exitwhen (i==0)
    endloop
    return neg+hex
endfunction

private function init takes nothing returns nothing
    // hex values
    set Hex_Values[0] = "0"
    set Hex_Values[1] = "1"
    set Hex_Values[2] = "2"
    set Hex_Values[3] = "3"
    set Hex_Values[4] = "4"
    set Hex_Values[5] = "5"
    set Hex_Values[6] = "6"
    set Hex_Values[7] = "7"
    set Hex_Values[8] = "8"
    set Hex_Values[9] = "9"
    set Hex_Values[10] = "A"
    set Hex_Values[11] = "B"
    set Hex_Values[12] = "C"
    set Hex_Values[13] = "D"
    set Hex_Values[14] = "E"
    set Hex_Values[15] = "F"
endfunction

endlibrary

Int2WordString
JASS:
library Int2WordString initializer init
    // Converts any integer to word form (163 -> "One Hundred and Sixty-Three")
    
//! textmacro Int2WordString_Config
    public string SPACE = " "
    public string HYPHEN = "-"
    public string NEGATIVE = "Negative"
    public string COMMA = ","
//! endtextmacro
    
globals
    //! runtextmacro Int2WordString_Config()
    
    private string array OnesTeens
    private string array Tens
    private string array Denom
endglobals

 // converts numbers less than a thousand to words
private function Int2WordString_helper takes integer i returns string
    local integer hundreds = i/100
    local integer tens = ModuloInteger(i, 100)
    local integer ones = 0
    local string r = null
    
    if (hundreds>0) then
        set r = OnesTeens[hundreds] + SPACE + Denom[0]+SPACE
    endif
    if (tens>0) then
        if (tens<20) then
            if (r==null) then
                set r = OnesTeens[tens]
            else
                set r = r+SPACE+OnesTeens[tens]
            endif
        else
            set ones = ModuloInteger(tens, 10)
            set tens = tens/10
            if (r==null) then
                set r = Tens[tens-2]
            else
                set r = r+Tens[tens-2]
            endif
            if (ones>0) then
                set r = r+HYPHEN+OnesTeens[ones]
            endif
        endif
    endif
    
    return r    
endfunction

 // works by reading right-to-left
 // each step in the loop converts the number into the next biggest denomination
 // then, converting that to a number (which can only be as high as 999) to words, and appending the appropriate denomination
function Int2WordString takes integer i returns string
    local string r = ""
    local string temp = ""
    local string neg = ""
    local integer denom = 0
    local integer n = 0
    
    if (i<0) then
        set i = -i
        set neg = NEGATIVE+SPACE
    endif
    if (i<20) then
        return OnesTeens<i>
    endif
    
    loop
        set n = ModuloInteger(i, 1000)
        set i = i/1000
        
        set temp = Int2WordString_helper(n)
        if (temp!=null) then
            if (denom&gt;0) then
                set r = Denom[denom]+COMMA+SPACE+r
            endif
            set r = temp+SPACE+r
        endif
        
        set denom = denom+1
        exitwhen (i&lt;=0)
    endloop
    
    return neg+r
endfunction

private function init takes nothing returns nothing
    set OnesTeens[0] = &quot;Zero&quot;
    set OnesTeens[1] = &quot;One&quot;
    set OnesTeens[2] = &quot;Two&quot;
    set OnesTeens[3] = &quot;Three&quot;
    set OnesTeens[4] = &quot;Four&quot;
    set OnesTeens[5] = &quot;Five&quot;
    set OnesTeens[6] = &quot;Six&quot;
    set OnesTeens[7] = &quot;Seven&quot;
    set OnesTeens[8] = &quot;Eight&quot;
    set OnesTeens[9] = &quot;Nine&quot;
    set OnesTeens[10] = &quot;Ten&quot;
    set OnesTeens[11] = &quot;Eleven&quot;
    set OnesTeens[12] = &quot;Twelve&quot;
    set OnesTeens[13] = &quot;Thirteen&quot;
    set OnesTeens[14] = &quot;Fourteen&quot;
    set OnesTeens[15] = &quot;Fifteen&quot;
    set OnesTeens[16] = &quot;Sixteen&quot;
    set OnesTeens[17] = &quot;Seventeen&quot;
    set OnesTeens[18] = &quot;Eighteen&quot;
    set OnesTeens[19] = &quot;Nineteen&quot;
    
    set Tens[0] = &quot;Twenty&quot;
    set Tens[1] = &quot;Thirty&quot;
    set Tens[2] = &quot;Forty&quot;
    set Tens[3] = &quot;Fifty&quot;
    set Tens[4] = &quot;Sixty&quot;
    set Tens[5] = &quot;Seventy&quot;
    set Tens[6] = &quot;Eighty&quot;
    set Tens[7] = &quot;Ninety&quot;
    
    set Denom[0] = &quot;Hundred&quot;
    set Denom[1] = &quot;Thousand&quot;
    set Denom[2] = &quot;Million&quot;
    set Denom[3] = &quot;Billion&quot;
endfunction

endlibrary</i>


Int2FormattedString
JASS:
library Int2FormattedString
    // converts an integer into a formatted string (163192161 -&gt; &quot;163,192,161&quot;)

//! textmacro Int2FormattedString_Config
    public string DELIMITER = &quot;,&quot;
    public integer DELIMIT_PLACES = 3
//! endtextmacro

globals
    //! runtextmacro Int2FormattedString_Config()
endglobals

function Int2FormattedString takes integer i returns string
    local string s = I2S(IAbsBJ(i))
    local string ret = &quot;&quot;
    local integer len = StringLength(s)
    local integer max = len/DELIMIT_PLACES         // how many delimiters we&#039;ll need
    local integer extra = len-(max)*DELIMIT_PLACES // mod(len,3), to find how many digits will be left
    local integer n = 0
    local string neg = &quot;&quot;
    if (i&gt;-1000 and i&lt;1000) then
        return I2S(i)
    endif
    if (i&lt;0) then
        set neg=&quot;-&quot;
    endif
    loop
        set ret = SubString(s, len-DELIMIT_PLACES, len)+ret
        set len = len-DELIMIT_PLACES
        set n = n+1
        exitwhen (n==max)
        set ret = DELIMITER+ret
    endloop
    if (extra&gt;0) then
        return neg+SubString(s, 0, extra)+DELIMITER+ret
    endif
    return neg+ret
endfunction

endlibrary

Real2FormattedString
JASS:
library Real2FormattedString requires Int2FormattedString, StringFind, StringReverse
    // converts a real into a formatted string (163192.161 -&gt; &quot;163,192.161&quot;)
    // will trim trailing zeroes, unless you do a &quot;set Real2FormattedString_TRIM_ZEROES = false&quot; before the call
    // just remember to set it back <img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" class="smilie smilie--sprite smilie--sprite7" alt=":p" title="Stick Out Tongue    :p" loading="lazy" data-shortname=":p" /> (if you like...)

//! textmacro Real2FormattedString_Config
    public string DELIMITER = &quot;,&quot;
    public string DECIMAL = &quot;.&quot;
    public boolean TRIM_ZEROES = true // will trim trailing zeroes
//! endtextmacro

globals
    //! runtextmacro Real2FormattedString_Config()
endglobals

function Real2FormattedString takes real r returns string
    local string sr = R2S(r)
    local integer pos = StringFind(sr, &quot;.&quot;)
    local string dec = DECIMAL+SubString(sr, pos+1, StringLength(sr))
    local integer i = R2I(r)
    local string s = I2S(IAbsBJ(i))
    local string ret = &quot;&quot;
    local integer len = StringLength(s)
    local integer max = len/3         // how many delimiters we&#039;ll need
    local integer extra = len-(max)*3 // mod(len,3), to find how many digits will be left
    local integer n = 0
    local string neg = &quot;&quot;
    if (i&gt;-1000 and i&lt;1000) then
        return I2S(i)
    endif
    if (i&lt;0) then
        set neg=&quot;-&quot;
    endif
    loop
        set ret = SubString(s, len-3, len)+ret
        set len = len-3
        set n = n+1
        exitwhen (n==max)
        set ret = DELIMITER+ret
    endloop
    if (extra&gt;0) then
        set ret = neg+SubString(s, 0, extra)+DELIMITER+ret
    else
        set ret = neg+ret
    endif
    if (TRIM_ZEROES==true) then
        set dec = StringReverse(dec)
        loop
            exitwhen (SubString(dec, 0, 1)!=&quot;0&quot;)
            set dec = SubString(dec, 1, StringLength(dec))
        endloop
        set dec = StringReverse(dec)+&quot;0&quot;
    endif
    return ret+dec
endfunction

endlibrary

StringFind
JASS:
library StringFind
    // finds a string (&#039;needle&#039;) within a string (&#039;haystack&#039;) and returns it&#039;s position
    // returns -1 if not found
    
function StringFind takes string haystack, string needle returns integer
    local integer i = StringLength(haystack)
    local integer j = StringLength(needle)
    local integer n = 0
    loop
        exitwhen (n&gt;i-j)
        if (SubString(haystack, n, n+j)==needle) then
            return n
        endif
        set n = n+1
    endloop
    return -1
endfunction

endlibrary

StringReplace
JASS:
library StringReplace
    // attempts to find any and all instances of &#039;toreplace&#039; in &#039;s&#039; and replaces with &#039;replacer&#039;
function StringReplaceAll takes string s, string toreplace, string replacer returns string
    local integer i = StringLength(s)
    local integer j = StringLength(toreplace)
    local integer n = 0
    if (toreplace==replacer) then
        return s
    endif
    loop
        exitwhen (n&gt;i-j)
        if (SubString(s, n, n+j)==toreplace) then
            set s = SubString(s, 0, n)+replacer+SubString(s, n+j, i)
            set i = StringLength(s)
        else
            set n = n+1
        endif
    endloop
    return s
endfunction

    // same as above, but will only replace the first instance
function StringReplace takes string s, string toreplace, string replacer returns string
    local integer i = StringLength(s)
    local integer j = StringLength(toreplace)
    local integer n = 0
    if (toreplace==replacer) then
        return s
    endif
    loop
        exitwhen (n&gt;i-j)
        if (SubString(s, n, n+j)==toreplace) then
            set s = SubString(s, 0, n)+replacer+SubString(s, n+j, i)
            exitwhen (true)
        else
            set n = n+1
        endif
    endloop
    return s
endfunction

endlibrary

StringStrip
JASS:
library StringStrip
    
    // attemps to find any and all instances of &#039;tostrip&#039; inside &#039;s&#039;
    // if found, they will be removed
function StringStripAll takes string s, string tostrip returns string
    local integer i = StringLength(s)
    local integer j = StringLength(tostrip)
    local integer n = 0
    loop
        exitwhen (n&gt;i-j)
        if (SubString(s, n, n+j)==tostrip) then
            set s = SubString(s, 0, n)+SubString(s, n+j, i)
            set i = StringLength(s)
        else
            set n = n+1
        endif
    endloop
    return s
endfunction

    // same as above, but will only remove the first instance
function StringStrip takes string s, string tostrip returns string
    local integer i = StringLength(s)
    local integer j = StringLength(tostrip)
    local integer n = 0
    loop
        exitwhen (n&gt;i-j)
        if (SubString(s, n, n+j)==tostrip) then
            set s = SubString(s, 0, n)+SubString(s, n+j, i)
            exitwhen (true)
        else
            set n = n+1
        endif
    endloop
    return s
endfunction
endlibrary

StringSwap
JASS:
library StringSwap requires StringFind, StringReplace
    // attempts to swap occurrances of string &#039;swap1&#039; with &#039;swap2&#039; within a string (&#039;s&#039;)
    
function StringSwap takes string s, string swap1, string swap2 returns string
    local string token1 = &quot;~!*^%@&quot;
    local string token2 = &quot;^$&amp;#~*&quot;
    loop
        exitwhen (StringFind(s, swap1)==-1 or StringFind(s, swap2)==-1)
        set s = StringReplace(s, swap1, token1)
        set s = StringReplace(s, swap2, token2)
    endloop    
    set s = StringReplaceAll(s, token1, swap2)
    set s = StringReplaceAll(s, token2, swap1)
    return s
endfunction

endlibrary

StringReverse
JASS:
library StringReverse
    // reverses a string
    
function StringReverse takes string s returns string
    local integer i = StringLength(s)
    local string s2 = &quot;&quot;
    loop
        exitwhen (i==0)
        set s2 = s2 + SubString(s, i-1, i)
        set i = i-1
    endloop
    return s2
endfunction

endlibrary

StringReverseColorSafe
JASS:
library StringReverseColorSafe requires StringReverse, StringReplace, StringFind
    // reverses a string as normal, but preservers color codes
    // all color codes must be in the proper format &quot;|cff00ff00&quot; with a closing tag &quot;|r&quot;
    
function StringReverseColorSafe takes string s returns string
    local integer i = 0
    local integer j = 0
    local string ccode = &quot;&quot;
    loop
        set i = StringFind(s, &quot;|c&quot;)
        set j = StringFind(s, &quot;|r&quot;)
        exitwhen (i==-1 or j==-1)
        set ccode=SubString(s, i, i+10)
        set s = StringReplace(s, ccode, &quot;r|&quot;)
        set s = StringReplace(s, &quot;|r&quot;, StringReverse(ccode))
    endloop
    return StringReverse(s)
endfunction

endlibrary
Note: The reason why the config globals are in textmacros was that I planned on having the config at the top of the inclusive library, while the globals were free to be located at the top of their respective functions.

So, please give feedback :thup:
 

Nestharus

o-o
Reaction score
84
Note: The reason why the config globals are in textmacros was that I planned on having the config at the top of the inclusive library, while the globals were free to be located at the top of their respective functions.

I do that too : D, I just put my config stuff in a sep trigger : P.

However, because you are doing these string functions, you should add in a base conversion utility =). Wouldn't be that hard, I've been meaning to do it myself, but you've already done so much o-o.

Just put hex in this ; P.
JASS:
function convert takes string value, string base1, string base2 returns string


What could it be used for? I don't know, easy and fast save/load? Then people could just add in their own little securities and quirks, and use their own bases.

And then you should also do sequencing =). You apply a key to something and it sequences it with the key o-o.

JASS:
function sequence takes string key, string value returns string
function disarray takes string key, string value returns string


Sequencing can be used for not only fun things, but useful things too ^^

More fun in the sun, do some keys for inserting values.

JASS:
function scatter takes string key, string insertion, string value returns string
function retrieve takes string key, string value returns string


What can this do? Let's say you wanted to scatter checksums about a string, or other keys about it, or w/e : O. Let's say you wanted to save values into a key, who knows : ).

What else? A string calculator = D.

JASS:
function add takes string base1, string base2, string value1, string value2 returns string

//etc


Oh well, +rep : D
 

Nestharus

o-o
Reaction score
84
Ok, so the base key for wc3 is 255, that is it uses 256 characters (0-255).

Thus, in your thing you might have a constant string that are these characters (string hash will probably do this, haven't really looked into it).

From here, users are able to apply any key (even of a dif size) and use that to change what their number or w/e looks like (translate it from one formatted base to another).

So, let's say you had this as a key-
0123456789

If a user was to do
call convert("0123456789", "10")

it'd obviously return 10 : P.

If they did like-
abcdefghijklmnopqrtuvwxyz

call convert("abcdefghijklmnopqrtuvwxyz", "10")

It'd return j =)

if they did call convert("", "10")

it'd just return your base key, so it'd shoot back a 10. In essence, that'd just hash the string (probably ;p).

From here, let's say they wanted to sequence their strings, so you use your base key again as the original sequence, and then they pass in their own keys. Any derivation from that means you move something from one position to another.

So, let's say that this is standard sequence-
0123456789

If they passed this in-
9123456780

This would happen-
call sequence("9123456780", "hello cat")

it'd return-
"tello cah"

thus sequencing it : )

Disarray returns it back to the base key given the key used to change it

Now what about scatter?
0123456789
362362

call scatter("362362", "cccccc", "0123456789")

this would return

"012ccc3cc456c789"

They are inserted at position 3, 6, 2, 3, 6, 2 =)

Obviously u'd probably want to do like 663322, but eh : P.

So what can this stuff be used for?

Convert allows easy save/load systems. They just use your conversion and put in their own bases, their own securities, etc =). Convert, done : D.

What can sequence be used for?

Sequence allows people to change how a string looks with a key. Let's say they want to make a save/load code harder to crack? Let's say they want to make a puzzle. Who knows? : o.

What can scatter be used for?

Scatter allows them to scatter values over a string. Let's say they wanted to scatter all their keys through out the string and then put the scatter key at the front. The keys would be array numbers probably, converted into some arbitrary constant base, probably their own default base =).

If you don't know how to do this stuff, it's really ok. I can do it, I've just been busy doing lots of other stuff ^^. I'll get around it... I already have sequencing and conversion done anyways : |... the code is somewhere o-o. I just saw you were working on string functions and I thought, hey cool, maybe they can make these 3 things too ^^, this way whenever someone comes along saying pwz make me an uber save/load system that'll save my thingies, everyone can just point them to ur string functions and say "go make ur own" =).
 

Renendaru

(Evol)ution is nothing without love.
Reaction score
309
Add ColorInString. It colors all letters of value inputted, and returns said string.
 
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

      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