string.format()

Made a small change or new addition to the POL Core that makes a difference? You can post the changes here in .patch or .diff file format, for our Dev team to screen and apply to the SVN!

Moderator: POL Developer

Locked
User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

string.format()

Post by andenixa » Sun Mar 24, 2013 1:46 pm

Update version #2:

Format is now accepts formatting:

{:x} for hex representation
{:o} for octal
{:b} for binary

add "#" to get the notation preffix, i.e:

"{:x}".format(10) -> "a"
"{:#x}".format(10) -> "0xa"


string.format() - replaces formatting tags with string representation of a corresponding parameter.
example:

"You have {} gold coins".format(120) - "You have 120 gold coins"

Parameters are unpacked implicitly by their order:

"{} hits {} for {} of damage".format("John", "Bob", 120): "John hits Bob for 120 of damage"


However you can specify parameters implicitly:

"You hit {2} for {1} damage".format(120, "John Doe") - "You hit John Doe for 120 damage"

* Note that first parameter is 1

Format also allows accessing object members:

"Spell {1.spell_name} requires reagents: {1.reagents}".format(struct{spell_name:="Fire Wrath", "Ba, Bm, Ga"}) - "Spell Fire Wrath requires reagents: Ba, Bm, Ga"

Implicit members also work:

"{name} you hit level {level}".format(struct{name:="Jane Doe", level:=4}) - "Jane Doe you hit level 4"

You can also access character or item members the same way:

"{1.name}, your shield {2.desc} has {2.hp} hp".format(who, who.shield)" will print like "Admin, your shiled Buckler of Death has 150 hp"

Hope that helps. If you discover any bugs please report me ASAP.
Attachments
string_format_v2.patch
version 2
(7.19 KiB) Downloaded 59 times
string.format.patch
(5.11 KiB) Downloaded 83 times
Last edited by andenixa on Sat Aug 17, 2013 8:10 am, edited 1 time in total.

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Mon Apr 01, 2013 4:45 am

Since you asked for comments on this in your other patch thread I will offer some.

Let me say at the outset that I am, at best, a mediocre programmer. So you should take any comments I offer based on that fact.

Personally I'm not sure I would find any use for the function because it duplicates things that can be done currently with other methods. I think this function adds complexity where it isn't needed. Again, keep in mind my programming language experience is limited. It's quite possible that I am ignorant of other languages that have this functionality built-in and it might be quite useful for reasons that are not apparent to me right now. As I said in the beginning much if not all of the string.format() function can be done already in eScript via the use of arrays.

For example:

Code: Select all

var params := {"120", "John Doe"};
var msg := "You hit " + params[2] + " for " + CStr(params[1]) + " damage.";
I assume the syntax for string.format would look something like this:

Code: Select all

var msg := "You hit {2} for {1} damage".format(120, "John Doe")
If so then I don't see much difference between my method using arrays and the string.format() method.

I did not take into account that the string.format() method may save a few bytes in the source since I assume it will automatically convert numbers to their string equivalents. If it increases code execution efficiency that would certainly be a point in its favour.


As for referencing members of objects that can also be done using arrays.

Code: Select all

function build_msg(player)
var params := {player.name, player.hp};
var msg := params[1] + " you have " + CStr(params[2]) + " hit points.";
return msg;
endfunction
Perhaps I am overlooking something though.

I can see how extending the function to handle the formatting of numbers would be helpful.

"You have {1 '#,###'} {2} in your bank box.".format(1234, "arrow shafts") = "You have 1,234 arrow shafts in your bank box."
or
"{1} the objtype number of that item is {2 '0x'}.".format(who.name, item.objtype) = "Admin Fred the objtype number of that item is 0x2FCD."
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: string.format()

Post by andenixa » Mon Apr 01, 2013 5:54 am

The difference is that it allows separation of data and code, thus you could put a string to some config file, and then reuse it.
You can't achieve that with simple concatenation.

One of the uses I have for it is to form NPC speech in lines like:

speech "Face your doom {opponent}!"

You are absolutely right that the whole API I created could be replicated via escript, for example, that little snippet would replicate the basic behavior of format:

Code: Select all

var myList := {"John Doe", 140};
var myString := "You have {2} gold coins, {1}.";
foreach elem in myList
    while(myString[{"+_elem_iter+"}])
        myString[{"+_elem_iter+"}] := CStr(elem);
    endwhile
endforeach
However that beyond the skill of scripters who have daily jobs other than programming.
Thus I merely try to allow some flexibility out of the box.

That is a very good suggestion regarding formatting of numbers. Pol CStr() formats integers in somewhat odd manner I will be implementing that as soon as I find a good convention to comply to.

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Tue Apr 02, 2013 4:14 am

Don't misunderstand me. I can see how the application of the string.format method could make things simpler to code.

Perhaps my bias is because I have an aversion to learning new things. : ) I'm not sure where you live but in America we have an expression: "You can't teach an old dog new tricks." Just consider me an old dog. I don't like learning new "tricks". : )
That is a very good suggestion regarding formatting of numbers. Pol CStr() formats integers in somewhat odd manner I will be implementing that as soon as I find a good convention to comply to.
Yes. I had trouble coming up with a convention for my examples. I chose the single quote but that may not work in practice. I came up with a few more possible options for the format method.

An option to capitalize each word of a phrase.
A colour option for text.
A font option for text.

I'm pretty good at two things, criticizing and coming up with ways to give people more work. : )
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: string.format()

Post by andenixa » Tue Apr 02, 2013 4:27 pm

I once knew a senior Chinese women who told me all your life is exam so you have to prepare for it daily, meaning there are no old dogs :D But I can perfectly understand you thats why the whole API is completely optional.

I don't think you can color things in UO messages, rather than in Clilocs, and thats beyond the scope. Capitalization is a tough fruit specially if you think Unicode and Multi-language, though possible. I gave your notes a great considerations and will concoct something in the next patch.

Don't worry about criticizing thought, when it comes to Ultima Online even the fiercest criticism is scarce, because the topic ain't that popular anymore.
Anyway it doesn't come any close to what my players put me through daily.

Thank you for suggestions.

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Wed Apr 03, 2013 4:41 am

I don't think you can color things in UO messages, rather than in Clilocs, and thats beyond the scope. Capitalization is a tough fruit specially if you think Unicode and Multi-language, though possible. I gave your notes a great considerations and will concoct something in the next patch.
Yes. Now that you mention it I guess there isn't a way to add colour to a string, or font properties either. I guess that was a ridiculous idea. I can see how capitalization could be an issue when handling Unicode text. Well, just work on the number formatting then. In particular I'm interested in the ability to do numeric conversion. I would like the ability to convert to decimal, hexadecimal and binary.

0% denotes binary
0x denotes hexadecimal
0# denotes decimal

I think the 0% and 0x are "standard" for the respective number base systems but I don't know what the standard designator is for base ten.
Don't worry about criticizing thought, when it comes to Ultima Online even the fiercest criticism is scarce, because the topic ain't that popular anymore.
Anyway it doesn't come any close to what my players put me through daily.
Hahaha. Yes. I know what you mean. Right now my playerbase is almost zero but when I had a lot of players I used to get so much criticism from my them it made me want to .kill them and freeze their ghosts so they couldn't go to a healer.

It is sad that UO is fading from popularity. It is one of the best environments for true role play. The client is simple and easy to learn.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: string.format()

Post by andenixa » Thu Apr 04, 2013 9:38 am

I shall look into it, but I think I'd replicate some preexisting syntax like of a .Net or something, perhaps Python's.

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Fri Apr 05, 2013 11:28 pm

Thanks.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: string.format()

Post by andenixa » Sat Apr 13, 2013 12:36 am

I think I'll be using python syntax very close to what Yukiko has suggested. I also have an idea for extending arrays API with 2 more calls.

array.reject_by(membername, membervalue) : new Array
array.reject_by(membername) : new Array
array.collect_by(membername) : new Array
array.collect_by(membername, membervalue) : new Array

Dare to tell me this is ad hoc =)

What about EnumerateOnlineCharacters().reject_by("cmdlevel") ?=)

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Sat Apr 13, 2013 2:24 pm

Very cool idea with the array extension.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

User avatar
andenixa
Grandmaster Poster
Posts: 105
Joined: Tue Nov 09, 2010 1:33 am

Re: string.format()

Post by andenixa » Sun Apr 14, 2013 3:08 am

What with all this positive attitude all of a sudden=) Anyway its done now.

Yukiko
Distro Developer
Posts: 2505
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: string.format()

Post by Yukiko » Sun Apr 14, 2013 5:15 am

If you want me to I can probably think up some negative ideas about your array method extension. I'm pretty good at criticizing if I need to be. :)
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

Locked