[Lazarus] How can I increment/decrement a alphanumeric value?

Antônio antoniog12345 at gmail.com
Sat Aug 21 07:36:20 CEST 2010


Sílvio,

Your routine, after some modifications, seems to do the job:

{$J+}
const
 CIncStrChars = '0123456789ABCDEF';
{$J-}

function IncStr(const AString: string): string;
var
 VCurrChar: PChar;
 I, VPos, VBackIndex: Integer;
begin
 Result := Copy(AString, 1, Length(AString));
 for I := 1 to Length(AString) do
   if Pos(AString[I], CIncStrChars) < 1 then
     raise Exception.Create('String contains an invalid character');
 if Length(AString) < 1 then
 begin
   Result := CIncStrChars[1];
   Exit;
 end;
 {if Length(AString) < 2 then
 begin
   Result := Succ(AString[1]);
   Exit;
 end;}
 VBackIndex := 0;
 while True do
 begin
   VCurrChar := @Result[Length(Result) - VBackIndex];
   VPos := Pos(VCurrChar^, CIncStrChars);
   if VPos = Length(CIncStrChars) then
   begin
     VCurrChar^ := CIncStrChars[1];
     if (Length(Result) - VBackIndex) <= 1 then
     begin
       Result := CIncStrChars[2] + Result;
       Break;
     end
     else
     begin
       Inc(VBackIndex);
       Continue;
     end;
   end
   else
   begin
     VCurrChar^ := CIncStrChars[VPos + 1];
     Break;
   end;
 end;
end;




More information about the Lazarus mailing list