Skip to content

Simplified GetEnumNameCount

by 'lagprogramming'.

rtl/objpas/typinfo.pp has

    function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
    var
      PS: PShortString;
      PT: PTypeData;
      Count: SizeInt;
    begin
      PT:=GetTypeData(enum1);
      if enum1^.Kind=tkBool then
        Result:=2
      else
        begin
          Count:=0;
          Result:=0;
     
          PS:=@PT^.NameList;
          While (PByte(PS)^<>0) do
            begin
              PS:=PShortString(pointer(PS)+PByte(PS)^+1);
              Inc(Count);
            end;
          { the last string is the unit name }
          Result := Count - 1;
        end;
    end;

The above function has been simplified. Variable "Count" has been replaced by "Result"; Removed the last "Result := Result - 1;" line by replacing "Result:=0;" with "Result:=-1;" Switched "Result:=-1;" and "PS:=@PT^.NameList;" lines; Variable "PT" has been removed by replacing "PS:=@PT^.NameList;" with "PS:=@GetTypeData(enum1)^.NameList;". The function becomes:

    function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
    var
      PS: PShortString;
    begin
      if enum1^.Kind=tkBool then
        Result:=2
      else
        begin
          PS:=@GetTypeData(enum1)^.NameList;
          Result:=-1;
          While (PByte(PS)^<>0) do
            begin
              PS:=PShortString(pointer(PS)+PByte(PS)^+1);
              Inc(Result);
            end;
        end;
    end;

patch.diff

Edited by Alexey Torgashin
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information