[Divunal-devel] StringUtilities
Phil Christensen
evil_ivan@yahoo.com
Wed, 11 Aug 1999 11:12:10 -0700 (PDT)
After many trials and tribulations, I've finished much of twisted.util.StringUtilities. This class, I think, is extremely useful. It has methods that emulate perl's split and join functions (allowing you to easily split a string into an array based on a delimiter and concatanate a String[] (actually, an array of Objects) into a single String, given a field separator)....There's also replaceInString which replaces all or some occurances of a sequence within a string with some other string.
Finally, and most importantly, it provides a mechanism to do variable substitution that is prespectives-compatible. For example.....
In my Smoking Room there is a component Class_Smokeable. It has several properties, namely strings that get displayed when a player does something with a child of this object:
-------------------------------------------------------------
string "subjectHearsLight" "You light $t ."
string "othersHearLight" "$m lights $t ."
string "alreadyLit" "$t is already lit."
string "subjectHearsPrepare" "You fill up $t with fine tobacco."
string "alreadyPrepared" "$t is already prepared."
string "emptyPipe" "$t is empty. You need to prepare it."
string "unlitPipe" "$t isn't lit!"
string "othersHearSmoke" "$m puffs away on $t ."
string "subjectHearsSmoke" "You puff away on $t ."
string "othersHearPrepare" "$m fills up $t with fine tobacco."
-------------------------------------------------------------
Then in the verb code for SmokePipe.java, this is all that's required (in this case, when a player "prepare"s the pipe.)
-------------------------------------------------------------
String[] vars = {"$m", "$t"};
Object[] subs = {d.subject(), d.directObject()};
Object[] othersHear = StringUtilities.substituteVariables(
d.directObject().getString("othersHearPrepare"),
vars, subs);
Object[] subjectHears = StringUtilities.substituteVariables(
d.directObject().getString("subjectHearsPrepare"),
vars, subs);
d.subject().topplace().tellAll(d.subject(), subjectHears, othersHear);
-------------------------------------------------------------
which results in:
-------------------------------------------------------------
> prepare pipe
You fill up carved bit pipe with fine tobacco.
-------------------------------------------------------------
This does reveal a change in the perspectives engine, previously, "the" was automatically inserted. Has this changed permanently? (Or worse yet, is it unchanged, meaning that's a bug on my part?)
Anyways, twisted.util.StringUtilities has been fully javadoc'd, so it should be pretty straightforward to use.
-phil<hr size=1><b>Do You Yahoo!?</b><br>Get your free @yahoo.com address at <a href="http://mail.yahoo.com/">Yahoo! Mail</a>.<br>