(* Collection of code snippets by Arne VajhÃ¸j *)
(* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) *)
program special;

{$APPTYPE CONSOLE}

uses
  Windows, SysUtils;

function incr1(V: String):String;

var
  A: String;  // Eventuel ikke-numerisk første del af nummeret.
  N: String;  // Heltals tæller i slutningen af nummeret.
  P: Integer;  // Position hvor alphanumerisk og numerisk skiller.
  L: Integer;  // Antal cifre i den numeriske del, mht. foranstillede nuller.

begin
  P := Length(V);
  while (P > 0) and (V[P]>='0') and (V[P]<='9') do
    P := P - 1;
  A := Copy(V, 1, P);            // A = Den alphanumeriske del.
  N := Copy(V, P+1, Length(V));  // N = Den numeriske del.
  if N = '' then N := '0';        // Der skal være en numerisk del.
  L := Length(N);
  N := IntToStr(StrToInt(N) +1);  // Tæl en frem.
  while Length(N) < L do          // Hvis der er foranstillede nuller på den
    N := '0' + N;                // numeriske del, skal disse bibeholdes,
  incr1 := A + N;                // indtil værdien kræver et ciffer mere.
end;

function incr2(V: String):String;

var
  P: Integer;  // Position hvor alphanumerisk og numerisk skiller.

begin
  P := Length(V);
  while (P > 0) and (V[P] in ['0'..'9']) do
    P := P - 1;
  Result := Copy(V, 1, P) + IntToStr(StrToIntDef(Copy(V, P+1, Length(V)), 0)+1);
  while Length(Result) < Length(V) do
    Insert('0', Result, P+1);
end;

function incr3(V: String):String;

var
  P: Integer;  // Position hvor alphanumerisk og numerisk skiller.

begin
  P := Length(V);
  while (P > 0) and (V[P] in ['0'..'9']) do
    P := P - 1;
  Result := Copy(V, 1, P) + Format('%.'+IntToStr(Length(V)-P)+'d', [StrToIntDef(Copy(V, P+1, Length(V)), 0)+1]);
end;

function incr4(s : string) : string;

var
  add,i : integer;
  res : string;

begin
  res := s;
  add := 1;
  i := length(res);
  while add > 0 do begin
     if (res[i] < '0') or (res[i] > '9') then begin
       res := copy(res, 1, i) + '0' + copy(res, i + 1, length(res) - i);
       i := i + 1;
     end;
     res[i] := chr(ord(res[i]) + add);
     if res[i] > '9' then begin
       res[i] := chr(ord(res[i]) - 10);
       add := 1;
     end else begin
       add := 0;
     end;
     i := i - 1;
  end;
  incr4 := res;
end;

procedure test(s : string);

const
  n = 1000000;

var
  i,t1,t2 : integer;
  res : string;

begin
  writeln(s);
  t1 := GetTickCount;
  for i := 1 to n do begin
     res := incr1(s);
  end;
  t2 := GetTickCount;
  writeln(res,' ',t2-t1);
  t1 := GetTickCount;
  for i := 1 to n do begin
     res := incr2(s);
  end;
  t2 := GetTickCount;
  writeln(res,' ',t2-t1);
  t1 := GetTickCount;
  for i := 1 to n do begin
     res := incr3(s);
  end;
  t2 := GetTickCount;
  writeln(res,' ',t2-t1);
  t1 := GetTickCount;
  for i := 1 to n do begin
     res := incr4(s);
  end;
  t2 := GetTickCount;
  writeln(res,' ',t2-t1);
end;

begin
  test('Test1');
  test('Test0098');
  test('Test0199');
  test('Test9');
end.
