WordWrap. Faster than Split?

At lunchtime today I was discussing my WordWrap component with a fellow developer which - of course - led us into discussions as to the merit of the task and also how "optimal" we could make the underlying "wrapping" algorithm. When I started talking about my current algorithm we discovered that I'm doing several Split's and then speculated as to what the faster algorithm might be. We decided that it would be good to benchmark our final wrapping algorithm against the time it takes to perform a single Split() operation against the same input. It will be interesting to see how close we can get to Split() time.

The rules that the Wrap algorithm must implement are:

  • break on text at given intervals
  • where possible, don't break in the middle of a word (i.e.: favour breaking on spaces)
  • breaking character can be greater than one character (i.e.: could be a carriage return or a Html break tag)

Related Reading:
http://www.chilton.com/~jimw/wordwrap.html - An example of the complexity involved
http://www.outer-court.com/basic/echo/T1100.HTM - Using a single loop with a few checks thrown in.

No Comments