- Joined
- Sep 29, 2014
- Messages
- 49
Since I posted about Tips & Tricks - Using Abilities as Data Reference for Triggering, I feel it is only right to share some functions to help the implementation of such system.
While some of the functions below can be found in various other libraries, I would like to highlight two which are extremely related to PRAT:
While some of the functions below can be found in various other libraries, I would like to highlight two which are extremely related to PRAT:
- strIndexer() Note: Bribe has already informed me that this a similar functionality exists in other library
- This function will treat string as array separated by a user-determined separator.
- removeWith()
- This function will remove all string within or outside a user-determined separators.
- The aim of this function is to allow users to add-in comments in their string data. Especially useful for PRAT since strings filled with only ASCII code can be confusing.
JASS:
library moStrLib /* v1.1 by Mastermind_OverRide
**********************B**********************B**********************B**********************B
*
* function strSearch(mString, searchString, occurenceNumber)
* returns position of the 'occurence Number'nd searched string
* negative occurenceNumber will search backwards
* occurenceNumber = 0 will return the count of the searchString
*
* function strSplit(mString, splitString, occurenceNumber, includeSplitString, otherSide)
* returns string BEFORE the 'occurence Number'nd splitString, AFTER if otherSide = true
*
* function strIndexer(mString, separator, index)
* gets the string located at index separated by "separator"
* e.g. "Thompshire, Konge, Ban'orak, Jay" with "," separator, will return Thompshire at index 0, Ban'orak at index 2, etc
*
* function replaceSubStr(mString, oriString, newString, replaceCount, startIndex)
* replaces "replaceCount"s oriString with newString
* replaceCount = 0 replaces all, negative replaces from last occurence, positive normal behaviour
* searches for oriString from startIndex. on negative replaceCount, start index counts from behind
*
* function removeSubStr(mString, unwantedString, removeCount, startIndex)
* just calling replaceSubStr with empty string for newString
*
* function replaceAllSubStr(mString, oriString, newString)
* replaces all occurence of oriString with newString
*
* function removeAllSubStr(mString, unwantedString)
* just calling replaceAllSubStr with empty string for newString
*
* function removeAllColors(mString, unwantedString)
* removes all color code definition in string
*
* function cleanAdjacentSpaces(mString)
* removee extra spaces that are adjacent to each other
* e.g. "Prince Arthas Menethil" -> "Prince Arthas Menethil"
*
* //Added in v1.1
* function removeWith(mString, sStart, sEnd, within, include, addExtraPer)
* remove all character between instances of sStart and sEnd
* on within = false, remove all outside
* addExtraPer will append another string that can be used as separator as within=false, will tend to remove these
* include will include sStart and sEnd on the resulting string
* e.g.
* "belv (Boots of Quel'Thalas), bspd (Boots of Speed), afac (Alleria's Flute of Accuracy), clsd (Cloak of Shadows)"
* sStart=" (", sEnd=")", within = true; "belv, bspd, afac, clsd"
* sStart="(", sEnd=")", within = true; "belv , bspd , afac , clsd "
* sStart="(", sEnd=")", within = true; "belv , bspd , afac , clsd "
* sStart="(", sEnd=")", within = false; "Boots of Quel'ThalasBoots of SpeedAlleria's Flute of AccuracyCloak of Shadows"
* sStart="(", sEnd=")", within = false, addExtraPer=", "; "Boots of Quel'Thalas, Boots of Speed, Alleria's Flute of Accuracy, Cloak of Shadows"
*
**********************B**********************B**********************B**********************B*/
public function strSearch takes string mString, string searchString, integer occurenceNumber, integer startIndex returns integer
local string tmpStr = ""
local integer mLength = StringLength(mString)
local integer sLength = StringLength(searchString)
local integer count = 0
local integer i = 0
if occurenceNumber < 0 then
set i = mLength - startIndex
set occurenceNumber = -occurenceNumber
loop
exitwhen i - sLength < 0
set tmpStr = SubString(mString, i - sLength, i)
if tmpStr == searchString then
set count = count + 1
if count == occurenceNumber then
return i - sLength
endif
endif
set i = i - 1
endloop
else
set i = 0 + startIndex
loop
exitwhen i + sLength > mLength
set tmpStr = SubString(mString, i, i + sLength)
if tmpStr == searchString then
set count = count + 1
if count == occurenceNumber then
return i
endif
endif
set i = i + 1
endloop
endif
//Return number of searchString afterloop or -1 indicating no substring
if occurenceNumber == 0 then
return count
else
return -1
endif
endfunction
public function strSplit takes string mString, string splitString, integer occurenceNumber, boolean includeSplitString, boolean otherSide returns string
local integer tmpInt = strSearch(mString, splitString, occurenceNumber, 0)
if otherSide then
if includeSplitString then
return SubString(mString, tmpInt, StringLength(mString))
else
return SubString(mString, tmpInt + StringLength(splitString), StringLength(mString))
endif
else
if includeSplitString then
return SubString(mString, 0, tmpInt + StringLength(splitString))
else
return SubString(mString, 0, tmpInt)
endif
endif
endfunction
public function strIndexer takes string mString, string separator, integer index returns string
local string tmpStr = ""
local integer mLength = StringLength(mString)
local integer sLength = StringLength(separator)
local integer count = 0
local integer lastIndex = 0
local integer i = 0
loop
exitwhen i + sLength > mLength
set tmpStr = SubString(mString, i, i + sLength)
if tmpStr == separator or i == mLength then
if count == index then
return SubString(mString, lastIndex, i)
endif
set count = count + 1
set i = i + sLength
set lastIndex = i
else
set i = i + 1
endif
endloop
return ""
endfunction
public function replaceSubStr takes string mString, string oriString, string newString, integer replaceCount, integer startIndex returns string
local string tmpStr = ""
local string finalString = ""
local integer mLength = StringLength(mString)
local integer oLength = StringLength(oriString)
local integer sLength = StringLength(newString)
local integer lastIndex = 0
local integer i = 0
local integer count = 0
local integer maxCount = 0
if replaceCount < 0 then
set i = mLength - startIndex
set maxCount = -replaceCount
loop
exitwhen i - sLength < 0
set tmpStr = SubString(mString, i - oLength, i)
//i == 0 to immediately append the rest of the string at the end
if tmpStr == oriString or i == 0 then
//Appending to the front instead
set finalString = newString + SubString(mString, i, lastIndex) + finalString
//Skip oLength characters to speed up loop
set lastIndex = i - oLength
set i = lastIndex
set count = count + 1
if count == maxCount then
set finalString = newString + SubString(mString, 0, lastIndex) + finalString
exitwhen true
endif
else
set i = i - 1
endif
endloop
else
set i = 0 + startIndex
if replaceCount == 0 then
set maxCount = StringLength(mString)
else
set maxCount = replaceCount
endif
loop
exitwhen i > mLength
set tmpStr = SubString(mString, i, i + oLength)
//i == mLength to immediately append the rest of the string at the end
if tmpStr == oriString or i == mLength then
set finalString = finalString + SubString(mString, lastIndex, i) + newString
//Skip oLength characters to speed up loop
set lastIndex = i + oLength
set i = lastIndex
set count = count + 1
if count == maxCount then
set finalString = finalString + SubString(mString, lastIndex, mLength) + newString
exitwhen true
endif
else
set i = i + 1
endif
endloop
endif
return finalString
endfunction
public function removeSubStr takes string mString, string unwantedString, integer removeCount, integer startIndex returns string
return replaceSubStr(mString, unwantedString, "", removeCount, startIndex)
endfunction
public function replaceAllSubStr takes string mString, string oriString, string newString returns string
return replaceSubStr(mString, oriString, newString, 0, 0)
endfunction
public function removeAllSubStr takes string mString, string unwantedString returns string
return replaceAllSubStr(mString, unwantedString, "")
endfunction
public function removeAllColors takes string mString returns string
local string tmpStr = ""
local string finalString = ""
local integer mLength = StringLength(mString)
local integer lastIndex = 0
local integer i = 0
loop
exitwhen i > mLength
set tmpStr = SubString(mString, i, i + 2)
//String Case to ensure that both |c and |C are detected
if StringCase(tmpStr, false) == "|c" or i == mLength then
set finalString = finalString + SubString(mString, lastIndex, i)
set lastIndex = i + 10
set i = lastIndex
else
if tmpStr == "|r" or i == mLength then
set finalString = finalString + SubString(mString, lastIndex, i)
set lastIndex = i + 2
set i = lastIndex
else
set i = i + 1
endif
endif
endloop
return finalString
endfunction
public function cleanAdjacentSpaces takes string mString returns string
//This function could realistically remove any adjacent duplicates, but I see little reason to use it outside of spaces
local string tmpStr = ""
local string finalString = ""
local integer mLength = StringLength(mString)
local integer lastIndex = 0
local integer i = 0
local boolean inSearch = false
loop
exitwhen i > mLength
set tmpStr = SubString(mString, i, i + 1)
//Check first space
if (tmpStr == " " and inSearch == false) or i == mLength then
if inSearch == true then
set lastIndex = i
endif
set inSearch = true
set finalString = finalString + SubString(mString, lastIndex, i + 1)
endif
//Check if next few letters are also spaces
if tmpStr != " " and inSearch == true then
//Tell to stop searching if it is not a space
set inSearch = false
set lastIndex = i
endif
set i = i + 1
endloop
return finalString
endfunction
//Added in v1.1
public function removeWith takes string mString, string sStart, string sEnd, boolean within, boolean include, string addExtraPer returns string
local string tmpStr = ""
local string finalString = ""
local integer mLength = StringLength(mString)
local integer sStaLength = StringLength(sStart)
local integer sEndLength = StringLength(sEnd)
local integer i = 0
local integer lastIndex = 0
local boolean inSearch = false
if within then
set sStaLength = StringLength(sStart)
set sEndLength = StringLength(sEnd)
else
set tmpStr = sStart
set sStart = sEnd
set sEnd = tmpStr
set sStaLength = StringLength(sStart)
set sEndLength = StringLength(sEnd)
set inSearch = true
endif
loop
exitwhen i > mLength
if not inSearch then
set tmpStr = SubString(mString, i, i + sStaLength)
if tmpStr == sStart or i == mLength then
set inSearch = true
if include then
set finalString = finalString + SubString(mString, lastIndex, i + sStaLength)
else
set finalString = finalString + SubString(mString, lastIndex, i)
endif
if i != mLength -1 then
set finalString = finalString + addExtraPer
endif
endif
else
set tmpStr = SubString(mString, i, i + sEndLength)
if tmpStr == sEnd or i == mLength then
set inSearch = false
if include then
set lastIndex = i
else
set lastIndex = i + sEndLength
endif
endif
endif
set i = i + 1
endloop
return finalString
endfunction
endlibrary
This one is a bonus, just some stuffs I used to make some cinematic texts adaptable to a changing context.
Can never be perfect, but enough to perform a convincing enough job I think.
Can never be perfect, but enough to perform a convincing enough job I think.
JASS:
library moTextLib requires moStrLib /* v1.0.1 by Mastermind_OverRide
**********************B**********************B**********************B**********************B
*
* function textPoss(mainString)
* returns possessive form, checks is last letter is "s", e.g. Kael -> Kael's
*
* function textPlural(mainString)
* returns possessive form, e.g. Footman -> Footmen, etc
*
* function textDemonym(mainString)
* returns e.g. Orc -> Orcish, Night Elf -> Night Elven, etc
*
* function autoArticulations(mainString)
* fixes "a"s and "an"s in text contextually
* //v1.0.1 - Added checkSpeceNull to prevent affecting unrelated texts ending with "a" or "an"
*
* function textDeleteSpace(mainString, last)
* removes, first or last space. best done after moStrLib_cleanAdjacentSpaces
* do nothing if not exist
*
* function changeCaseFirst(mainString, upCase)
* change the of only the first letter, upCase true for uppercase, flase for lowercase
*
**********************B**********************B**********************B**********************B*/
//Private Functions
private function getFirst takes string mString, integer length returns string
local integer tmpInt = StringLength(mString)
return StringCase(SubString(mString, 0, length), false)
endfunction
private function getLast takes string mString, integer length returns string
local integer tmpInt = StringLength(mString)
return StringCase(SubString(mString, tmpInt - length, tmpInt), false)
endfunction
private function beforeLast takes string mString, integer length, integer fromLast returns string
local integer tmpInt = StringLength(mString)
set tmpInt = tmpInt - (fromLast + 1)
return StringCase(SubString(mString, tmpInt - length, tmpInt), false)
endfunction
private function cutFirst takes string mString, integer length returns string
return SubString(mString, length, StringLength(mString))
endfunction
private function cutLast takes string mString, integer length returns string
local integer tmpInt = StringLength(mString)
return SubString(mString, 0, tmpInt - length)
endfunction
private function vowelCheck takes string mString returns boolean
local string tmpStr = StringCase(mString, false)
if StringLength(tmpStr) > 1 then
set tmpStr = getFirst(tmpStr, 1)
endif
if tmpStr == "a" then
return true
endif
if tmpStr == "e" then
return true
endif
if tmpStr == "i" then
return true
endif
if tmpStr == "o" then
return true
endif
if tmpStr == "u" then
return true
endif
return false
endfunction
//Public Functions
public function textPoss takes string mString returns string
//General Patterns
if getLast(mString, 1) == "s" then
return mString + "'"
endif
return mString + "'s"
endfunction
public function textPlural takes string mString returns string
local boolean tmpBool = false
if StringLength(mString) > 1 then
//Special Patterns
if getLast(mString, 5) == "mouse" or getLast(mString, 5) == "louse" then
return cutLast(mString, 4) + "ice"
endif
if getLast(mString, 3) == "die" then
return cutLast(mString, 1) + "ce"
endif
if getLast(mString, 4) == "foot" then
return cutLast(mString, 4) + moStrLib_replaceAllSubStr(getLast(mString, 4), "o", "e")
endif
if getLast(mString, 5) == "tooth" or getLast(mString, 5) == "goose" then
return cutLast(mString, 5) + moStrLib_replaceAllSubStr(getLast(mString, 5), "o", "e")
endif
//General Patterns
//-- Rifleman -> Riflemen
if getLast(mString, 3) == "man" then
return cutLast(mString, 2) + "en"
endif
//-- Leaf -> Leaves
if getLast(mString, 1) == "f" or getLast(mString, 2) == "e" then
return cutLast(mString, 1) + "ves"
endif
//-- Fungus -> Fungi / Necropolis -> Necropoli / Magus -> Magi
set tmpBool = false
set tmpBool = tmpBool or getLast(mString, 2) == "is"
set tmpBool = tmpBool or getLast(mString, 2) == "os"
set tmpBool = tmpBool or getLast(mString, 2) == "us"
if tmpBool then
return cutLast(mString, 2) + "i"
endif
//-- Huntress -> Huntresses
set tmpBool = false
set tmpBool = tmpBool or getLast(mString, 1) == "s"
set tmpBool = tmpBool or getLast(mString, 1) == "x"
set tmpBool = tmpBool or getLast(mString, 1) == "o"
set tmpBool = tmpBool or getLast(mString, 1) == "z"
set tmpBool = tmpBool or getLast(mString, 2) == "ch"
set tmpBool = tmpBool or getLast(mString, 2) == "sh"
if tmpBool then
return mString + "es"
endif
endif
//Normal
return mString + "s"
endfunction
public function textDemonym takes string mString returns string
local boolean tmpBool = false
//-- Human -> Human
if getLast(mString, 3) == "man" then
return mString
endif
//-- Dragon -> Draconic
if getLast(mString, 6) == "dragon" then
return cutLast(mString, 3) + "conic"
endif
//-- Demon -> Demonic
if getLast(mString, 5) == "demon" or getLast(mString, 5) == "titan" then
return mString + "ic"
endif
//-- Skeleton -> Skeletal
if getLast(mString, 8) == "skeleton" then
return cutLast(mString, 2) + "al"
endif
//-- Orc -> Orcish
if getLast(mString, 3) == "orc" then
return mString + "ish"
endif
//-- Elf -> Elves
if getLast(mString, 1) == "f" then
return cutLast(mString, 1) + "ven"
endif
//-- Plagueland -> Plaguelander
if getLast(mString, 4) == "land" then
return mString + "er"
endif
//Ended with 'S'
//-- Darnassus -> Darnassian
set tmpBool = getLast(mString, 1) == "s"
set tmpBool = tmpBool and vowelCheck(beforeLast(mString, 1, 0))
set tmpBool = tmpBool and beforeLast(mString, 1, 1) == "s"
if tmpBool then
return cutLast(mString, 2) + "ian"
endif
//-- Glineas -> Gilnean / Kul Tiras -> Kul Tiran
set tmpBool = getLast(mString, 1) == "s"
set tmpBool = tmpBool and vowelCheck(beforeLast(mString, 1, 0))
set tmpBool = tmpBool and vowelCheck(beforeLast(mString, 1, 1))
if tmpBool then
return cutLast(mString, 1) + "n"
endif
//-- Quel'Thalas -> Thalassian / Kul Tiras -> Tirassian
set tmpBool = getLast(mString, 1) == "s"
if getLast(mString, 1) == "s" then
return mString + "sian"
endif
//Strom -> Stromic //
if getLast(mString, 1) == "m" then
return mString + "ic"
endif
//Normal
if getLast(mString, 1) == "a" then
return mString + "n"
endif
if getLast(mString, 1) == "e" then
return cutLast(mString, 1) + "ian"
endif
if vowelCheck(getLast(mString, 1)) then
return mString + "an"
endif
return mString + "ian"
endfunction
private function checkSpaceNull takes string cString returns boolean
if StringLength(cString) <= 0 then
return true
endif
if cString == " " then
return true
endif
return false
endfunction
public function autoArticulations takes string mString returns string
local string tmpStr = ""
local string prevChar = ""
local string finalString = ""
local boolean tmpBool = false
local integer mLength = StringLength(mString)
local integer i = 0
local integer lastIndex = 0
loop
exitwhen i > mLength
set tmpStr = StringCase(SubString(mString, i, i + 2), false)
set prevChar = StringCase(SubString(mString, i - 1, i), false)
//v1.0.1 - Added checkSpeceNull to prevent affecting unrelated texts ending with "a" or "an"
if tmpStr == "a " and checkSpaceNull(prevChar) then
set tmpStr = SubString(mString, i + 2, i + 3)
if vowelCheck(tmpStr) then
set finalString = finalString + SubString(mString, lastIndex, i + 1) + "n "
set lastIndex = i + 2
endif
endif
set tmpStr = StringCase(SubString(mString, i, i + 3), false)
if tmpStr == "an " and checkSpaceNull(prevChar) then
set tmpStr = SubString(mString, i + 3, i + 4)
if not vowelCheck(tmpStr) then
set finalString = finalString + SubString(mString, lastIndex, i + 1)
set lastIndex = i + 2
endif
endif
if i == mLength then
set finalString = finalString + SubString(mString, lastIndex, i)
endif
set i = i + 1
endloop
return finalString
endfunction
public function textDeleteSpace takes string mString, boolean last returns string
if last == true then
if getFirst(mString, 1) == " " then
return cutFirst(mString, 1)
endif
else
if getLast(mString, 1) == " " then
return cutLast(mString, 1)
endif
endif
return mString
endfunction
public function changeCaseFirst takes string mString, boolean upCase returns string
return StringCase(getFirst(mString, 1), upCase) + cutFirst(mString, 1)
endfunction
endlibrary
Last edited: