7

Delphi has a WrapText function:

function WrapText(const Line, BreakStr: string; const BreakChars: TSysCharSet; MaxCol: Integer): string;
function WrapText(const Line: string; MaxCol: Integer): string;

Now i need a version that handles WideStrings:

function WrapTextW(const Line: WideString; MaxCol: Integer): WideString;

Is any such function written somewhere already?

WARNING: Not every wide string character is 2-bytes

Which is why i'm afraid to write it

Update: Example of a character that takes more than 2-bytes to represent:

Capital Latin W with ring and cedilla

  • Bytes: 57 00 66 03 27 03
  • Rendered in Chrome 17: enter image description here
  • Rendered in Internet Explorer 9: enter image description here
  • Rendered in Notepad using Segoe UI: enter image description here
  • Rendered in Notepad using Consolas: enter image description here
  • Rendered in your browser in sans-serif font: W̧̊
  • Rendered in your browser in monospaced font: W̧̊
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • 2
    This will lead probably to copy the source from the Delphi 2009 up as the answer. +1 anyway – TLama Mar 14 '12 at 14:08
  • 5
    @TLama: That's why i tagged it as the last version of Delphi that didn't have better widestring support (to keep David off my back!) – Ian Boyd Mar 14 '12 at 14:14
  • 4
    Tnt component suit has `WideWrapText` in `TntSysUtils.pas` – kobik Mar 14 '12 at 14:21
  • @IanBoyd Yeah, no doubt this is really D5 but the D2007 solution will be the same!! ;-) – David Heffernan Mar 14 '12 at 14:51
  • 3
    *I have a dream* that one day I will wake up and my D5 monster application will transform itself magically to XE2... :/ – kobik Mar 14 '12 at 16:11
  • 1
    @DavidHeffernan Quiet you! Pretend, for the purposes of this question, that i'm in the "modern" age. :) – Ian Boyd Mar 14 '12 at 18:10
  • Oi vey, dude, if you're dealing with Unicode XML data and WideString types every day (as your questions suggest) you really need to ditch your ancient delphi version! – Warren P Mar 30 '12 at 16:40
  • @WarrenP Got a couple dozen thousand dollars so we can upgrade? – Ian Boyd Mar 30 '12 at 18:33
  • BTW, I just noticed your "WARNING: Not every wide string character is 2-bytes" - What makes you think that? this is false. – kobik May 01 '12 at 15:56
  • @kobik Look at the charater **W̧̧̧̧ͦ** (Capital Latin W with ring and cedilla). It's one "*character*". But in UTF-16 would be six bytes: `57 00 66 03 27 03` (or `U+0057 U+0366 U+0327`) If you were to blindly flip each UTF-16 code point: from `0057 0366 0327` to `0327 0366 0057` you would end up with invalid nonsense. – Ian Boyd May 01 '12 at 18:20
  • Interesting... I can see this "char" correctly only in FireFox. IE and Google Chrome shows `W` and some unknown gibberish. if I copy it to Notepad and save as Unicode I get 8 bytes (2 for Unicode header). so I guess `WideString` will hold 6 bytes for it (3 `WideChar`s). but this fact interesting as it is, dose not make a difference that `WideChar` is 2 bytes. I wonder how can you show this character in notepad for example... – kobik May 01 '12 at 21:12
  • @kobik Oh a `WideChar` is definitely two bytes, but not all *characters* fit in a single 2-byte `WideChar`. That's why the question has the warning. Chrome display's the character properly; Internet Explorer displays the cedilia correctly, but the ring is too far to the right) – Ian Boyd May 01 '12 at 21:29
  • 1
    so W + cedilla + ring represent a glyph. not a character. – kobik May 01 '12 at 21:51

1 Answers1

3

As mentioned by kobik, TNT UnicodeControls has a unit TntSysUtils which includes following function :

function WideWrapText(const Line, BreakStr: WideString; const BreakChars: TSysCharSet;
  MaxCol: Integer): WideString; overload;
function WideWrapText(const Line: WideString; MaxCol: Integer): WideString; overload;

Alternate download site is here.

LU RD
  • 34,438
  • 5
  • 88
  • 296