<?xml version="1.0" encoding="UTF-8"?>
<refactors type="array">
  <refactor>
    <code></code>
    <code-id type="integer">1042</code-id>
    <comment>Sorry i should have made it more clear about the function, i will update the first post.</comment>
    <created-at type="datetime">2009-09-25T03:50:32+00:00</created-at>
    <id type="integer">318255</id>
    <language>Delphi</language>
    <rating type="integer">0</rating>
    <ratings-count type="integer">0</ratings-count>
    <title>On TStrings IndexOfValue</title>
    <user-id type="integer">1315</user-id>
    <user-name>DarkAxi0m</user-name>
    <user-website>http://darkaxi0m.name</user-website>
    <user>
      <id type="integer">1315</id>
      <identity-url>http://darkaxi0m.name</identity-url>
      <name>DarkAxi0m</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website>http://darkaxi0m.name</website>
    </user>
  </refactor>
  <refactor>
    <code>function indexOfValue(list : TStrings; value : string): integer;
begin
  Result := list.IndexOf(value);
end;</code>
    <code-id type="integer">1042</code-id>
    <comment>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.</comment>
    <created-at type="datetime">2009-09-24T21:15:35+00:00</created-at>
    <id type="integer">317867</id>
    <language>Delphi</language>
    <rating type="integer">0</rating>
    <ratings-count type="integer">0</ratings-count>
    <title>On TStrings IndexOfValue</title>
    <user-id type="integer" nil="true"></user-id>
    <user-name>Kenny</user-name>
    <user-website></user-website>
  </refactor>
  <refactor>
    <code></code>
    <code-id type="integer">805</code-id>
    <comment>you could use a different xml parser.  i think sax is faster. </comment>
    <created-at type="datetime">2009-03-29T14:54:06+00:00</created-at>
    <id type="integer">153535</id>
    <language>Delphi</language>
    <rating type="integer">0</rating>
    <ratings-count type="integer">0</ratings-count>
    <title>On XML Helper Function</title>
    <user-id type="integer">1124</user-id>
    <user-name>X-Ray</user-name>
    <user-website></user-website>
    <user>
      <id type="integer">1124</id>
      <identity-url>http://radiationblast.myopenid.com</identity-url>
      <name>X-Ray</name>
      <rating type="float">3.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website></website>
    </user>
  </refactor>
  <refactor>
    <code>{$IFNDEF UNICODE} // pre Delphi 2009
type
  UnicodeString = WideString;
{$ENDIF ~UNICODE}

function ChangeValue(const AData: UnicodeString; const AKeyVar: UnicodeString; AIncr: Integer): UnicodeString;
var
  I: Integer;
  lPosIni, lPosEqual, lPosSep: Integer;
  lStrBegin, lStrEnd: UnicodeString;
  lValue: Integer;
  lDataLen: Integer;
Begin
  {$IFDEF UNICODE}
  lPosIni := Pos(AnsiUpperCase(AKeyVar), AnsiUpperCase(AData));
  {$ELSE}
  lPosIni := Pos(WideUpperCase(AKeyVar), WideUpperCase(AData));
  {$ENDIF UNICODE}
  if lPosIni = 0 then
  begin
    Result := AData;
    Exit;
  end;

  lDataLen := Length(AData);

  // Find equal symbol
  lPosEqual := 0;
  for I := lPosIni + Length(AKeyVar) to lDataLen do
  begin
    case AData[I] of
      '=':
        begin
          lPosEqual := I;
          Break;
        end;
      ' ', ';':
        Break; // no equal symbol for this KeyVar
    end;
  end;

  // Search for sep - 'B' OR ;
  if lPosEqual &lt;&gt; 0 then
  begin
    lPosSep := lDataLen + 1;
    for I := lPosEqual + 1 to lDataLen do
    begin
      if (AData[I] = ';') or (AData[I] = ' ') then
      begin
        lPosSep := I;
        Break;
      end;
    end;

    lStrBegin := Copy(AData, 1, lPosEqual);
    lValue := AIncr + StrToInt(Copy(AData, lPosEqual + 1, (lPosSep - 1) - lPosEqual));
    lStrEnd	:= Copy(AData, lPosSep, MaxInt);

    Result := Format('%s%d%s', [lStrBegin, lValue, lStrEnd])
  end
  else
    Result := AData;
end;</code>
    <code-id type="integer">762</code-id>
    <comment>This code is is based on your algorithm but it is optimized to not unnecessary convert strings and chars.</comment>
    <created-at type="datetime">2009-02-22T11:00:34+00:00</created-at>
    <id type="integer">148294</id>
    <language>Delphi</language>
    <rating type="integer">2</rating>
    <ratings-count type="integer">2</ratings-count>
    <title>On Change Value Or Word in a WideString</title>
    <user-id type="integer">1316</user-id>
    <user-name>ahuser</user-name>
    <user-website nil="true"></user-website>
    <user>
      <id type="integer">1316</id>
      <identity-url>http://ahuser.wordpress.com</identity-url>
      <name>ahuser</name>
      <rating type="float">3.6667</rating>
      <refactors-count type="integer">3</refactors-count>
      <website nil="true"></website>
    </user>
  </refactor>
  <refactor>
    <code>// not tested

