0

OS: Hungarian Windows (Windows 1250)

Under Delphi 6 Prof there is no WideStringPos, WideStringCopy, WideStringReplace...

But in an XML based project I need to use them.

Because that I tried to write "something like" these functions.

But I'm not sure they are working as I want...

Because Delphi converts the Wide to Ansi and reverse in the background, I cannot be sure that my code is safe from these side effects... :-)

The code is very primitive - I need the solution quickly...

 function WideStringCopy(WWhat : WideString; From, HowMany : integer) : WideString;
 var
     i : integer;
     l : integer;
     wc : WideChar;
 begin
     Result := '';

     if WWhat = ''
         then Exit;

     if (HowMany <= 0)
         then Exit;

     if  (From < 1)
         then From := 1;

     l := From + HowMany - 1;
     if l > Length(WWhat)
         then l := Length(WWhat);

     for i := From to l do begin
         wc := WWhat[i];
         Result := Result + wc;
     end;
 end;

 function WideStringPos(WWhere, WWhat : WideString) : integer;
 var
     wscomp : WideString;
     i : integer;
 begin
     Result := 0;
     for i := 1 to Length(WWhere) do begin
         wscomp := WideStringCopy(WWhere, i, LengtH(WWhat));
         if WideSameStr(wscomp, WWhat)
             then begin
                 Result := i;
                 Exit;
             end;
     end;
 end;

 function WideStringReplace(WWhere, WFrom, WTo : WideString) : WideString;
 var
     actpos : integer;
     wcomp : WideString;
     wc : WideChar;
 begin
     Result := '';
     actpos := 1;
     while actpos <= Length(WWhere) do begin
         wcomp := WideStringCopy(WWhere, actpos, Length(WFrom));
         if WideSameStr(wcomp, WFrom) then begin

             Result := Result + WTo;
             inc(actpos, Length(WFrom));

         end else begin

             wc := WWhere[actpos];
             Result := Result + wc;
             inc(actpos);

         end;
     end;
 end;

I have two questions about it:

  1. Do you see any piece of code that surely making bad result (converting the Wide to Ansi silently, and causing character loosing)?

  2. Do you know some character with I can test this code?

For example, chr(XXX) what is remaining when my converters are keeping the Wide rules, but loosing if I make wrong code...

Thanks for every info you will write...

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
durumdara
  • 3,411
  • 4
  • 43
  • 71
  • 4
    Try to look for the TNT Controls. They have all this stuff already done. Look for `WStrPos`, `WStrCopy` functions from `TntWideStrUtils.pas` and `WideStringReplace` from `TntSysUtils.pas`. – TLama Sep 12 '12 at 12:59
  • 3
    I don't see the point of `WideStringCopy`; the built-in `Copy` command already does the same thing (but *without* making unnecessary copies of all the intermediate strings). – Rob Kennedy Sep 12 '12 at 13:14
  • Jedi CodeLibrary is D6-compatible for now, has a LOT of WideString functions and has an XMl parser as well. Frankly, why re-invent the bycicle, instead of taking already tested code ? – Arioch 'The Sep 12 '12 at 13:46
  • If it's XML, wouldn't UTF-8 be more appropriate than UTF-16? – David Heffernan Sep 12 '12 at 13:48
  • @David if he uses something like MSXML ActiveX - then that conversion is done under the hood. MSXML exposes WideString interfaces API. – Arioch 'The Sep 12 '12 at 13:59
  • Is there a way you can avoid using wide strings internally, and only pass them in/out of wide APIs? (e.g. utf8everywhere.org) – Pavel Radzivilovsky Sep 12 '12 at 21:51

1 Answers1

0

Do you know some character with I can test this code?

Any codepage beyond Win1250 - for example Cyrillic Win1251, Greek, Hebrew - almost all letters there would be missed from 1250/1252

You can take Jedi CodeLibrary and use its locale conversion routines: make a string consisting of #128 till #255 in some encoding like aforementioned, convert it to Unicode from that codepage and then convert back from Unicode to Hungarian codepage.

function StringToWideStringEx(const S: AnsiString; CodePage: Word): WideString; function WideStringToStringEx(const WS: WideString; CodePage: Word): AnsiString;

Or in one call

function TranslateString(const S: AnsiString; CP1, CP2: Word): AnsiString;

Then look which chars failed to translate and turned into ReplacementCharacter.


However in JCL you'd have your Pos function and such ready to use. And XML parser. So why bother ?

Arioch 'The
  • 15,799
  • 35
  • 62