### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: [SOLVED] Simplified GetEnumNameCount  (Read 765 times)

#### lagprogramming

• Sr. Member
• Posts: 390
##### [SOLVED] Simplified GetEnumNameCount
« on: August 23, 2023, 03:35:22 pm »
rtl/objpas/typinfo.pp has
Code: Pascal  [Select][+][-]
1. function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
2. var
3.   PS: PShortString;
4.   PT: PTypeData;
5.   Count: SizeInt;
6. begin
7.   PT:=GetTypeData(enum1);
8.   if enum1^.Kind=tkBool then
9.     Result:=2
10.   else
11.     begin
12.       Count:=0;
13.       Result:=0;
14.
15.       PS:=@PT^.NameList;
16.       While (PByte(PS)^<>0) do
17.         begin
18.           PS:=PShortString(pointer(PS)+PByte(PS)^+1);
19.           Inc(Count);
20.         end;
21.       { the last string is the unit name }
22.       Result := Count - 1;
23.     end;
24. 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:
Code: Pascal  [Select][+][-]
1. function GetEnumNameCount(enum1: PTypeInfo): SizeInt;
2. var
3.   PS: PShortString;
4. begin
5.   if enum1^.Kind=tkBool then
6.     Result:=2
7.   else
8.     begin
9.       PS:=@GetTypeData(enum1)^.NameList;
10.       Result:=-1;
11.       While (PByte(PS)^<>0) do
12.         begin
13.           PS:=PShortString(pointer(PS)+PByte(PS)^+1);
14.           Inc(Result);
15.         end;
16.     end;
17. end;

The patch is attached to the message.
« Last Edit: August 25, 2023, 12:11:48 pm by lagprogramming »

#### AlexTP

• Hero Member
• Posts: 2294
##### Re: Simplified GetEnumNameCount
« Reply #1 on: August 24, 2023, 08:07:51 am »