- Joined
- Aug 4, 2006
- Messages
- 357
Introduction
This is a vJASS library of useful string functions made by me (MaskedPoptart). Since the total length of this post is quite long, let me start out by explaining the most useful feature of these functions. This feature uses the FindIndex, NumOccurances, and RemoveString functions.
Most Useful Feature – Function Testing in Chat
Let's face it: most of us never finish a map. Whether it's being too ambitious, not having enough time, or being stumped by bugs, something always comes up. Wouldn’t it be nice if there was an easy, quick way to test and debug your functions? Well, now there is! With simply one line of code, you can allow a function to be tested through in-game chat messages with whatever parameters you want (as long as your parameters can be converted from a string). This allows you to see the results of each function call immediately after you press the enter key! For example, we can allow ourselves to use the CreateUnit function in-game simply by writing
Now we can test the CreateUnit function with any player, unit-type id, and coordinates without having to switch back and forth between world editor and warcraft 3, waiting for the map to load each time.
So how exactly can I do it?
Functions are separated into four categories: Important, Color, Chat Events, and Debugging.
Dates are MM/DD/YY. Times are GMT-10:00
1.00 (06/24/09, 2:16am)
This is a vJASS library of useful string functions made by me (MaskedPoptart). Since the total length of this post is quite long, let me start out by explaining the most useful feature of these functions. This feature uses the FindIndex, NumOccurances, and RemoveString functions.
Most Useful Feature – Function Testing in Chat
Let's face it: most of us never finish a map. Whether it's being too ambitious, not having enough time, or being stumped by bugs, something always comes up. Wouldn’t it be nice if there was an easy, quick way to test and debug your functions? Well, now there is! With simply one line of code, you can allow a function to be tested through in-game chat messages with whatever parameters you want (as long as your parameters can be converted from a string). This allows you to see the results of each function call immediately after you press the enter key! For example, we can allow ourselves to use the CreateUnit function in-game simply by writing
JASS:
//! runtextmacro FunctionTesting5("CreateUnit", "player", "S2Player", "integer", "S2RawCode", "real", "S2R", "real", "S2R", "real", "S2R")
So how exactly can I do it?
- Create a new trigger window (Ctrl-T) with name: "String", convert it to custom text, and delete the whole contents.
- Copy all the code in the hidden "String Functions" JASS block below, and paste it into your "String" trigger.
- Repeat step 1 with name: "Function Testing"
- Copy all the code in the hidden "FunctionTesting" JASS block below, and paste it into your "Function Testing" trigger.
- At the bottom, run the text macro that matches the number of parameters your function has. For example, //! runtextmacro FunctionTesting4 for functions with 4 parameters. You will have to input the functionName, variable type of the first parameter (i.e. player), string to type of first parameter (i.e. S2Player), variable type of the second parameter (i.e. real), string to type of second parameter (i.e. S2R), etc. Make sure your function can be accessed from this trigger.
- Repeat step 5 for all functions you want to test.
- Once you start up warcraft 3, you should receive a message like "created textmacro for CreateUnit(player,integer,real,real,real)" for each function you textmacro'd.
- Call your function like so: "CreateUnit( p0, hfoo, 0, 0, 0) ". Make sure you do not put quotes (" ") or apostrophes (' '). If you ran the textmacro correctly, your function should run. If nothing happens, make sure you put calls to BJDebugMsg inside your function.
JASS:
//made by MaskedPoptart
//! textmacro FunctionTesting0 takes functionName
scope $functionName$Testing initializer Init
//===========================================================================
private function Actions takes nothing returns nothing
call $functionName$()
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$()", true )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$()")
endfunction
endscope
//! endtextmacro
//! textmacro FunctionTesting1 takes functionName, type1, S2type1
scope $functionName$Testing initializer Init
globals
private string enteredString
private integer esLength
private boolean condition1
private boolean condition2
private integer tempInt1
private $type1$ $type1$Param1
endglobals
//========================================================================
private function Conditions takes nothing returns boolean
set enteredString = GetEventPlayerChatString()
set condition1 = NumOccurances(enteredString, ",") == 0
if(not(condition1))then
return false
endif
set condition2 = FindIndex(enteredString, ")") == StringLength(enteredString)-1
return condition2
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
set enteredString = RemoveString(enteredString, " ")
set esLength = StringLength(enteredString)
set tempInt1 = FindIndex(enteredString, "(") + 1
set $type1$Param1 = $S2type1$(SubString(enteredString, tempInt1, esLength-1))
call $functionName$($type1$Param1)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$(", false )
call TriggerAddCondition( chat_event_trig, Condition( function Conditions ) )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$($type1$)")
endfunction
endscope
//! endtextmacro
//! textmacro FunctionTesting2 takes functionName, type1, S2type1, type2, S2type2
scope $functionName$Testing initializer Init
globals
private string enteredString
private integer esLength
private boolean condition1
private boolean condition2
private integer tempInt1
private integer tempInt2
private $type1$ $type1$Param1
private $type2$ $type2$Param2
endglobals
//========================================================================
private function Conditions takes nothing returns boolean
set enteredString = GetEventPlayerChatString()
set condition1 = NumOccurances(enteredString, ",") == 1
if(not(condition1))then
return false
endif
set condition2 = FindIndex(enteredString, ")") == StringLength(enteredString)-1
return condition2
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
set enteredString = RemoveString(enteredString, " ")
set esLength = StringLength(enteredString)
set tempInt1 = FindIndex(enteredString, "(") + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type1$Param1 = $S2type1$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = esLength - 1
set $type2$Param2 = $S2type2$(SubString(enteredString, tempInt1, tempInt2))
call $functionName$($type1$Param1, $type2$Param2)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$(", false )
call TriggerAddCondition( chat_event_trig, Condition( function Conditions ) )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$($type1$,$type2$)")
endfunction
endscope
//! endtextmacro
//! textmacro FunctionTesting3 takes functionName, type1, S2type1, type2, S2type2, type3, S2type3
scope $functionName$Testing initializer Init
globals
private string enteredString
private integer esLength
private boolean condition1
private boolean condition2
private integer tempInt1
private integer tempInt2
private $type1$ $type1$Param1
private $type2$ $type2$Param2
private $type3$ $type3$Param3
endglobals
//========================================================================
private function Conditions takes nothing returns boolean
set enteredString = GetEventPlayerChatString()
set condition1 = NumOccurances(enteredString, ",") == 2
if(not(condition1))then
return false
endif
set condition2 = FindIndex(enteredString, ")") == StringLength(enteredString)-1
return condition2
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
set enteredString = RemoveString(enteredString, " ")
set esLength = StringLength(enteredString)
set tempInt1 = FindIndex(enteredString, "(") + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type1$Param1 = $S2type1$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type2$Param2 = $S2type2$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = esLength - 1
set $type3$Param3 = $S2type3$(SubString(enteredString, tempInt1, tempInt2))
call $functionName$($type1$Param1, $type2$Param2, $type3$Param3)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$(", false )
call TriggerAddCondition( chat_event_trig, Condition( function Conditions ) )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$($type1$,$type2$,$type3$)")
endfunction
endscope
//! endtextmacro
//! textmacro FunctionTesting4 takes functionName, type1, S2type1, type2, S2type2, type3, S2type3, type4, S2type4
scope $functionName$Testing initializer Init
globals
private string enteredString
private integer esLength
private boolean condition1
private boolean condition2
private integer tempInt1
private integer tempInt2
private $type1$ $type1$Param1
private $type2$ $type2$Param2
private $type3$ $type3$Param3
private $type4$ $type4$Param4
endglobals
//========================================================================
private function Conditions takes nothing returns boolean
set enteredString = GetEventPlayerChatString()
set condition1 = NumOccurances(enteredString, ",") == 3
if(not(condition1))then
return false
endif
set condition2 = FindIndex(enteredString, ")") == StringLength(enteredString)-1
return condition2
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
set enteredString = RemoveString(enteredString, " ")
set esLength = StringLength(enteredString)
set tempInt1 = FindIndex(enteredString, "(") + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type1$Param1 = $S2type1$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type2$Param2 = $S2type2$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type3$Param3 = $S2type3$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = esLength - 1
set $type4$Param4 = $S2type4$(SubString(enteredString, tempInt1, tempInt2))
call $functionName$($type1$Param1, $type2$Param2, $type3$Param3, $type4$Param4)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$(", false )
call TriggerAddCondition( chat_event_trig, Condition( function Conditions ) )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$($type1$,$type2$,$type3$,$type4$)")
endfunction
endscope
//! endtextmacro
//! textmacro FunctionTesting5 takes functionName, type1, S2type1, type2, S2type2, type3, S2type3, type4, S2type4, type5, S2type5
scope $functionName$Testing initializer Init
globals
private string enteredString
private integer esLength
private boolean condition1
private boolean condition2
private integer tempInt1
private integer tempInt2
private $type1$ $type1$Param1
private $type2$ $type2$Param2
private $type3$ $type3$Param3
private $type4$ $type4$Param4
private $type5$ $type5$Param5
endglobals
//========================================================================
private function Conditions takes nothing returns boolean
set enteredString = GetEventPlayerChatString()
set condition1 = NumOccurances(enteredString, ",") == 4
if(not(condition1))then
return false
endif
set condition2 = FindIndex(enteredString, ")") == StringLength(enteredString)-1
return condition2
endfunction
//===========================================================================
private function Actions takes nothing returns nothing
set enteredString = RemoveString(enteredString, " ")
set esLength = StringLength(enteredString)
set tempInt1 = FindIndex(enteredString, "(") + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type1$Param1 = $S2type1$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type2$Param2 = $S2type2$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type3$Param3 = $S2type3$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = FindIndexFrom(enteredString, ",", tempInt1)
set $type4$Param4 = $S2type4$(SubString(enteredString, tempInt1, tempInt2))
set tempInt1 = tempInt2 + 1
set tempInt2 = esLength - 1
set $type5$Param5 = $S2type5$(SubString(enteredString, tempInt1, tempInt2))
call $functionName$($type1$Param1, $type2$Param2, $type3$Param3, $type4$Param4, $type5$Param5)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local trigger chat_event_trig = CreateTrigger( )
call TriggerRegisterPlayerChatEvent( chat_event_trig, Player(0), "$functionName$(", false )
call TriggerAddCondition( chat_event_trig, Condition( function Conditions ) )
call TriggerAddAction( chat_event_trig, function Actions )
debug call BJDebugMsg("created textmacro for $functionName$($type1$,$type2$,$type3$,$type4$,$type5$)")
endfunction
endscope
//! endtextmacro
//--------------------------------------------------------------------
//------------------{ NO PARAMETER FUNCTIONS }----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting0( "functionName" )
//--------------------------------------------------------------------
//------------------{ 1 PARAMETER FUNCTIONS }-----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting1( "functionName", "type1", "S2type1" )
//--------------------------------------------------------------------
//------------------{ 2 PARAMETER FUNCTIONS }-----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting2( "functionName", "type1", "S2type1", "type2", "S2type2")
//--------------------------------------------------------------------
//------------------{ 3 PARAMETER FUNCTIONS }-----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting3( "functionName", "type1", "S2type1", "type2", "S2type2", "type3", "S2type3")
//--------------------------------------------------------------------
//------------------{ 4 PARAMETER FUNCTIONS }-----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting4( "functionName", "type1", "S2type1", "type2", "S2type2", "type3", "S2type3", "type4", "S2type4")
//--------------------------------------------------------------------
//------------------{ 5 PARAMETER FUNCTIONS }-----------------------
//--------------------------------------------------------------------
// runtextmacro FunctionTesting5( "functionName", "type1", "S2type1", "type2", "S2type2", "type3", "S2type3", "type4", "S2type4", "type5", "S2type5")
The String Library
- Important functions are used within the library, so they need to be at the top.
- Color functions are anything having to do with creating/removing color codes.
- Chat Event functions help with handling chat events, or convert strings to other things.
- Debugging functions convert things to strings, so you can put them in BJDebugMsg calls.
JASS:
//--------------------IMPORTANT FUNCTIONS------------------------
function FindIndexFrom takes string mainString, string stringToFind, integer startingIndex returns integer
//returns the first (leftmost) index of "stringToFind" in "mainString"
// after "startingIndex." (Starts looking at starting index)
//returns -1 if "stringToFind" is not found
//example:
local string fatty = "To be or not to be"
local string shorty = "be"
local integer index = FindIndexFrom(fatty, shorty, 4)
call BJDebugMsg(index) //16
function FindIndex takes string mainString, string stringToFind returns integer
//returns the first (leftmost) index of "stringToFind" in "mainString"
//returns -1 if "stringToFind" is not found
function FindLastIndexFrom takes string mainString, string stringToFind, integer startingIndex returns integer
//returns the last (rightmost) index of "stringToFind" in "mainString"
// before "startingIndex." (Looks backwards from startingIndex)
//returns -1 if "stringToFind" is not found
//example:
local string big = "Am I a hot bot, or not?"
local string small = "ot"
local integer index = FindLastIndexFrom(big, small, 19)
call BJDebugMsg(index) //12
function FindLastIndex takes string mainString, string stringToFind returns integer
//returns the last (rightmost) index of "stringToFind" in "mainString"
//returns -1 if "stringToFind" is not found
//-----------------------COLOR FUNCTIONS ------------------------
function PlayerColor2ColorString takes playercolor pc returns string
//takes a playercolor handle (does anyone actually use those?) and
// returns the corresponding color string (same form as above)
function GetPlayerColorString takes player p returns string
//takes a player and returns the color string associated with him/her
//in the form of "|cffRRGGBB"
function GetPlayerNameColored takes player p returns string
//returns the player’s name with appropriate color
//example:
GetPlayerNameColored(Player(0)) //returns "|cffff0303World Edit|r"
function RemoveColorCode takes string mainString returns string
//takes in a string encased in a color code and returns that string
// without its color code
//example:
RemoveColorCode("|cffea9b33HELLO|r") //returns "HELLO"
function IBase2S takes integer base10Num, integer newBase returns string
//converts an integer (base 10) and a base into a string of numbers in
// that base. Returns the string.
//does not support bases over 16. does not support negative numbers.
//example:
IBase2S(134, 2) //returns "10000110". (converts 134 to a base 2 (binary) string)
IBase2S(134, 16) //returns "86". (converts 134 to a base 16 (hexadecimal) string)
IBase2S(212, 16) //returns "d4". (converts 212 to a base 16 (hexadecimal) string)
function SBase2I takes string oldBaseString, integer oldBase returns integer
//does the opposite of the previous function.
//does not support bases over 16. does not support negative numbers.
//example:
SBase2I("10000110", 2) //returns 134. (converts "10000110", a binary string, to a decimal number)
SBase2I("86", 16) //returns 134. (converts "86", a hexadecimal string, to a decimal number)
SBase2I("e3", 16) //returns 227. (converts "e3", a hexadecimal string, to a decimal number)
function ConvertRGBToColorString takes integer red, integer green, integer blue returns string
//creates a color code prefix using the three integers
//0 is darkest, 255 is brightest for each color
//example:
ConvertRBGToColorString(200,0,200) //returns "|cffc800c8"
function GetColoredString takes string str, integer r, integer g, integer b returns string
//creates a color code prefix (using previous function) and returns
// str with the full color code, including ending
//example:
GetColoredString("wee", 255, 192, 0) //returns "|cffffc000wee|r"
//----------------------CHAT EVENT FUNCTIONS------------------------------
function RemoveString takes string mainString, string toRemove returns string
//removes all occurances of toRemove from mainString
//returns the new string
//example:
local string silly = "aaaaheaaaalaloaa"
set silly = RemoveString(silly, "a")
call BJDebugMsg(silly) //"hello"
function NumOccurances takes string mainString, string stringToFind returns integer
//returns the number of times stringToFind is found in mainString
//example:
local string str = "wefejwejdsfowe fj"
local string str2 = "we"
local integer num = NumOccurances(str, str2)
call BJDebugMsg(num) //3
function S2B takes string word returns Boolean
//converts "true" into true and "false" into false
//returns false if the string is not valid
function S2Player takes string word returns player
//takes a string in the form of "pX" where X is an integer
// representing a player id
//returns Player(X)
//example:
S2Player("p5") //returns Player(5)
function S2RawCode takes string str returns integer
//takes a string and converts it into a rawcode integer
//A rawcode is any integer that you normally put ' ' around,
// such as a unit-type id or an ability id.
//example:
S2RawCode("hfoo") //returns 'hfoo' (unit id of Footman)
S2RawCode("Aply") //returns 'Aply' (ability id of Polymorph)
S2RawCode("pghe") //returns 'pghe' (item id of Potion of Greater Healing)
S2RawCode("@$$#") //returns '@$$#' (custom rawcode)
//-----------------------DEBUG FUNCTIONS-------------------------
function B2S takes boolean bool returns string
//returns "true" or "false"
//example usage:
local boolean b = IsPlayerAlly(Player(0), Player(1))
call BJDebugMsg("Player 0 allied with Player 1? "+B2S(b))
function Player2S takes player p returns string
//takes a player and returns a string in the form of
//"Player(X)" where X is the player id.
//example usage:
call BJDebugMsg("Triggering Player is: "+Player2S(GetTriggerPlayer()))
function Unit2S takes unit u returns string
//returns the unit type name _ the unit's unique number.
//uses GetHandleId() from beta patch 1.23b (available in Westfall server)
//so it only works in 1.23b+. Replace GetHandleId with the H2I function
// if you use 1.23 or lower.
//example usage:
local unit yourMom = CreateUnit(Player(0), 'hpea', 0, 0, 0)
call BJDebugMsg(Unit2S(yourMom)) //Peasant_92
function RawCode2S takes string str returns integer
//takes a rawcode integer and converts it into a string
//A rawcode is any integer that you normally put ' ' around,
// such as an item-type id or a buff id.
//example:
RawCode2S('hfoo') //returns "hfoo" (human unit id of Footman)
RawCode2S('Aply') //returns "Aply" (ability id of Polymorph)
RawCode2S('pghe') //returns "pghe" (item id of Potion of Greater Healing)
RawCode2S(1969709426) //returns "ugar" (undead unit id of Gargoyle)
//WARNING: do not input any integers that do not correspond to raw codes
// or the function will not work properly.
JASS:
library String initializer Init
//String functions v1.04
//made by MaskedPoptart
//--------------------IMPORTANT FUNCTIONS------------------------
function FindIndexFrom takes string mainString, string stringToFind, integer startingIndex returns integer
local integer msLength = StringLength(mainString)
local integer sfLength = StringLength(stringToFind)
local integer i = startingIndex
if(sfLength > msLength or i < 0)then
return -1
endif
loop
exitwhen i > msLength - sfLength
if(SubString(mainString, i, i+sfLength) == stringToFind)then
return i
endif
set i = i + 1
endloop
return -1
endfunction
function FindIndex takes string mainString, string stringToFind returns integer
return FindIndexFrom(mainString, stringToFind, 0)
endfunction
function FindLastIndexFrom takes string mainString, string stringToFind, integer startingIndex returns integer
local integer msLength = StringLength(mainString)
local integer sfLength = StringLength(stringToFind)
local integer i = msLength-sfLength
if(startingIndex < i)then
set i = startingIndex
endif
if(sfLength > msLength)then
return -1
endif
loop
exitwhen i < 0
if(SubString(mainString, i, i+sfLength) == stringToFind)then
return i
endif
set i = i - 1
endloop
return -1
endfunction
function FindLastIndex takes string mainString, string stringToFind returns integer
return FindLastIndexFrom(mainString, stringToFind, 2147483647)
endfunction
//-----------------------COLOR FUNCTIONS ------------------------
globals
private playercolor array PLAYER_COLORS
private string array PLAYER_COLOR_STRINGS
private constant string HEX_CHARS = "0123456789abcdef"
private string COLOR_ENDING = "|r"
endglobals
private function Init takes nothing returns nothing
local integer i = 0
loop
exitwhen i >= 12
set PLAYER_COLORS[i] = ConvertPlayerColor(i)
set i = i + 1
endloop
set PLAYER_COLOR_STRINGS[0] = "|cffff0303"
set PLAYER_COLOR_STRINGS[1] = "|cff0042ff"
set PLAYER_COLOR_STRINGS[2] = "|cff1ce6b9"
set PLAYER_COLOR_STRINGS[3] = "|cff540081"
set PLAYER_COLOR_STRINGS[4] = "|cfffffc01"
set PLAYER_COLOR_STRINGS[5] = "|cfffe8a0e"
set PLAYER_COLOR_STRINGS[6] = "|cff20c000"
set PLAYER_COLOR_STRINGS[7] = "|cffe55bb0"
set PLAYER_COLOR_STRINGS[8] = "|cff959697"
set PLAYER_COLOR_STRINGS[9] = "|cff7ebff1"
set PLAYER_COLOR_STRINGS[10] = "|cff106246"
set PLAYER_COLOR_STRINGS[11] = "|cff4e2a04"
set PLAYER_COLOR_STRINGS[12] = "|cff272727"
set PLAYER_COLOR_STRINGS[13] = "|cff272727"
set PLAYER_COLOR_STRINGS[14] = "|cff272727"
endfunction
function PlayerColor2ColorString takes playercolor pc returns string
local integer i = 0
loop
exitwhen i >= 12
if(PLAYER_COLORS[i] == pc)then
return PLAYER_COLOR_STRINGS[i]
endif
set i = i + 1
endloop
return PLAYER_COLOR_STRINGS[12]
endfunction
function GetPlayerColorString takes player p returns string
return PlayerColor2ColorString(GetPlayerColor(p))
endfunction
function GetPlayerNameColored takes player p returns string
return GetPlayerColorString(p)+GetPlayerName(p)+COLOR_ENDING
endfunction
//please use responsibly
function RemoveColorCode takes string mainString returns string
local integer msLength = StringLength(mainString)
if(msLength<12)then
return mainString
endif
return SubString(mainString, 10, msLength-2)
endfunction
function IBase2S takes integer base10Num, integer newBase returns string
local integer placeNum //number at current place
local string newBaseString = ""
loop
exitwhen base10Num == 0
set placeNum = ModuloInteger(base10Num, newBase)
set newBaseString = SubString(HEX_CHARS, placeNum, placeNum+1) + newBaseString
set base10Num = base10Num / newBase
endloop
if(newBaseString == "")then
return "0"
endif
return newBaseString
endfunction
function SBase2I takes string oldBaseString, integer oldBase returns integer
local integer base10Num = 0
local integer placeNum //number at current place
local integer placeIndex = 0 //index of current place. 0 = one's place, etc.
local integer i = StringLength(oldBaseString)-1
loop
exitwhen i < 0
set placeNum = FindLastIndexFrom(HEX_CHARS, SubString(oldBaseString, i, i+1), oldBase-1)
set base10Num = base10Num + placeNum*R2I(Pow(oldBase, placeIndex))
set placeIndex = placeIndex + 1
set i = i - 1
endloop
return base10Num
endfunction
function ConvertRGBToColorString takes integer red, integer green, integer blue returns string
local string RR
local string GG
local string BB
if(red>255)then
set red = 255
endif
if(green>255)then
set green = 255
endif
if(blue>255)then
set blue = 255
endif
set RR = IBase2S(red, 16)
set GG = IBase2S(green, 16)
set BB = IBase2S(blue, 16)
if(StringLength(RR)<2)then
set RR = "0"+RR
endif
if(StringLength(GG)<2)then
set GG = "0"+GG
endif
if(StringLength(BB)<2)then
set BB = "0"+BB
endif
return "|cff"+RR+GG+BB
endfunction
function GetColoredString takes string str, integer r, integer g, integer b returns string
return ConvertRGBToColorString(r,g,b)+str+COLOR_ENDING
endfunction
//----------------------CHAT EVENT FUNCTIONS------------------------------
function RemoveString takes string mainString, string toRemove returns string
local integer i = 0
local string currentString
local integer msLength = StringLength(mainString)
local integer trLength = StringLength(toRemove)
if(trLength > msLength)then
return mainString
endif
loop
exitwhen i+trLength > msLength
set currentString = SubString(mainString, i, i+trLength)
if(currentString == toRemove)then
if(i+trLength <= msLength)then
set mainString = SubString(mainString, 0, i)+SubString(mainString, i+trLength, msLength)
else
set mainString = SubString(mainString, 0, i)
endif
set i = i - trLength
endif
set i = i + 1
endloop
return mainString
endfunction
function NumOccurances takes string mainString, string stringToFind returns integer
local integer count = 0
local integer i = 0
local integer msLength = StringLength(mainString)
local integer sfLength = StringLength(stringToFind)
loop
exitwhen (i+sfLength) > msLength
if(SubString(mainString, i, i+sfLength) == stringToFind)then
set count = count + 1
endif
set i = i + 1
endloop
return count
endfunction
function S2B takes string word returns boolean
if(word == "true")then
return true
endif
return false
endfunction
function S2Player takes string word returns player
return Player(S2I(SubString(word, 1, StringLength(word))))
endfunction
globals
private integer MIN_RAW_CODE = ' ' //32
private string RAW_CHARS = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
endglobals
function S2RawCode takes string str returns integer
local integer rawCode = 0
local integer placeNum //number at current place
local integer placeIndex = 0 //index of current place. 0 = one's place, etc.
local integer i = StringLength(str)-1
loop
exitwhen i < 0
set placeNum = MIN_RAW_CODE + FindIndex(RAW_CHARS, SubString(str, i, i+1))
//the char at index 0 of RAW_CHARS has ASCII value 32, so we need to offset each FindIndex by 32.
set rawCode = rawCode + placeNum*R2I(Pow(256., placeIndex))
set placeIndex = placeIndex + 1
set i = i - 1
endloop
return rawCode
endfunction
//-----------------------DEBUG FUNCTIONS-------------------------
function B2S takes boolean bool returns string
if(bool)then
return "true"
endif
return "false"
endfunction
function Player2S takes player p returns string
return "Player("+I2S(GetPlayerId(p))+")"
endfunction
function Unit2S takes unit u returns string
return GetUnitName(u)+ "_"+I2S(GetHandleId(u)-0x100000)
endfunction
function RawCode2S takes integer rawCode returns string
local integer placeNum //number at current place
local string str = ""
if(rawCode < MIN_RAW_CODE)then
return str
endif
loop
exitwhen rawCode == 0
set placeNum = ModuloInteger(rawCode, 256) - MIN_RAW_CODE
set str = SubString(RAW_CHARS, placeNum, placeNum+1) + str
set rawCode = rawCode / 256
endloop
return str
endfunction
endlibrary
Dates are MM/DD/YY. Times are GMT-10:00
1.00 (06/24/09, 2:16am)
- initial release
- added 8 color-related functions
- added Player2S and S2Player
- updated documentation
- added example usage of chat event functions (11:23pm)
- added FindLastIndex
- changed names (I2HexString/HexString2I)
- improved S2Player
- improved Unit2S
- updated documentation, started changelog
- made kickass textmacros for testing functions through chat
- added FindIndexFrom, FindLastIndexFrom
- changed I2HexString/HexString2I to IBase2S, SBase2I
- added S2Id (also kickass), and Id2S
- moved Find functions to top of library, so they could be used by other functions
- reorganized original post
- a few other minor changes
- changed names (S2RawCode, RawCode2S)
- a few minor changes (SBase2I, S2RawCode, RawCode2S)
- improved S2RawCode, RawCode2S.
- they now work with any sequence of standard ASCII chars.
- fixed a bug with ConvertRGBToColorString
- IBase2S now returns "0" instead of ""
- Changed the name of "ConvertPlayerToColorString" to "GetPlayerColorString"
- GetPlayerColorString and GetPlayerNameColored now work even if players do
not have the default colors (i.e. if one team is all red, all their color strings will be red).
Last edited: