function indexOfValue(list : TStrings; value : string): integer;
begin
Result := 0;
while (Result < list.Count) and (not SameText(list.ValueFromIndex[Result], value)) do
Inc(Result);
if Result >= list.Count then
Result := -1;
end;
Refactorings
No refactoring yet !
Kenny
September 24, 2009, September 24, 2009 21:15, permalink
The way you implemented this is identical to how TStrings.IndexOf handles it. Only your implementation has the overhead of additional function calls to look up TStrings.Count and TStrings.ValueFromIndex. Unless there are other characteristics to the TStrings list that haven't been mentioned (its sorted or only a specific subset of strings is allowed) you won't get better than Delphi's own implementation.
function indexOfValue(list : TStrings; value : string): integer; begin Result := list.IndexOf(value); end;
DarkAxi0m
September 25, 2009, September 25, 2009 03:50, permalink
Sorry i should have made it more clear about the function, i will update the first post.
atmotaIrrap
September 13, 2010, September 13, 2010 08:46, permalink
I suck coz I only appear to be able to think of one joke and one joke alone...
Why did the plane crash into the house????
COZ THE LANDING LIGHT WAS ON!!
Tony
November 3, 2011, November 03, 2011 06:07, permalink
I use functions like this too (I agree it's not the same as IndexOf, which matches the whole string) for improvement I'd do two things.
1. Use const to avoid copying the objects around
2. Use a for loop - for loops in delphi are highly optimised
function IndexOfValue(const List: TStrings; const Value: String): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to List.Count - 1 do
if SameText(Value, List.ValueFromIndex[I]) then
Exit(I);
end;
Tony
November 3, 2011, November 03, 2011 06:07, permalink
I use functions like this too (I agree it's not the same as IndexOf, which matches the whole string) for improvement I'd do two things.
1. Use const to avoid copying the objects around
2. Use a for loop - for loops in delphi are highly optimised
function IndexOfValue(const List: TStrings; const Value: String): Integer;
var
I: Integer;
begin
Result := -1;
for I := 0 to List.Count - 1 do
if SameText(Value, List.ValueFromIndex[I]) then
Exit(I);
end;
this function finds the first match in stringlist
where the value part of a "name=value" pair is equal value passed.
Any thoughts on speeding this up?