Author Topic: Code Resources - Free Use  (Read 3083 times)

These are a couple of little resources I've created for helping to do certain things. I'll include a little bit of documentation here for each one, including the new functions they add and sample usage for these functions.

libstr.cs - String manipulation helper functions
Note: A list of functions and descriptions of them is also available in the comment block at the top of the file. Aside from this the code is by and large uncommented and may or may not be a clusterforget.
Quote
striReplace(%string,%replace,%with[,%startindex])
The default strReplace() function, but case-insensitive; for some reason this is missing by default.

getSubStrR(%string,%start,%end)
The default getSubStr() function, but instead of being based on a start index and length, this uses a start index and an end index.

setSubStr(%string,%start,%count,%insert)
Insert a string at a specified point in another string, replacing a given number of characters.

setSubStrR(%string,%start,%end,%insert)
Insert a string replacing a given range of another string (see getSubStr/getSubStrR).

strRev(%string)
Reverse a string. Who knows if you'll ever need it?

getStrBetween(%string,%prefix,%suffix,%index)
Get the %indexth instance of any strings occurring between a given prefix and suffix within the supplied string. (getStrBetween("[0][1][2][3]","[","]",1))

getLineCount(%string)
Returns the number of lines (separated by \n) in the given string.

getLine(%string,%index)
Returns the %indexth line of the given string.

strMatch(%string,%pattern)
Returns true if the given string matches the pattern. Patterns can contain wildcards (*) which match anything (including nothing).

striMatch(%string,%pattern)
strMatch, but case-insensitive.

typeOf(%variable[,%boolean])
Returns "float" if %variable is a number with decimal places, "integer" if %variable is a number with no decimal, or "string" if %variable does not appear to be a number. Good for validating user input.

LoopHandler.cs - Looping event handler (for "ticks")
Note: A brief reference is available in the comment block at the top of the file, and the code is commented with comments of dubious helpfulness.
Quote
The LoopHandler is an object, and thus can be instantiated using new ScriptObject(LoopHandler) or new ScriptObject(AnyName) { class=LoopHandler; }. Any number of LoopHandlers can exist separately from one another, though this shouldn't realistically be necessary for a single project.

Methods:
.addEvent(%id,%callback,%delay[,%initcallback])
Add a looping event. Every %delayms, %callback will be called with one argument. This argument is an object which is unique to this loop and handler, and can be used as a sort of 'memory' for loop states and the like. If a function named %initcallback exists, that function will be called before the loop is begun, with the memory object as the first and only argument.

.cancelEvent(%id)
Cancel a looping event, identified by the given ID string (used when registering the event). Its memory object will be deleted and all data pertaining to it removed from the loop handler.

.pause(%id) and .unpause(%id)
Pause or resume a given looping event. When it is resumed it will be resumed with the elapsed time since the last call accounted for; ie, pausing it halfway through its tick delay and then unpausing it later will only require it to complete the remaining half of its delay.

If the loop handler is deleted, all memory objects for its loops will be deleted with it, and all schedules canceled. The functions you supply for looping events must be console functions, not Class::Methods.

When you're using these resources, feel free to just drop them into your add-on as-is and use them. I don't require attribution, just do not claim you made them.


Thread note: This is in Coding Help instead of Modification Discussion as these are made to help other coders, and also because they are not stand-alone add-ons for use, thus discussion of them is in fact not discussion of a modification.

Oh my these are very useful
I've been looking for something like striReplace for a while

Great contribution for the newbies.

« Last Edit: December 05, 2011, 11:07:19 AM by Fluff-is-back »

Could you possibly make a function that gets a random letter between [letter1] and [letter2], like getRandom()?

Looks nice but I'm pretty sure getLine and getLineCount are default functions.



Could you possibly make a function that gets a random letter between [letter1] and [letter2], like getRandom()?
Code: [Select]
function randomLetter(%start,%end)
{
%letters = "abcdefghijklmnopqrstuvwyxz";

if(%start $= "")
%start = "a";
if(%end $= "")
%end = "z";

%pos1 = strPos(%letters,%start);
%pos2 = strPos(%letters,%end);

return getSubStr(%letters,getRandom(%pos1,%pos2),1);
}

Looks nice but I'm pretty sure getLine and getLineCount are default functions.


Code: [Select]
function randomLetter(%start,%end)
{
%letters = "abcdefghijklmnopqrstuvwyxz";

if(%start $= "")
%start = "a";
if(%end $= "")
%end = "z";

%pos1 = strPos(%letters,%start);
%pos2 = strPos(%letters,%end);

return getSubStr(%letters,getRandom(%pos1,%pos2),1);
}


Ah, thanks.