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
Int2WordString
Int2FormattedString
Real2FormattedString
StringFind
StringReplace
StringStrip
StringSwap
StringReverse
StringReverseColorSafe
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:
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>0) then
set r = Denom[denom]+COMMA+SPACE+r
endif
set r = temp+SPACE+r
endif
set denom = denom+1
exitwhen (i<=0)
endloop
return neg+r
endfunction
private function init takes nothing returns nothing
set OnesTeens[0] = "Zero"
set OnesTeens[1] = "One"
set OnesTeens[2] = "Two"
set OnesTeens[3] = "Three"
set OnesTeens[4] = "Four"
set OnesTeens[5] = "Five"
set OnesTeens[6] = "Six"
set OnesTeens[7] = "Seven"
set OnesTeens[8] = "Eight"
set OnesTeens[9] = "Nine"
set OnesTeens[10] = "Ten"
set OnesTeens[11] = "Eleven"
set OnesTeens[12] = "Twelve"
set OnesTeens[13] = "Thirteen"
set OnesTeens[14] = "Fourteen"
set OnesTeens[15] = "Fifteen"
set OnesTeens[16] = "Sixteen"
set OnesTeens[17] = "Seventeen"
set OnesTeens[18] = "Eighteen"
set OnesTeens[19] = "Nineteen"
set Tens[0] = "Twenty"
set Tens[1] = "Thirty"
set Tens[2] = "Forty"
set Tens[3] = "Fifty"
set Tens[4] = "Sixty"
set Tens[5] = "Seventy"
set Tens[6] = "Eighty"
set Tens[7] = "Ninety"
set Denom[0] = "Hundred"
set Denom[1] = "Thousand"
set Denom[2] = "Million"
set Denom[3] = "Billion"
endfunction
endlibrary</i>
Int2FormattedString
JASS:
library Int2FormattedString
// converts an integer into a formatted string (163192161 -> "163,192,161")
//! textmacro Int2FormattedString_Config
public string DELIMITER = ","
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 = ""
local integer len = StringLength(s)
local integer max = len/DELIMIT_PLACES // how many delimiters we'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 = ""
if (i>-1000 and i<1000) then
return I2S(i)
endif
if (i<0) then
set neg="-"
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>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 -> "163,192.161")
// will trim trailing zeroes, unless you do a "set Real2FormattedString_TRIM_ZEROES = false" 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 = ","
public string DECIMAL = "."
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, ".")
local string dec = DECIMAL+SubString(sr, pos+1, StringLength(sr))
local integer i = R2I(r)
local string s = I2S(IAbsBJ(i))
local string ret = ""
local integer len = StringLength(s)
local integer max = len/3 // how many delimiters we'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 = ""
if (i>-1000 and i<1000) then
return I2S(i)
endif
if (i<0) then
set neg="-"
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>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)!="0")
set dec = SubString(dec, 1, StringLength(dec))
endloop
set dec = StringReverse(dec)+"0"
endif
return ret+dec
endfunction
endlibrary
StringFind
JASS:
library StringFind
// finds a string ('needle') within a string ('haystack') and returns it'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>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 'toreplace' in 's' and replaces with 'replacer'
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>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>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 'tostrip' inside 's'
// 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>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>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 'swap1' with 'swap2' within a string ('s')
function StringSwap takes string s, string swap1, string swap2 returns string
local string token1 = "~!*^%@"
local string token2 = "^$&#~*"
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
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 "|cff00ff00" with a closing tag "|r"
function StringReverseColorSafe takes string s returns string
local integer i = 0
local integer j = 0
local string ccode = ""
loop
set i = StringFind(s, "|c")
set j = StringFind(s, "|r")
exitwhen (i==-1 or j==-1)
set ccode=SubString(s, i, i+10)
set s = StringReplace(s, ccode, "r|")
set s = StringReplace(s, "|r", StringReverse(ccode))
endloop
return StringReverse(s)
endfunction
endlibrary
So, please give feedback :thup: