OpenLayers OpenLayers

Ticket #1133 (closed feature: fixed)

Opened 10 months ago

Last modified 9 months ago

Format a string given a string template and some context.

Reported by: tschaub Assigned to:
Priority: minor Milestone: 2.6 Release
Component: general Version: 2.5
Keywords: Cc:
State:

Description

Sounds like the SLD and text label work could use some string templating.

How about an OpenLayers.String.format function?

This function could have a signature like:

/*{String}*/ OpenLayers.String.format(/*{String}*/ template, /*{Object}*/ context);

No idea if this addresses all needs, but I'll attach a proposed patch.

Attachments

string_format_improved.diff (2.1 kB) - added by ahocevar on 11/09/07 17:38:32.
Improved version of the original patch. Allows to represent "${" by "${${" and handles "}" without preceding "${" correctly.
string_format.patch (3.3 kB) - added by tschaub on 11/09/07 19:08:19.
process a templated string
string_format.2.patch (3.3 kB) - added by ahocevar on 11/13/07 20:52:55.
Fixed tests, are now ok in FF, IE and Opera

Change History

(follow-up: ↓ 2 ) 11/09/07 15:27:34 changed by tschaub

and I had my head on backwards, that "$\{" doesn't work as advertised in the doc string - so that's an open question: "how do you represent the string "${"?

(in reply to: ↑ 1 ) 11/09/07 17:37:34 changed by ahocevar

Replying to tschaub:

and I had my head on backwards, that "$\{" doesn't work as advertised in the doc string - so that's an open question: "how do you represent the string "${"?

I think that optimized performance is crucial to this function, so I would propose to represent "${" by "${${". I created a patch for this, which also handles the "}" character without a preceding "${" correctly. Performance is improved by starting the loop at 1 instead of 0 - the first token never needs to be replaced.

11/09/07 17:38:32 changed by ahocevar

  • attachment string_format_improved.diff added.

Improved version of the original patch. Allows to represent "${" by "${${" and handles "}" without preceding "${" correctly.

11/09/07 19:08:19 changed by tschaub

  • attachment string_format.patch added.

process a templated string

(follow-up: ↓ 4 ) 11/09/07 19:15:39 changed by tschaub

  • keywords set to review.

That's nice.

I made a few other changes. Ignore any items that start with "}" (last == 0). Also, variable assignment is cheap, array index is expensive - so I set item = tokens[i]. Now you do two array lookups instead of four on successful match (didn't test performance here).

Finally, I didn't get the bit about adding "}" on the end (line 122 in your patch). Please set me straight if I missed something. Better yet, add a new test case.

The tests are pretty simple, but I think they're enough to get this in - if someone reviews in IE (can't remember if undefined.toString() == "undefined" there too).

(in reply to: ↑ 3 ) 11/13/07 20:50:27 changed by ahocevar

Replying to tschaub:

I made a few other changes. Ignore any items that start with "}" (last == 0). Also, variable assignment is cheap, array index is expensive - so I set item = tokens[i]. Now you do two array lookups instead of four on successful match (didn't test performance here).

Thanks for that information. I was not aware that array index access is expensive.

Finally, I didn't get the bit about adding "}" on the end (line 122 in your patch). Please set me straight if I missed something. Better yet, add a new test case.

The whole block you are referring to is never called, and to be honest I do not remember why I added it. I added two unchanged strings that I thought that would need this, but it was simply a mistake.

The tests are pretty simple, but I think they're enough to get this in - if someone reviews in IE (can't remember if undefined.toString() == "undefined" there too).

I ran the tests in IE, and they failed. It seems IE does not support "delete". So I had to move the undefined test above the window context test. Now the tests are all passed.

I attached a new patch with fixed tests. Everything is fine now.

11/13/07 20:52:55 changed by ahocevar

  • attachment string_format.2.patch added.

Fixed tests, are now ok in FF, IE and Opera

12/01/07 08:23:23 changed by crschmidt

  • keywords deleted.
  • status changed from new to closed.
  • resolution set to fixed.

Committed in r5317. Thanks!