memsom at interalpha.co.uk
Fri Jul 11 11:48:55 CEST 2003
> Well ok thanks Matt, however I kind of already had this figured out, but
> was posting here to find out if there's a way around it (other than
> naming two separate functions)...
> Perhaps there isn't...
only tested in Delphi, but the following would seem to work....
procedure replace(search: String; replace: String; var s: String); overload;
procedure replace(const chars: array of char; replace: String; var s:
a: array[0..9] of char;
w, s, d: string;
replace(a, w, s); // get's '2'
replace(d, w, s); // gets '1'
Whilst this isn;t really the answer you were looking for, it works for me.
Your original version (replace(['0'..'9'], w, s);) didn't even compile in
BTW, if you are using longstrings, you shouldn't be passing by reference, as
the longstring (AnsiString) is already a pointer. If FPC handles memory in a
similar way to Delphi with respects to longstrings, it may cause a
performance hit if the string is fairly long (say 1024+ chars).
The other thing that worried me about your code is that the 'procedure
replace(const chars: array of char; replace: String; var s: String);' uses
'index', but you don't initialize it to a sensible value, you just take for
granted that it is set. I would be happier with:
function isin(s: string; const chars: array of char; var index: Integer):
for index := low(chars) to high(chars) do
if not isin(s, '' + chars[index]) then begin
Result := FALSE;
Result := True;
index := -1; //nice default to check for
procedure replace(const chars: array of char; replace: string; var s:
while true do begin
if not isin(s, chars, index) then
delete(s, index, 1);
insert(replace, s, index);
More information about the fpc-pascal