var
  sl:TStringList;
begin
  sl:=TStringList.Create;
  try
    // i think Text property splits on space AND #13.  if it doesn't, you can repl space with #13 too
    sl.Text:=StringReplace(s, ';', #13, [rfReplaceAll]);
    sl.Values[sTokenName]:='something';
    Result:=sl.Values[sTokenName];
  finally
    sl.Free;
  end;
end;


</code>
    <code-id type="integer">762</code-id>
    <comment>this comment doesn't directly answer your question but here's how i do things like that.

i like to use TStringList's parsing.  StringReplace hasn't been so fast but usually it doesn't matter much.

</comment>
    <created-at type="datetime">2009-02-21T17:46:30+00:00</created-at>
    <id type="integer">148194</id>
    <language>Delphi</language>
    <rating type="integer">3</rating>
    <ratings-count type="integer">2</ratings-count>
    <title>On Change Value Or Word in a WideString</title>
    <user-id type="integer">1124</user-id>
    <user-name>X-Ray</user-name>
    <user-website></user-website>
    <user>
      <id type="integer">1124</id>
      <identity-url>http://radiationblast.myopenid.com</identity-url>
      <name>X-Ray</name>
      <rating type="float">3.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website></website>
    </user>
  </refactor>
  <refactor>
    <code>type
  THashValue = Cardinal;

  TMylist = class(TList)
  private
    FName: string;
    FHashValue: THashValue;
  public
    constructor Create(const AName: string);
    function GetNameIndex(const aName: string): integer;
    function GetItems(Index: integer): TMylist;

    property Name: string read FName;
  end;

function NonAsciiHashName(aName: string): THashValue;
var
  I: Integer;
begin
  aName := AnsiLowerCase(aName);
  Result := 0;
  for I := 1 to Length(aName) do
    Inc(Result, Ord(aName[I]));
end;

function HashName(const aName: string): THashValue;
var
  Len: Integer;
  Ch: Char;
  P: PChar;
begin
  Len := Length(aName);
  P := Pointer(aName);
  Result := 0;
  while Len &gt; 0 do
  begin
    Ch := P[0];
    if (Ord(Ch) and {$IFDEF UNICODE}$FF80{$ELSE}$80{$ENDIF}) &lt;&gt; 0 then
      Break;
    Inc(Result, Ord(Ch) or $20);
    Inc(P);
    Dec(Len);
  end;
  if Len &gt; 0 then
    Result := NonAsciiHashName(aName);
end;

{ TMylist }

constructor TMylist.Create(const aName: string);
begin
  inherited Create;
  FName := aName;
  FHashValue := HashName(AName);
end;

function TMylist.GetNameIndex(const aName: string): integer;
var
  LocalList: PPointerList;
  HashValue: THashValue;
begin
  HashValue := HashName(aName);
  LocalList := List;
  for Result := 0 to Count - 1 do
    if (HashValue = TMylist(LocalList[Result]).FHashValue) and
       (SameText(TMylist(LocalList[Result]).Name, aName)) then
      Exit;
  Result := -1;
end;

function TMylist.GetItems(Index: integer): TMylist;
begin
  Result := TMylist(inherited Items[index]);
end;
</code>
    <code-id type="integer">754</code-id>
    <comment>And this is the code that uses a hash value to omit many string comparisons. But it requires an additional field (FHashValue: THashValue) that must be recalculated after the FName field was changed.</comment>
    <created-at type="datetime">2009-02-12T17:32:07+00:00</created-at>
    <id type="integer">146926</id>
    <language>Delphi</language>
    <rating type="integer">4</rating>
    <ratings-count type="integer">2</ratings-count>
    <title>On custom index for class(TList)</title>
    <user-id type="integer">1316</user-id>
    <user-name>ahuser</user-name>
    <user-website nil="true"></user-website>
    <user>
      <id type="integer">1316</id>
      <identity-url>http://ahuser.wordpress.com</identity-url>
      <name>ahuser</name>
      <rating type="float">3.6667</rating>
      <refactors-count type="integer">3</refactors-count>
      <website nil="true"></website>
    </user>
  </refactor>
  <refactor>
    <code>function TMylist.GetNameIndex(const aName: string): integer; // use "const" for strings to eliminate the injected try/finally
var
  LocalList: PPointerList;
begin
  LocalList := List; // use a local variable, removes one indirection, no bound checks required
  for Result := 0 to Count - 1 do
    if SameText(TMylist(LocalList[Result]).name, aName) then // do not call GetItems
      Exit;
  Result := -1;
end;

function TMylist.GetItems(Index: integer): TMylist;
begin
  Result := TMylist(inherited Items[index]);
end;
</code>
    <code-id type="integer">754</code-id>
    <comment>The following code should be slightly faster. It removes the try/finally that the compiler injects into your code because of the missing "const" for the string parameter. And it has only one function call in the loop. But the main problem are the string comparisons in the loop.
For this you could either sort the list and then access if via a binary search algorithm, or you could hash the string values and compare the hashes first before performing the costly string comparison.
The binary search can only be used if you are not interested in the order of the list items. And using a hash would require an additional field (the hash value) for each item (preferable placed into the item's class).</comment>
    <created-at type="datetime">2009-02-12T11:52:37+00:00</created-at>
    <id type="integer">146893</id>
    <language>Delphi</language>
    <rating type="integer">5</rating>
    <ratings-count type="integer">3</ratings-count>
    <title>On custom index for class(TList)</title>
    <user-id type="integer">1316</user-id>
    <user-name>ahuser</user-name>
    <user-website nil="true"></user-website>
    <user>
      <id type="integer">1316</id>
      <identity-url>http://ahuser.wordpress.com</identity-url>
      <name>ahuser</name>
      <rating type="float">3.6667</rating>
      <refactors-count type="integer">3</refactors-count>
      <website nil="true"></website>
    </user>
  </refactor>
  <refactor>
    <code></code>
    <code-id type="integer">731</code-id>
    <comment>Apologies to all - @Andreas Hausladen - you're absolutely correct. I changed the type of the Dest argument in AppendToArray after I pasted the code into this site to make it simpler to refactor at a glance and forgot about not being able to SetLegnth on the open array parameter. 

Thanks for the refactorings!</comment>
    <created-at type="datetime">2009-02-04T11:48:21+00:00</created-at>
    <id type="integer">145611</id>
    <language>Delphi</language>
    <rating type="integer">0</rating>
    <ratings-count type="integer">0</ratings-count>
    <title>On Array Helper Functions</title>
    <user-id type="integer">1312</user-id>
    <user-name>jamiei</user-name>
    <user-website></user-website>
    <user>
      <id type="integer">1312</id>
      <identity-url>http://jamie.mp</identity-url>
      <name>jamiei</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">1</refactors-count>
      <website></website>
    </user>
  </refactor>
  <refactor>
    <code>{$IF not declared(TBytes)}
type
  TBytes = array of Byte;
{$IFEND}

procedure CopyIntoArray(var DestArray: TBytes; const SourceArray: array of Byte; StartIndex: integer);
begin
  Assert(StartIndex &gt;= 0);
  Assert(Length(DestArray) &lt;= StartIndex + Length(SourceArray));

  Move(SourceArray[0], DestArray[StartIndex], Length(SourceArray));
end;

procedure AppendToArray(var Dest: TBytes; Source: array of Byte);
var
  DestLen: Integer;
begin
  DestLen := Length(Dest);
  SetLength(Dest, DestLen + Length(Source));
  Move(Source, Dest[DestLen], Length(Source));
end;
</code>
    <code-id type="integer">731</code-id>
    <comment>How did you get this to compile. I always get "Incompatible types" because you can't use SetLength() on an open array parameter (Dest). You must declare a type lile TDynByteArray or TBytes (as it is in Delphi 2009).

@DarkAxi0m: Your AppendToArray copies the Source-data beyond the Dest-array end.</comment>
    <created-at type="datetime">2009-02-04T11:26:58+00:00</created-at>
    <id type="integer">145609</id>
    <language>Delphi</language>
    <rating type="integer">5</rating>
    <ratings-count type="integer">4</ratings-count>
    <title>On Array Helper Functions</title>
    <user-id type="integer" nil="true"></user-id>
    <user-name>Andreas Hausladen</user-name>
    <user-website></user-website>
  </refactor>
  <refactor>
    <code>## Delphi 7 [pascal]
procedure CopyIntoArray(var DestArray: Array of Byte; SourceArray: Array of Byte; StartIndex: integer);
begin
 Move(SourceArray,DestArray[StartIndex],SizeOf(SourceArray));
end;

procedure AppendToArray(var Dest: Array of Byte; Source: Array of Byte);
begin
  SetLength(Dest,SizeOf(dest)+SizeOf(Source));
  Move(Source,Dest[SizeOf(dest)],SizeOf(Source));
end;
</code>
    <code-id type="integer">731</code-id>
    <comment>Using Delphi 7;

Move seems to be fast,

CopyIntoArray, after a quick test looks like it returns the same results.

AppendToArray, I couldn't get to compile either versions.
Can not seem to do setlength on 'var Dest: Array of Byte;'
</comment>
    <created-at type="datetime">2009-02-03T23:14:02+00:00</created-at>
    <id type="integer">145549</id>
    <language>Delphi</language>
    <rating type="integer">0</rating>
    <ratings-count type="integer">0</ratings-count>
    <title>On Array Helper Functions</title>
    <user-id type="integer">1315</user-id>
    <user-name>DarkAxi0m</user-name>
    <user-website>http://darkaxi0m.name</user-website>
    <user>
      <id type="integer">1315</id>
      <identity-url>http://darkaxi0m.name</identity-url>
      <name>DarkAxi0m</name>
      <rating type="float">0.0</rating>
      <refactors-count type="integer">2</refactors-count>
      <website>http://darkaxi0m.name</website>
    </user>
  </refactor>
</refactors>
