Browse Source

* GetEnumName handles subrange types correctly, resolves #15377

git-svn-id: trunk@14461 -
florian 15 years ago
parent
commit
8cecea9b53
3 changed files with 24 additions and 9 deletions
  1. 1 0
      .gitattributes
  2. 10 9
      rtl/objpas/typinfo.pp
  3. 13 0
      tests/webtbs/tw15377.pp

+ 1 - 0
.gitattributes

@@ -10149,6 +10149,7 @@ tests/webtbs/tw15304.pp svneol=native#text/plain
 tests/webtbs/tw1532.pp svneol=native#text/plain
 tests/webtbs/tw15364.pp svneol=native#text/plain
 tests/webtbs/tw15370.pp svneol=native#text/plain
+tests/webtbs/tw15377.pp svneol=native#text/pascal
 tests/webtbs/tw1539.pp svneol=native#text/plain
 tests/webtbs/tw1567.pp svneol=native#text/plain
 tests/webtbs/tw1573.pp svneol=native#text/plain

+ 10 - 9
rtl/objpas/typinfo.pp

@@ -363,7 +363,7 @@ Function GetEnumName(TypeInfo : PTypeInfo;Value : Integer) : string;
 
 begin
   PT:=GetTypeData(TypeInfo);
-  if TypeInfo^.Kind=tkBool then 
+  if TypeInfo^.Kind=tkBool then
     begin
       case Value of
         0,1:
@@ -375,6 +375,7 @@ begin
  else
    begin
      PS:=@PT^.NameList;
+     dec(Value,PT^.MinValue);
      While Value>0 Do
        begin
          PS:=PShortString(pointer(PS)+PByte(PS)^+1);
@@ -399,8 +400,8 @@ begin
   PT:=GetTypeData(TypeInfo);
   Count:=0;
   Result:=-1;
-  
-  if TypeInfo^.Kind=tkBool then 
+
+  if TypeInfo^.Kind=tkBool then
     begin
     If CompareText(BooleanIdents[false],Name)=0 then
       result:=0
@@ -409,7 +410,7 @@ begin
     end
  else
    begin
-  
+
      PS:=@PT^.NameList;
      While (Result=-1) and (PByte(PS)^<>0) do
        begin
@@ -429,20 +430,20 @@ var
   Count: SizeInt;
 begin
   PT:=GetTypeData(enum1);
-  if enum1^.Kind=tkBool then 
+  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;
-    
+
       Result := Count;
     end;
 end;
@@ -795,7 +796,7 @@ begin
       GetPropInfos(TypeInfo,PropList);
     end
   else
-    PropList:=Nil;  
+    PropList:=Nil;
 end;
 
 function GetPropList(AClass: TClass; out PropList: PPropList): Integer;
@@ -1461,7 +1462,7 @@ begin
     tkSString,tkAString:
       Result:=GetStrProp(Instance,PropInfo);
     tkWString:
-      Result:=GetWideStrProp(Instance,PropInfo);      
+      Result:=GetWideStrProp(Instance,PropInfo);
     tkUString:
       begin
         case (PropInfo^.PropProcs) and 3 of

+ 13 - 0
tests/webtbs/tw15377.pp

@@ -0,0 +1,13 @@
+program test;
+
+uses typinfo;
+
+Type
+  E1 = (en1,en2,en3,en4,en5);
+  E2 = en3..en5;
+begin
+  if (GetEnumName(TypeInfo(E1),Ord(High(E1))) <> 'en5') then
+    halt(1);
+  if (GetEnumName(TypeInfo(E2),Ord(High(E2))) <> 'en5') then
+    halt(1);
+end.