Mark Williams 14 Posted March 15, 2022 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
David Heffernan 2354 Posted March 15, 2022 https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.StrUtils.SearchBuf Suggests it is now in the AnsiStrings unit. 1 Share this post Link to post
Mark Williams 14 Posted March 15, 2022 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
David Heffernan 2354 Posted March 15, 2022 Can you just look at the RTL source code and work it out? Share this post Link to post
Stano 143 Posted March 15, 2022 Fill in the unit name before the function name! 1 Share this post Link to post
Mark Williams 14 Posted March 15, 2022 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
Lajos Juhász 295 Posted March 15, 2022 {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
Mark Williams 14 Posted March 16, 2022 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
Lajos Juhász 295 Posted March 16, 2022 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
Marsil 4 Posted May 6, 2022 (edited) 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 May 6, 2022 by Marsil 1 Share this post Link to post