Jump to content
Mark Williams

SearchBuf is deprecated

Recommended Posts

I am using Searchbuf from SysUtils. I am getting the above warning. Often when the compiler warns that a function is deprecated it points you to a new function that performs the same or advanced role. Not in this case. Anyone know what if anything has replaced SearchBuf?

Share this post


Link to post

Sorry should have spotted that myself, although the help file in the StrUtils unit only refers to the functuion being deprecated for C++.

 

I am now getting an "Ambiguous overloaded call to SearchBuf" error.

 

I am calling SearchBuf in a functions declared as follows:


 

Function FindWordInText(Const Text:PAnsiChar; Const FindText:AnsiString; caseSens, WholeW: boolean; index:integer):Integer;
  var
    P:PAnsiChar;    
    Size:word;
    Options:TStringSearchOptions;
begin
 {irrelevant code removed}
  P := SearchBuf(text, Size , 0, index,
  FindText, options);
end;

After changing my reference to strUtils in uses to AnsiStrings this would not compile. Reason being that TStringSearchOptions is in strUtils. So I also added StrUtils back in to my uses after AnsiStrings. As soon as I did so, I get the ambiguous error.

 

The parameters passed into my function are correct for the SearchBuf function in AnsiStrings unit. I also get the same error if I add the AnsiStrings unit after StrUtils in my uses clause.

 

If I change my functions parameters as follows:

Function FindWordInText(Const Text:PWideChar; Const FindText:AnsiString; caseSens, WholeW: boolean; index:integer):Integer;
  var
    P:PWideChar;    
    Size:word;
    Options:TStringSearchOptions;
begin
  P := SearchBuf(text, Size , 0, index,
  FindText, options);
end;

It compiles and executes fine. Presumably, it is calling the StrUtils function. 

 

But why am I getting an ambiguous error with PAnsiChar as a parameter, but not PWideChar?

 

 

 

 

Share this post


Link to post

Fill in the unit name before the function name!

  • Thanks 1

Share this post


Link to post

Thanks to Stano the function now compiles. However, I cannot get the AnsiStrings function to work as I would expect ie in the same way as the strUtils unit.

 

The code is:

	{Ansitrings:}
   Function FindWordInText(Const Text:PAnsiChar; Const FindText:AnsiString; index:integer):Integer;
{StrUtils:}
	Function FindWordInText(Const Text:PWideChar; Const FindText:AnsiString; index:integer):Integer;
  var
	{AnsiStrings:}
    	P:AnsiChar;
	{StrUtils}
		P:PWideChar;
    Size:word;
    Options:TStringSearchOptions;
begin
  Size := length(text);
  //Buffer := StrAlloc(Size+1);

  Options:=[soDown];
  
  P := {System.AnsiStrings.}SearchBuf(text, Size , 0, index,
  FindText, options);


  if P <> nil then
    Result:=P-text;
end;

StrUtils function correctly returns the position of a word within the text, AnsiStrings function always returns -1.

 

I assume it is to do with the difference between PWideChar and PAnsiChar, but how do I handle that please within this function?

Share this post


Link to post
{irrelevant code removed}

 

Most probably is relevant code. We cannot know what is the problem in your code without a simple test case. You're changing the data types of the parameters in your posts. You should post a test application where we can see what are you doing in the function and how you're calling it.

Share this post


Link to post
16 hours ago, Lajos Juhász said:

You should post a test application

Unit2.pas

Unit2.dfm

 

I have made slight changes. Both of my own functions are now called with the same parameters.

 

The strUtils one works as expected. The AnsiStrings one reports -1

 

Thanks

Share this post


Link to post

Edit.text is unicode string so you should not use AnsiStrings for that. I tried your FindWordInTextStrUtils and in D11.1 get no deprecated warning:

dcc32 command line for "Project1.dpr"
[dcc32 Hint] Unit1.pas(30): H2164 Variable 's' is declared but never used in 'FindWordInTextStrUtils'
Success
Elapsed time: 00:00:00.7

Share this post


Link to post

There are two overloaded versions of SearchBuf in System.StrUtils:

 

{$IFNDEF NEXTGEN}
function SearchBuf(Buf: PAnsiChar; BufLen: Integer; SelStart, SelLength: Integer;
  SearchString: AnsiString; Options: TStringSearchOptions = [soDown]): PAnsiChar; overload; deprecated 'Moved to the AnsiStrings unit';
{$ENDIF}

{$IFDEF UNICODE}
function SearchBuf(Buf: PChar; BufLen: Integer; SelStart, SelLength: Integer;
  SearchString: String; Options: TStringSearchOptions = [soDown]): PChar; overload;
{$ENDIF}

 

you can see that the deprecated one is the ANSI version., actually the deprecated keyword is to alert you that the function is moved (rather than taken off).

The Unicode version is still there, I think it is more suitable than the ANSI version when working with strings in general.

Remove any reference to the AnsiStrings from your uses section , Keep only StrUtils, and replace AnsiString  type with the string type, PAnsiChar with the PChar so that the

compiler will use the Unicode overload instead of the deprecated ANSI overload.

 

 

Edited by Marsil
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×