syshelpo.inc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. {%MainUnit sysutils.pp}
  2. Class Function TORDINALHELPER.Parse(const AString: string): TORDINALTYPE; inline; static;
  3. var
  4. Error: Integer;
  5. begin
  6. Val(AString,Result,Error);
  7. if Error<>0 then
  8. raise EConvertError.CreateFmt(SInvalidInteger,[AString]);
  9. end;
  10. Class Function TORDINALHELPER.Size: Integer; inline; static;
  11. begin
  12. Result:=SizeOf(TORDINALTYPE);
  13. end;
  14. Class Function TORDINALHELPER.ToString(const AValue: TORDINALTYPE): string; overload; inline; static;
  15. begin
  16. Str(AValue,Result);
  17. end;
  18. Class Function TORDINALHELPER.TryParse(const AString: string; out AValue: TORDINALTYPE): Boolean; inline; static;
  19. Var
  20. C : Integer;
  21. begin
  22. Val(AString,AValue,C);
  23. Result:=(C=0);
  24. end;
  25. Function TORDINALHELPER.ToBoolean: Boolean; inline;
  26. begin
  27. Result:=(Self<>0);
  28. end;
  29. Function TORDINALHELPER.ToDouble: Double; inline;
  30. begin
  31. Result:=Self;
  32. end;
  33. Function TORDINALHELPER.ToExtended: Extended; inline;
  34. begin
  35. Result:=Self;
  36. end;
  37. Function TORDINALHELPER.ToBinString: string; inline;
  38. begin
  39. Result:=BinStr(Self,SizeOf(TORDINALTYPE)*8);
  40. end;
  41. Function TORDINALHELPER.ToHexString(const AMinDigits: Integer): string;
  42. overload; inline;
  43. begin
  44. Result:=IntToHex(Self,AMinDigits);
  45. end;
  46. Function TORDINALHELPER.ToHexString: string; overload; inline;
  47. begin
  48. Result:=IntToHex(Self);
  49. end;
  50. Function TORDINALHELPER.ToSingle: Single; inline;
  51. begin
  52. Result:=Self;
  53. end;
  54. Function TORDINALHELPER.ToString: string; overload; inline;
  55. begin
  56. Str(Self,Result);
  57. end;
  58. Function TORDINALHELPER.SetBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  59. begin
  60. Self := Self or (TORDINALTYPE(1) shl index);
  61. Result:=Self;
  62. end;
  63. Function TORDINALHELPER.ClearBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  64. begin
  65. Self:=Self and not TORDINALTYPE((TORDINALTYPE(1) shl index));
  66. Result:=Self;
  67. end;
  68. Function TORDINALHELPER.ToggleBit(const index: TORDINALBITINDEX) : TORDINALTYPE; inline;
  69. begin
  70. Self := Self xor TORDINALTYPE((TORDINALTYPE(1) shl index));
  71. Result:=Self;
  72. end;
  73. Function TORDINALHELPER.TestBit(const Index: TORDINALBITINDEX):Boolean; inline;
  74. begin
  75. Result := (Self and TORDINALTYPE((TORDINALTYPE(1) shl index)))<>0;
  76. end;
  77. procedure TORDINALHELPER.Clear;
  78. begin
  79. Self := 0;
  80. end;
  81. function TORDINALHELPER.HighestSetBitPos: int8;
  82. begin
  83. {$ifdef TORDINALTYPESIZE1}
  84. Result := int8(BsrByte(byte(Self)));
  85. {$else}
  86. {$ifdef TORDINALTYPESIZE2}
  87. Result := int8(BsrWord(word(Self)));
  88. {$else}
  89. {$ifdef TORDINALTYPESIZE4}
  90. Result := int8(BsrDWord(dword(Self)));
  91. {$else} // TORDINALTYPESIZE8
  92. Result := int8(BsrQWord(qword(Self)));
  93. {$endif}
  94. {$endif}
  95. {$endif}
  96. end;
  97. function TORDINALHELPER.LowestSetBitPos: int8;
  98. begin
  99. {$ifdef TORDINALTYPESIZE1}
  100. Result := int8(BsfByte(byte(Self)));
  101. {$else}
  102. {$ifdef TORDINALTYPESIZE2}
  103. Result := int8(BsfWord(word(Self)));
  104. {$else}
  105. {$ifdef TORDINALTYPESIZE4}
  106. Result := int8(BsfDWord(dword(Self)));
  107. {$else} // TORDINALTYPESIZE8
  108. Result := int8(BsfQWord(qword(Self)));
  109. {$endif}
  110. {$endif}
  111. {$endif}
  112. end;
  113. function TORDINALHELPER.SetBitsCount: byte;
  114. begin
  115. {$ifdef TORDINALTYPESIZE1}
  116. {$ifdef VER3_2_2}
  117. Result := PopCnt(Word(byte(Self)));
  118. {$else VER3_2_2}
  119. Result := PopCnt(byte(Self));
  120. {$endif VER3_2_2}
  121. {$else}
  122. {$ifdef TORDINALTYPESIZE2}
  123. Result := PopCnt(word(Self));
  124. {$else}
  125. {$ifdef TORDINALTYPESIZE4}
  126. Result := PopCnt(dword(Self));
  127. {$else} // TORDINALTYPESIZE8
  128. Result := PopCnt(qword(Self));
  129. {$endif}
  130. {$endif}
  131. {$endif}
  132. end;
  133. function TORDINALHELPER.GetBit(const aIndex: TORDINALBITINDEX): boolean;
  134. begin
  135. Result := ((Self shr aIndex) and TORDINALTYPE(1)) = TORDINALTYPE(1);
  136. end;
  137. procedure TORDINALHELPER.PutBit(const aIndex: TORDINALBITINDEX; const aNewValue: boolean);
  138. begin
  139. Self := Self or (TORDINALTYPE(1) shl aIndex) xor (TORDINALTYPE(not aNewValue) shl aIndex);
  140. end;
  141. function TORDINALHELPER.GetNibble(const aIndex: TORDINALNIBBLEINDEX): nibble;
  142. begin
  143. Result := TORDINALOVERLAY(Self).AsNibble[aIndex];
  144. end;
  145. procedure TORDINALHELPER.PutNibble(const aIndex: TORDINALNIBBLEINDEX; const aNewValue: nibble);
  146. begin
  147. TORDINALOVERLAY(Self).AsNibble[aIndex] := aNewValue;
  148. end;
  149. {$ifndef TORDINALTYPESIZE1} // TWordHelper, TDWordHelper, TQWordHelper jump in here (and others with 2, 4 and 8 bytes)
  150. function TORDINALHELPER.GetByte(const aIndex: TORDINALBYTEINDEX): byte;
  151. begin
  152. Result := TORDINALOVERLAY(Self).AsByte[aIndex];
  153. end;
  154. procedure TORDINALHELPER.PutByte(const aIndex: TORDINALBYTEINDEX; const aNewValue: byte);
  155. begin
  156. TORDINALOVERLAY(Self).AsByte[aIndex] := aNewValue;
  157. end;
  158. {$ifndef TORDINALTYPESIZE2} // TDWordHelper, TQWordHelper jump in here (and others with 4 and 8 bytes)
  159. function TORDINALHELPER.GetWord(const aIndex: TORDINALWORDINDEX): word;
  160. begin
  161. Result := TORDINALOVERLAY(Self).AsWord[aIndex];
  162. end;
  163. procedure TORDINALHELPER.PutWord(const aIndex: TORDINALWORDINDEX; const aNewValue: word);
  164. begin
  165. TORDINALOVERLAY(Self).AsWord[aIndex] := aNewValue;
  166. end;
  167. {$ifndef TORDINALTYPESIZE4} // TQWordHelper jumps in here (and others with 8 bytes)
  168. function TORDINALHELPER.GetDword(const aIndex: TORDINALDWORDINDEX): dword;
  169. begin
  170. Result := TORDINALOVERLAY(Self).AsDword[aIndex];
  171. end;
  172. procedure TORDINALHELPER.PutDword(const aIndex: TORDINALDWORDINDEX; const aNewValue: dword);
  173. begin
  174. TORDINALOVERLAY(Self).AsDword[aIndex] := aNewValue;
  175. end;
  176. {$endif}
  177. {$endif}
  178. {$endif}