[fpc-pascal]Strange Segfault...
Matt Emson
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...
James,
only tested in Delphi, but the following would seem to work....
procedure replace(search: String; replace: String; var s: String); overload;
begin
writeln('1');
end;
procedure replace(const chars: array of char; replace: String; var s:
String); overload;
begin
writeln('2');
end;
procedure Test;
var
a: array[0..9] of char;
w, s, d: string;
begin
replace(a, w, s); // get's '2'
replace(d, w, s); // gets '1'
end;
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
Delphi.
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):
Boolean;
begin
for index := low(chars) to high(chars) do
if not isin(s, '' + chars[index]) then begin
Result := FALSE;
Exit;
end;
Result := True;
index := -1; //nice default to check for
end;
procedure replace(const chars: array of char; replace: string; var s:
string);
var
index: Integer;
begin
while true do begin
if not isin(s, chars, index) then
Break
else begin
delete(s, index, 1);
insert(replace, s, index);
end;
end;
end;
Matt
More information about the fpc-pascal
mailing list