?
Solved

PosEx routine not working in Delphi 2009 unicode

Posted on 2012-08-24
7
Medium Priority
?
1,449 Views
Last Modified: 2012-09-05
Incompatible types ansichar and char to the code line...

i:=strpos(@s[offset],@substr[1]);

Anyone can make this works in Delphi 2009?

Function PosEx(const SubStr, S: string; Offset: Cardinal): Integer;

var i : pchar;
begin
  if (offset<1) or (offset>length(s)) then
  begin
    Result := 0;
    Exit
  end;

  i:=strpos(@s[offset],@substr[1]);
  if i=nil then
    PosEx:=0
  else
    PosEx:=succ(i-pchar(s));
end;
0
Comment
Question by:DelphiEmil
7 Comments
 
LVL 4

Expert Comment

by:MichaelStaszewski
ID: 38331181
Check out Delphi's RTL PosEx defined in SysUtils.pas. Unless I'm misunderstanding, it seems you're trying to create a function that already exists and that does exactly what you're looking for.

In SysUtils.pas...

function PosEx(const SubStr, S: string; Offset: Integer): Integer;
0
 

Author Comment

by:DelphiEmil
ID: 38331241
Yes, but I get error in this code line, about string errors with @ sign.

i:=strpos(@s[offset],@substr[1]);
0
 
LVL 9

Expert Comment

by:rinfo
ID: 38332321
try  this instead
i:=strpos(Addr(s[offset]),Addr(substr[1]));
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 8

Accepted Solution

by:
lomo74 earned 1005 total points
ID: 38332665
the solution:
i:=strpos(pchar(@s[offset]),pchar(@substr[1]));

basically: the @ gets the address of the operand.
but then, the compiler is unable to decide which StrPos fits best:
function StrPos(const Str1: PAnsiChar; const Str2: PAnsiChar): PAnsiChar; overload;
function StrPos(const Str1: PWideChar; const Str2: PWideChar): PWideChar; overload;

which one to call? you pass two generic "pointers"; both pansichar and pwidechar are pointers.

casting the parameters to pchar (which on D2009 maps to pwidechar), the compiler has no more doubts: the correct call is the unicode version:
function StrPos(const Str1: PWideChar; const Str2: PWideChar): PWideChar; overload;
0
 

Author Comment

by:DelphiEmil
ID: 38332901
I got it I changed var i : pchar; to var i : pansichar; and it compiles now.
0
 
LVL 8

Expert Comment

by:lomo74
ID: 38333148
It will probably not work as expected. You are passing a string (unicode) to posex. You have to treat each character as unicode, NOT as ansi.
0
 
LVL 8

Expert Comment

by:lomo74
ID: 38335683
you are calling the ansi version of strpos IMHO.
this will obviously give wrong results on unicode strings.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question