2
0

syshelph.inc 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. {%mainunit syshelpers.pp}
  2. Type
  3. generic TArray<T> = array of T;
  4. TStringArray = Array of string;
  5. TCharArray = Array of char;
  6. TEndian = ObjPas.TEndian;
  7. TByteBitIndex = 0..7;
  8. TShortIntBitIndex = 0..7;
  9. TWordBitIndex = 0..15;
  10. TSmallIntBitIndex = 0..15;
  11. TCardinalBitIndex = 0..31;
  12. {$if sizeof(Integer)=2}
  13. TIntegerBitIndex = 0..15;
  14. {$elseif sizeof(Integer)=4}
  15. TIntegerBitIndex = 0..31;
  16. {$else}
  17. {$fatal Unsupported Integer type size}
  18. {$endif}
  19. TLongIntBitIndex = 0..31;
  20. TQwordBitIndex = 0..63;
  21. TInt64BitIndex = 0..63;
  22. {$IFDEF cpu16}
  23. TNativeUIntBitIndex = TWordBitIndex;
  24. TNativeIntBitIndex = TSmallIntBitIndex;
  25. {$ENDIF}
  26. {$IFDEF cpu32}
  27. TNativeUIntBitIndex = TCardinalBitIndex;
  28. TNativeIntBitIndex = TIntegerBitIndex;
  29. {$ENDIF}
  30. {$IFDEF cpu64}
  31. TNativeUIntBitIndex = TQWordBitIndex;
  32. TNativeIntBitIndex = TInt64BitIndex;
  33. {$ENDIF}
  34. Const
  35. CPUEndian = {$IFDEF FPC_LITTLE_ENDIAN}TEndian.Little{$ELSE}TEndian.Big{$ENDIF};
  36. Type
  37. TGuidHelper = record helper for TGUID
  38. Class function Create(const Data; BigEndian: Boolean): TGUID; overload; static;
  39. class function Create(const Data: array of Byte; AStartIndex: Cardinal; BigEndian: Boolean): TGUID; overload; static;
  40. Class Function Create(const Data; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  41. Class Function Create(const B: TBytes; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  42. Class Function Create(const B: TBytes; AStartIndex: Cardinal; DataEndian: TEndian = CPUEndian): TGUID; overload; static;
  43. Class Function Create(const S: string): TGUID; overload; static;
  44. Class Function Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
  45. Class Function Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  46. Class Function Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  47. Class Function NewGuid: TGUID; static;
  48. Function ToByteArray(DataEndian: TEndian = CPUEndian): TBytes;
  49. Function ToString(SkipBrackets: Boolean = False): string;
  50. end;
  51. TCompareOption = system.TCompareOption;
  52. TCompareOptions = system.TCompareOptions;
  53. {$SCOPEDENUMS ON}
  54. TStringSplitOptions = (None, ExcludeEmpty, ExcludeLastEmpty);
  55. {$SCOPEDENUMS OFF}
  56. { TStringHelper }
  57. TStringHelper = Type Helper for AnsiString
  58. Private
  59. Function GetChar(AIndex : SizeInt) : Char;
  60. Function GetLength : SizeInt;
  61. public
  62. const Empty = '';
  63. // Methods
  64. Class Function Compare(const A: string; const B: string): Integer; overload; static; //inline;
  65. Class Function Compare(const A: string; const B: string; IgnoreCase: Boolean): Integer; overload; static; //inline; //deprecated 'Use same with TCompareOptions';
  66. Class Function Compare(const A: string; const B: string; Options: TCompareOptions): Integer; overload; static; // inline;
  67. Class Function Compare(const A: string; IndexA: SizeInt; const B: string; IndexB: SizeInt; ALen: SizeInt): Integer; overload; static; // inline;
  68. Class Function Compare(const A: string; IndexA: SizeInt; const B: string; IndexB: SizeInt; ALen: SizeInt; IgnoreCase: Boolean): Integer; overload; static; // inline; //deprecated 'Use same with TCompareOptions';
  69. Class Function Compare(const A: string; IndexA: SizeInt; const B: string; IndexB: SizeInt; ALen: SizeInt; Options: TCompareOptions): Integer; overload; static;// inline;
  70. Class Function CompareOrdinal(const A: string; const B: string): Integer; overload; static;
  71. Class Function CompareOrdinal(const A: string; IndexA: SizeInt; const B: string; IndexB: SizeInt; ALen: SizeInt): Integer; overload; static;
  72. Class Function CompareText(const A: string; const B: string): Integer; static; inline;
  73. Class Function Copy(const Str: string): string; inline; static;
  74. Class Function Create(AChar: Char; ACount: SizeInt): string; overload; inline; static;
  75. Class Function Create(const AValue: array of Char): string; overload; static;
  76. Class Function Create(const AValue: array of Char; StartIndex: SizeInt; ALen: SizeInt): string; overload; static;
  77. Class Function EndsText(const ASubText, AText: string): Boolean; static;
  78. Class Function Equals(const a: string; const b: string): Boolean; overload; static;
  79. Class Function Format(const AFormat: string; const args: array of const): string; overload; static;
  80. Class Function IsNullOrEmpty(const AValue: string): Boolean; static;
  81. Class Function IsNullOrWhiteSpace(const AValue: string): Boolean; static;
  82. Class Function Join(const Separator: string; const Values: array of const): string; overload; static;
  83. Class Function Join(const Separator: string; const Values: array of string): string; overload; static;
  84. Class Function Join(const Separator: string; const Values: array of string; StartIndex: SizeInt; ACount: SizeInt): string; overload; static;
  85. Class Function LowerCase(const S: string): string; overload; static; inline;
  86. Class Function Parse(const AValue: Boolean): string; overload; static; inline;
  87. Class Function Parse(const AValue: Extended): string; overload; static;inline;
  88. Class Function Parse(const AValue: Int64): string; overload; static; inline;
  89. Class Function Parse(const AValue: Integer): string; overload; static; inline;
  90. Class Function ToBoolean(const S: string): Boolean; overload; static; inline;
  91. Class Function ToDouble(const S: string): Double; overload; static; inline;
  92. Class Function ToExtended(const S: string): Extended; overload; static; inline;
  93. Class Function ToInt64(const S: string): Int64; overload; static; inline;
  94. Class Function ToInteger(const S: string): Integer; overload; static; inline;
  95. Class Function ToSingle(const S: string): Single; overload; static; inline;
  96. Class Function UpperCase(const S: string): string; overload; static; inline;
  97. Function CompareTo(const B: string): Integer;
  98. Function Contains(const AValue: string): Boolean;
  99. procedure CopyTo(SourceIndex: SizeInt; var destination: array of Char; DestinationIndex: SizeInt; ACount: SizeInt);
  100. Function CountChar(const C: Char): SizeInt;
  101. Function DeQuotedString: string; overload;
  102. Function DeQuotedString(const AQuoteChar: Char): string; overload;
  103. Function EndsWith(const AValue: string): Boolean; overload; inline;
  104. Function EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
  105. Function Equals(const AValue: string): Boolean; overload;
  106. Function Format(const args: array of const): string; overload;
  107. Function GetHashCode: Integer;
  108. Function IndexOf(AValue: Char): SizeInt; overload; inline;
  109. Function IndexOf(const AValue: string): SizeInt; overload; inline;
  110. Function IndexOf(AValue: Char; StartIndex: SizeInt): SizeInt; overload;
  111. Function IndexOf(const AValue: string; StartIndex: SizeInt): SizeInt; overload;
  112. Function IndexOf(AValue: Char; StartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  113. Function IndexOf(const AValue: string; StartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  114. Function IndexOfUnQuoted(const AValue: string; StartQuote, EndQuote: Char; StartIndex: SizeInt = 0): SizeInt; overload;
  115. Function IndexOfAny(const AnyOf: array of Char): SizeInt; overload;
  116. Function IndexOfAny(const AnyOf: array of Char; StartIndex: SizeInt): SizeInt; overload;
  117. Function IndexOfAny(const AnyOf: array of Char; StartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  118. Function IndexOfAny(const AnyOf: array of String): SizeInt; overload;
  119. Function IndexOfAny(const AnyOf: array of String; StartIndex: SizeInt): SizeInt; overload;
  120. Function IndexOfAny(const AnyOf: array of String; StartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  121. Function IndexOfAny(const AnyOf: array of String; StartIndex: SizeInt; ACount: SizeInt; Out AMatch : SizeInt): SizeInt; overload;
  122. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char): SizeInt; overload;
  123. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char; StartIndex: SizeInt): SizeInt; overload;
  124. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char; StartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  125. function IndexOfAnyUnquoted(const AnyOf: array of string; StartQuote, EndQuote: Char; StartIndex: SizeInt; Out Matched: SizeInt): SizeInt; overload;
  126. Function Insert(StartIndex: SizeInt; const AValue: string): string;
  127. Function IsDelimiter(const Delimiters: string; Index: SizeInt): Boolean;
  128. Function IsEmpty: Boolean;
  129. Function LastDelimiter(const Delims: string): SizeInt;
  130. Function LastIndexOf(AValue: Char): SizeInt; overload;
  131. Function LastIndexOf(const AValue: string): SizeInt; overload;
  132. Function LastIndexOf(AValue: Char; AStartIndex: SizeInt): SizeInt; overload;
  133. Function LastIndexOf(const AValue: string; AStartIndex: SizeInt): SizeInt; overload;
  134. Function LastIndexOf(AValue: Char; AStartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  135. Function LastIndexOf(const AValue: string; AStartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  136. Function LastIndexOfAny(const AnyOf: array of Char): SizeInt; overload;
  137. Function LastIndexOfAny(const AnyOf: array of Char; AStartIndex: SizeInt): SizeInt; overload;
  138. Function LastIndexOfAny(const AnyOf: array of Char; AStartIndex: SizeInt; ACount: SizeInt): SizeInt; overload;
  139. Function PadLeft(ATotalWidth: SizeInt): string; overload; inline;
  140. Function PadLeft(ATotalWidth: SizeInt; PaddingChar: Char): string; overload; inline;
  141. Function PadRight(ATotalWidth: SizeInt): string; overload; inline;
  142. Function PadRight(ATotalWidth: SizeInt; PaddingChar: Char): string; overload; inline;
  143. Function QuotedString: string; overload;
  144. Function QuotedString(const AQuoteChar: Char): string; overload;
  145. Function Remove(StartIndex: SizeInt): string; overload; inline;
  146. Function Remove(StartIndex: SizeInt; ACount: SizeInt): string; overload; inline;
  147. Function Replace(OldChar: Char; NewChar: Char): string; overload;
  148. Function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; overload;
  149. Function Replace(const OldValue: string; const NewValue: string): string; overload;
  150. Function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; overload;
  151. Function Split(const Separators: array of Char): TStringArray; overload;
  152. Function Split(const Separators: array of Char; ACount: SizeInt): TStringArray; overload;
  153. Function Split(const Separators: array of Char; Options: TStringSplitOptions): TStringArray; overload;
  154. Function Split(const Separators: array of Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray; overload;
  155. Function Split(const Separators: array of string): TStringArray; overload;
  156. Function Split(const Separators: array of string; ACount: SizeInt): TStringArray; overload;
  157. Function Split(const Separators: array of string; Options: TStringSplitOptions): TStringArray; overload;
  158. Function Split(const Separators: array of string; ACount: SizeInt; Options: TStringSplitOptions): TStringArray; overload;
  159. Function Split(const Separators: array of Char; AQuote: Char): TStringArray; overload;
  160. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
  161. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
  162. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: SizeInt): TStringArray; overload;
  163. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray; overload;
  164. Function Split(const Separators: array of string; AQuote: Char): TStringArray; overload;
  165. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
  166. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
  167. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: SizeInt): TStringArray; overload;
  168. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: SizeInt; Options: TStringSplitOptions): TStringArray; overload;
  169. Function StartsWith(const AValue: string): Boolean; overload; inline;
  170. Function StartsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
  171. Function Substring(AStartIndex: SizeInt): string; overload;
  172. Function Substring(AStartIndex: SizeInt; ALen: SizeInt): string; overload;
  173. Function ToBoolean: Boolean; overload; inline;
  174. Function ToInteger: Integer; overload; inline;
  175. Function ToInt64: Int64; overload; inline;
  176. Function ToSingle: Single; overload; inline;
  177. Function ToDouble: Double; overload; inline;
  178. Function ToExtended: Extended; overload; inline;
  179. Function ToCharArray: TCharArray; overload;
  180. Function ToCharArray(AStartIndex: SizeInt; ALen: SizeInt): TCharArray; overload;
  181. Function ToLower: string; overload; inline;
  182. Function ToLowerInvariant: string;
  183. Function ToUpper: string; overload; inline;
  184. Function ToUpperInvariant: string; inline;
  185. Function Trim: string; overload;
  186. Function TrimLeft: string; overload;
  187. Function TrimRight: string; overload;
  188. Function Trim(const ATrimChars: array of Char): string; overload;
  189. Function TrimLeft(const ATrimChars: array of Char): string; overload;
  190. Function TrimRight(const ATrimChars: array of Char): string; overload;
  191. Function TrimEnd(const ATrimChars: array of Char): string; deprecated 'Use TrimRight';
  192. Function TrimStart(const ATrimChars: array of Char): string; deprecated 'Use TrimLeft';
  193. property Chars[AIndex: SizeInt]: Char read GetChar;
  194. property Length: SizeInt read GetLength;
  195. end;
  196. {$IFDEF FPC_HAS_TYPE_SINGLE}
  197. TSingleHelper = Type Helper for Single
  198. Private
  199. Function GetB(AIndex: Cardinal): Byte;
  200. Function GetW(AIndex: Cardinal): Word;
  201. Function GetE: QWord; inline;
  202. Function GetF: QWord; inline;
  203. Function GetS: Boolean; inline;
  204. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  205. procedure SetW(AIndex: Cardinal; const AValue: Word);
  206. procedure SetE(AValue: QWord);
  207. procedure SetF(AValue: QWord);
  208. procedure SetS(AValue: Boolean);
  209. public
  210. {$push}
  211. {$R-}
  212. {$Q-}
  213. const
  214. Epsilon : Single = 1.4012984643248170709e-45;
  215. MaxValue : Single = 340282346638528859811704183484516925440.0;
  216. MinValue : Single = -340282346638528859811704183484516925440.0;
  217. PositiveInfinity : Single = 1.0/0.0;
  218. NegativeInfinity : Single = -1.0/0.0;
  219. NaN : Single = 0.0/0.0;
  220. {$POP}
  221. Class Function IsNan(const AValue: Single): Boolean; overload; inline; static;
  222. Class Function IsInfinity(const AValue: Single): Boolean; overload; inline; static;
  223. Class Function IsNegativeInfinity(const AValue: Single): Boolean; overload; inline; static;
  224. Class Function IsPositiveInfinity(const AValue: Single): Boolean; overload; inline; static;
  225. Class Function Parse(const AString: string): Single; overload; inline; static;
  226. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Single; overload; inline; static;
  227. Class Function Size: Integer; inline; static;
  228. Class Function ToString(const AValue: Single): string; overload; inline; static;
  229. Class Function ToString(const AValue: Single; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  230. Class Function ToString(const AValue: Single; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  231. Class Function ToString(const AValue: Single; const AFormat: TFloatFormat; const APrecision, ADigits: Integer;
  232. const AFormatSettings: TFormatSettings): string; overload; inline; static;
  233. Class Function TryParse(const AString: string; out AValue: Single): Boolean; overload; inline; static;
  234. Class Function TryParse(const AString: string; out AValue: Single; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  235. Procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; const AExponent: Integer);
  236. Function Exponent: Integer;
  237. Function Fraction: Extended;
  238. Function IsInfinity: Boolean; overload; inline;
  239. Function IsNan: Boolean; overload; inline;
  240. Function IsNegativeInfinity: Boolean; overload; inline;
  241. Function IsPositiveInfinity: Boolean; overload; inline;
  242. Function Mantissa: QWord;
  243. Function SpecialType: TFloatSpecial;
  244. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  245. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  246. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  247. Function ToString: string; overload; inline;
  248. property Bytes [AIndex: Cardinal]: Byte read GetB write SetB;
  249. property Words[AIndex: Cardinal]: Word read GetW write SetW;
  250. property Sign: Boolean read GetS write SetS;
  251. property Exp: QWord read GetE write SetE;
  252. property Frac: QWord read GetF write SetF;
  253. end;
  254. {$ENDIF FPC_HAS_TYPE_SINGLE}
  255. {$IFDEF FPC_HAS_TYPE_DOUBLE}
  256. TDoubleHelper = Type Helper for Double
  257. private
  258. Function GetB(AIndex: Cardinal): Byte;
  259. Function GetW(AIndex: Cardinal): Word;
  260. Function GetE: QWord; inline;
  261. Function GetF: QWord; inline;
  262. Function GetS: Boolean; inline;
  263. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  264. procedure SetW(AIndex: Cardinal; const AValue: Word);
  265. procedure SetE(AValue: QWord);
  266. procedure SetF(AValue: QWord);
  267. procedure SetS(AValue: Boolean);
  268. public
  269. const
  270. {$push}
  271. {$R-}
  272. {$Q-}
  273. Epsilon : Double = 4.9406564584124654418e-324;
  274. MaxValue : Double = 1.7976931348623157081e+308;
  275. MinValue : Double = -1.7976931348623157081e+308;
  276. PositiveInfinity : Double = 1.0/0.0;
  277. NegativeInfinity : Double = -1.0/0.0;
  278. NaN : Double = 0.0/0.0;
  279. {$POP}
  280. Class Function IsInfinity(const AValue: Double): Boolean; overload; inline; static;
  281. Class Function IsNan(const AValue: Double): Boolean; overload; inline; static;
  282. Class Function IsNegativeInfinity(const AValue: Double): Boolean; overload; inline; static;
  283. Class Function IsPositiveInfinity(const AValue: Double): Boolean; overload; inline; static;
  284. Class Function Parse(const AString: string): Double; overload; inline; static;
  285. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Double; overload; inline; static;
  286. Class Function Size: Integer; inline; static;
  287. Class Function ToString(const AValue: Double): string; overload; inline; static;
  288. Class Function ToString(const AValue: Double; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  289. Class Function ToString(const AValue: Double; const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  290. Class Function ToString(const AValue: Double; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  291. Class Function TryParse(const AString: string; out AValue: Double): Boolean; overload; inline; static;
  292. Class Function TryParse(const AString: string; out AValue: Double; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  293. Procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; const AExponent: Integer);
  294. Function Exponent: Integer;
  295. Function Fraction: Extended;
  296. Function IsInfinity: Boolean; overload; inline;
  297. Function IsNan: Boolean; overload; inline;
  298. Function IsNegativeInfinity: Boolean; overload; inline;
  299. Function IsPositiveInfinity: Boolean; overload; inline;
  300. Function Mantissa: QWord;
  301. Function SpecialType: TFloatSpecial;
  302. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  303. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  304. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  305. Function ToString: string; overload; inline;
  306. property Bytes [AIndex: Cardinal]: Byte read GetB write SetB; // 0..7
  307. property Words [AIndex: Cardinal]: Word read GetW write SetW; // 0..3
  308. property Sign: Boolean read GetS write SetS;
  309. property Exp: QWord read GetE write SetE;
  310. property Frac: QWord read GetF write SetF;
  311. end;
  312. {$ENDIF FPC_HAS_TYPE_DOUBLE}
  313. {$ifdef FPC_HAS_TYPE_EXTENDED}
  314. TExtendedHelper = Type Helper for Extended
  315. private
  316. Function GetB(AIndex: Cardinal): Byte;
  317. Function GetW(AIndex: Cardinal): Word;
  318. Function GetE: QWord; inline;
  319. Function GetF: QWord; inline;
  320. Function GetS: Boolean; inline;
  321. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  322. procedure SetW(AIndex: Cardinal; const AValue: Word);
  323. procedure SetE(AValue: QWord);
  324. procedure SetF(AValue: QWord);
  325. procedure SetS(AValue: Boolean);
  326. public
  327. {$push}
  328. {$R-}
  329. {$Q-}
  330. const
  331. Epsilon : Extended = 3.64519953188247460253e-4951;
  332. MaxValue : Extended = 1.18973149535723176505e+4932;
  333. MinValue : Extended = -1.18973149535723176505e+4932;
  334. PositiveInfinity : Extended = 1.0/0.0;
  335. NegativeInfinity : Extended = -1.0/0.0;
  336. NaN : Extended = 0.0/0.0;
  337. {$POP}
  338. Class Function ToString(const AValue: Extended): string; overload; inline; static;
  339. Class Function ToString(const AValue: Extended; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  340. Class Function ToString(const AValue: Extended; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  341. Class Function ToString(const AValue: Extended; const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  342. Class Function Parse(const AString: string): Extended; overload; inline; static;
  343. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Extended; overload; inline; static;
  344. Class Function TryParse(const AString: string; out AValue: Extended): Boolean; overload; inline; static;
  345. Class Function TryParse(const AString: string; out AValue: Extended; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  346. Class Function IsNan(const AValue: Extended): Boolean; overload; inline; static;
  347. Class Function IsInfinity(const AValue: Extended): Boolean; overload; inline; static;
  348. Class Function IsNegativeInfinity(const AValue: Extended): Boolean; overload; inline; static;
  349. Class Function IsPositiveInfinity(const AValue: Extended): Boolean; overload; inline; static;
  350. Class Function Size: Integer; inline; static;
  351. procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; Const AExponent: Integer);
  352. Function Exponent: Integer;
  353. Function Fraction: Extended;
  354. Function IsInfinity: Boolean; overload; inline;
  355. Function IsNan: Boolean; overload; inline;
  356. Function IsNegativeInfinity: Boolean; overload; inline;
  357. Function IsPositiveInfinity: Boolean; overload; inline;
  358. Function Mantissa: QWord;
  359. Function SpecialType: TFloatSpecial;
  360. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  361. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  362. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  363. Function ToString: string; overload; inline;
  364. property Bytes [AIndex: Cardinal]: Byte read GetB write SetB;
  365. property Words [AIndex: Cardinal]: Word read GetW write SetW;
  366. property Sign: Boolean read GetS write SetS;
  367. property Exp: QWord read GetE write SetE;
  368. property Frac: QWord read GetF write SetF;
  369. end;
  370. {$ENDIF FPC_HAS_TYPE_EXTENDED}
  371. nibble = 0..15;
  372. TNibbleBitIndex = 0..3;
  373. // TByteBitIndex is already defined
  374. TByteNibbleIndex = 0..1;
  375. TByteOverlay = bitpacked record case integer of // for fast extraction of bits
  376. 0: (AsBit: bitpacked array[TByteBitIndex] of boolean);
  377. 1: (AsNibble: bitpacked array[TByteNibbleIndex] of nibble);
  378. 2: (AsByte: byte);
  379. end;
  380. TByteHelper = Type Helper for Byte
  381. const
  382. MaxValue = High(byte);
  383. MinValue = Low(byte);
  384. MaxBit = High(TByteBitIndex);
  385. MinBit = Low(TByteBitIndex);
  386. MaxNibble = High(TByteNibbleIndex);
  387. MinNibble = Low(TByteNibbleIndex);
  388. public
  389. Class Function Parse(const AString: string): Byte; inline; static;
  390. Class Function Size: Integer; inline; static;
  391. Class Function ToString(const AValue: Byte): string; overload; inline; static;
  392. Class Function TryParse(const AString: string; out AValue: Byte): Boolean; inline; static;
  393. protected
  394. function GetBit(const aIndex: TByteBitIndex): boolean; inline;
  395. procedure PutBit(const aIndex: TByteBitIndex; const aNewValue: boolean); inline;
  396. function GetNibble(const aIndex: TByteNibbleIndex): nibble; inline;
  397. procedure PutNibble(const aIndex: TByteNibbleIndex; const aNewValue: nibble); inline;
  398. public
  399. Function ToBoolean: Boolean; inline;
  400. Function ToDouble: Double; inline;
  401. Function ToExtended: Extended; inline;
  402. Function ToBinString:string;
  403. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  404. Function ToHexString: string; overload; inline;
  405. Function ToSingle: Single; inline;
  406. Function ToString: string; overload; inline;
  407. Function SetBit(const Index: TByteBitIndex) : Byte; inline;
  408. Function ClearBit(const Index: TByteBitIndex) : Byte; inline;
  409. Function ToggleBit(const Index: TByteBitIndex) : Byte; inline;
  410. Function TestBit(const Index:TByteBitIndex):Boolean; inline;
  411. procedure Clear; inline;
  412. function HighestSetBitPos: cardinal; inline;
  413. function LowestSetBitPos: cardinal; inline;
  414. function SetBitsCount: byte; inline;
  415. property Bits [aIndex: TByteBitIndex]: boolean read GetBit write PutBit;
  416. property Nibbles [aIndex: TByteNibbleIndex]: nibble read GetNibble write PutNibble;
  417. end;
  418. // TShortIntBitIndex is already defined
  419. TShortIntNibbleIndex = TByteNibbleIndex;
  420. TShortIntOverlay = TByteOverlay;
  421. TShortIntHelper = Type Helper for ShortInt
  422. const
  423. MaxValue = High(shortint);
  424. MinValue = Low(shortint);
  425. MaxBit = High(TShortIntBitIndex);
  426. MinBit = Low(TShortIntBitIndex);
  427. MaxNibble = High(TShortIntNibbleIndex);
  428. MinNibble = Low(TShortIntNibbleIndex);
  429. public
  430. Class Function Parse(const AString: string): ShortInt; inline; static;
  431. Class Function Size: Integer; inline; static;
  432. Class Function ToString(const AValue: ShortInt): string; overload; inline; static;
  433. Class Function TryParse(const AString: string; out AValue: ShortInt): Boolean; inline; static;
  434. protected
  435. function GetBit(const aIndex: TShortIntBitIndex): boolean; inline;
  436. procedure PutBit(const aIndex: TShortIntBitIndex; const aNewValue: boolean); inline;
  437. function GetNibble(const aIndex: TShortIntNibbleIndex): nibble; inline;
  438. procedure PutNibble(const aIndex: TShortIntNibbleIndex; const aNewValue: nibble); inline;
  439. public
  440. Function ToBoolean: Boolean; inline;
  441. Function ToDouble: Double; inline;
  442. Function ToExtended: Extended; inline;
  443. Function ToBinString:string; inline;
  444. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  445. Function ToHexString: string; overload; inline;
  446. Function ToSingle: Single; inline;
  447. Function ToString: string; overload; inline;
  448. Function SetBit(const Index: TShortIntBitIndex): Shortint; inline;
  449. Function ClearBit(const Index: TShortIntBitIndex): Shortint; inline;
  450. Function ToggleBit(const Index: TShortIntBitIndex): Shortint; inline;
  451. Function TestBit(const Index:TShortIntBitIndex):Boolean;
  452. procedure Clear; inline;
  453. function HighestSetBitPos: cardinal; inline;
  454. function LowestSetBitPos: cardinal; inline;
  455. function SetBitsCount: byte; inline;
  456. property Bits [aIndex: TShortIntBitIndex]: boolean read GetBit write PutBit;
  457. property Nibbles [aIndex: TShortIntNibbleIndex]: nibble read GetNibble write PutNibble;
  458. end;
  459. // TWordBitIndex is already defined
  460. TWordByteIndex = 0..1;
  461. TWordNibbleIndex = 0..3;
  462. TWordOverlay = bitpacked record case integer of // for fast extraction of bytes and bits
  463. 0: (AsBit: bitpacked array[TWordBitIndex] of boolean);
  464. 1: (AsNibble: bitpacked array[TWordNibbleIndex] of nibble);
  465. 2: (AsByte: array[TWordByteIndex] of byte);
  466. 3: (AsWord: word);
  467. // recursive overlay:
  468. 4: (AsByteOverlay: array[TWordByteIndex] of TByteOverlay);
  469. end;
  470. TWordHelper = Type Helper for Word
  471. const
  472. MaxValue = High(word);
  473. MinValue = Low(word);
  474. MaxBit = High(TWordBitIndex);
  475. MinBit = Low(TWordBitIndex);
  476. MaxNibble = High(TWordNibbleIndex);
  477. MinNibble = Low(TWordNibbleIndex);
  478. MaxByte = High(TWordByteIndex);
  479. MinByte = Low(TWordByteIndex);
  480. public
  481. Class Function Parse(const AString: string): Word; inline; static;
  482. Class Function Size: Integer; inline; static;
  483. Class Function ToString(const AValue: Word): string; overload; inline; static;
  484. Class Function TryParse(const AString: string; out AValue: Word): Boolean; inline; static;
  485. protected
  486. function GetBit(const aIndex: TWordBitIndex): boolean; inline;
  487. procedure PutBit(const aIndex: TWordBitIndex; const aNewValue: boolean); inline;
  488. function GetNibble(const aIndex: TWordNibbleIndex): nibble; inline;
  489. procedure PutNibble(const aIndex: TWordNibbleIndex; const aNewValue: nibble); inline;
  490. function GetByte(const aIndex: TWordByteIndex): byte;
  491. procedure PutByte(const aIndex: TWordByteIndex; const aNewValue: byte);
  492. public
  493. Function ToBoolean: Boolean; inline;
  494. Function ToDouble: Double; inline;
  495. Function ToExtended: Extended; inline;
  496. Function ToBinString:string; inline;
  497. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  498. Function ToHexString: string; overload; inline;
  499. Function ToSingle: Single; inline;
  500. Function ToString: string; overload; inline;
  501. Function SetBit(const Index: TWordBitIndex) : Word; inline;
  502. Function ClearBit(const Index: TWordBitIndex) : Word; inline;
  503. Function ToggleBit(const Index: TWordBitIndex) : Word; inline;
  504. Function TestBit(const Index:TWordBitIndex):Boolean; inline;
  505. procedure Clear; inline;
  506. function HighestSetBitPos: cardinal; inline;
  507. function LowestSetBitPos: cardinal; inline;
  508. function SetBitsCount: byte; inline;
  509. property Bits [aIndex: TWordBitIndex]: boolean read GetBit write PutBit;
  510. property Nibbles [aIndex: TWordNibbleIndex]: nibble read GetNibble write PutNibble;
  511. property Bytes [aIndex: TWordByteIndex]: byte read GetByte write PutByte;
  512. end;
  513. // TSmallIntBitIndex is already defined
  514. TSmallIntByteIndex = TWordByteIndex;
  515. TSmallIntNibbleIndex = TWordNibbleIndex;
  516. TSmallIntOverlay = TWordOverlay;
  517. TSmallIntHelper = Type Helper for SmallInt
  518. const
  519. MaxValue = High(SmallInt);
  520. MinValue = Low(SmallInt);
  521. MaxBit = High(TSmallIntBitIndex);
  522. MinBit = Low(TSmallIntBitIndex);
  523. MaxNibble = High(TSmallIntNibbleIndex);
  524. MinNibble = Low(TSmallIntNibbleIndex);
  525. MaxByte = High(TSmallIntByteIndex);
  526. MinByte = Low(TSmallIntByteIndex);
  527. public
  528. Class Function Parse(const AString: string): SmallInt; inline; static;
  529. Class Function Size: Integer; inline; static;
  530. Class Function ToString(const AValue: SmallInt): string; overload; inline; static;
  531. Class Function TryParse(const AString: string; out AValue: SmallInt): Boolean; inline; static;
  532. protected
  533. function GetBit(const aIndex: TSmallIntBitIndex): boolean; inline;
  534. procedure PutBit(const aIndex: TSmallIntBitIndex; const aNewValue: boolean); inline;
  535. function GetNibble(const aIndex: TSmallIntNibbleIndex): nibble; inline;
  536. procedure PutNibble(const aIndex: TSmallIntNibbleIndex; const aNewValue: nibble); inline;
  537. function GetByte(const aIndex: TSmallIntByteIndex): byte;
  538. procedure PutByte(const aIndex: TSmallIntByteIndex; const aNewValue: byte);
  539. public
  540. Function ToString: string; overload; inline;
  541. Function ToBoolean: Boolean; inline;
  542. Function ToBinString:string; inline;
  543. Function ToHexString: string; overload; inline;
  544. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  545. Function ToSingle: Single; inline;
  546. Function ToDouble: Double; inline;
  547. Function ToExtended: Extended; inline;
  548. Function SetBit(const Index: TSmallIntBitIndex) : Smallint; inline;
  549. Function ClearBit(const Index: TSmallIntBitIndex) : Smallint; inline;
  550. Function ToggleBit(const Index: TSmallIntBitIndex) : Smallint; inline;
  551. Function TestBit(const Index:TSmallIntBitIndex):Boolean;
  552. procedure Clear; inline;
  553. function HighestSetBitPos: cardinal; inline;
  554. function LowestSetBitPos: cardinal; inline;
  555. function SetBitsCount: byte; inline;
  556. property Bits [aIndex: TSmallIntBitIndex]: boolean read GetBit write PutBit;
  557. property Nibbles [aIndex: TSmallIntNibbleIndex]: nibble read GetNibble write PutNibble;
  558. property Bytes [aIndex: TSmallIntByteIndex]: byte read GetByte write PutByte;
  559. end;
  560. TDwordBitIndex = TCardinalBitIndex;
  561. TDwordNibbleIndex = 0..7;
  562. TDwordByteIndex = 0..3;
  563. TDwordWordIndex = 0..1;
  564. TDwordOverlay = bitpacked record case integer of // for fast extraction of words, bytes and bits
  565. 0: (AsBit: bitpacked array[TDwordBitIndex] of boolean);
  566. 1: (AsNibble: bitpacked array[TDwordNibbleIndex] of nibble);
  567. 2: (AsByte: array[TDwordByteIndex] of byte);
  568. 3: (AsWord: array[TDwordWordIndex] of word);
  569. 4: (AsDword: dword);
  570. // recursive overlays:
  571. 5: (AsByteOverlay: array[TDwordByteIndex] of TByteOverlay);
  572. 6: (AsWordOverlay: array[TDwordWordIndex] of TWordOverlay);
  573. end;
  574. // TCardinalBitIndex is already defined
  575. TCardinalNibbleIndex = TDwordNibbleIndex;
  576. TCardinalByteIndex = TDwordByteIndex;
  577. TCardinalWordIndex = TDwordWordIndex;
  578. TCardinalOverlay = TDwordOverlay;
  579. TCardinalHelper = Type Helper for Cardinal { for LongWord Type too }
  580. const
  581. MaxValue = High(Cardinal);
  582. MinValue = Low(Cardinal);
  583. MaxBit = High(TCardinalBitIndex);
  584. MinBit = Low(TCardinalBitIndex);
  585. MaxNibble = High(TCardinalNibbleIndex);
  586. MinNibble = Low(TCardinalNibbleIndex);
  587. MaxByte = High(TCardinalByteIndex);
  588. MinByte = Low(TCardinalByteIndex);
  589. MaxWord = High(TCardinalWordIndex);
  590. MinWord = Low(TCardinalWordIndex);
  591. public
  592. Class Function Parse(const AString: string): Cardinal; inline; static;
  593. Class Function Size: Integer; inline; static;
  594. Class Function ToString(const AValue: Cardinal): string; overload; inline; static;
  595. Class Function TryParse(const AString: string; out AValue: Cardinal): Boolean; inline; static;
  596. protected
  597. function GetBit(const aIndex: TCardinalBitIndex): boolean; inline;
  598. procedure PutBit(const aIndex: TCardinalBitIndex; const aNewValue: boolean); inline;
  599. function GetNibble(const aIndex: TCardinalNibbleIndex): nibble; inline;
  600. procedure PutNibble(const aIndex: TCardinalNibbleIndex; const aNewValue: nibble); inline;
  601. function GetByte(const aIndex: TCardinalByteIndex): byte;
  602. procedure PutByte(const aIndex: TCardinalByteIndex; const aNewValue: byte);
  603. function GetWord(const aIndex: TCardinalWordIndex): word;
  604. procedure PutWord(const aIndex: TCardinalWordIndex; const aNewValue: word);
  605. public
  606. Function ToBoolean: Boolean; inline;
  607. Function ToDouble: Double; inline;
  608. Function ToExtended: Extended; inline;
  609. Function ToBinString:string; inline;
  610. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  611. Function ToHexString: string; overload; inline;
  612. Function ToSingle: Single; inline;
  613. Function ToString: string; overload; inline;
  614. Function SetBit(const Index: TCardinalBitIndex) : Cardinal; inline;
  615. Function ClearBit(const Index: TCardinalBitIndex) : Cardinal; inline;
  616. Function ToggleBit(const Index: TCardinalBitIndex) : Cardinal; inline;
  617. Function TestBit(const Index:TCardinalBitIndex):Boolean; inline;
  618. procedure Clear; inline;
  619. function HighestSetBitPos: cardinal; inline;
  620. function LowestSetBitPos: cardinal; inline;
  621. function SetBitsCount: byte; inline;
  622. property Bits [aIndex: TCardinalBitIndex]: boolean read GetBit write PutBit;
  623. property Nibbles[aIndex: TCardinalNibbleIndex]: nibble read GetNibble write PutNibble;
  624. property Bytes [aIndex: TCardinalByteIndex]: byte read GetByte write PutByte;
  625. property Words [aIndex: TCardinalWordIndex]: word read GetWord write PutWord;
  626. end;
  627. // TIntegerBitIndex is already defined
  628. {$if sizeof(Integer)=2}
  629. TIntegerNibbleIndex = TWordNibbleIndex;
  630. TIntegerByteIndex = TWordByteIndex;
  631. TIntegerOverlay = TWordOverlay;
  632. {$elseif sizeof(Integer)=4}
  633. TIntegerNibbleIndex = TDwordNibbleIndex;
  634. TIntegerByteIndex = TDwordByteIndex;
  635. TIntegerWordIndex = TDwordWordIndex;
  636. TIntegerOverlay = TDwordOverlay;
  637. {$else}
  638. {$fatal Unsupported Integer type size}
  639. {$endif}
  640. TIntegerHelper = Type Helper for Integer
  641. const
  642. MaxValue = High(integer);
  643. MinValue = Low(integer);
  644. MaxBit = High(TIntegerBitIndex);
  645. MinBit = Low(TIntegerBitIndex);
  646. MaxNibble = High(TIntegerNibbleIndex);
  647. MinNibble = Low(TIntegerNibbleIndex);
  648. MaxByte = High(TIntegerByteIndex);
  649. MinByte = Low(TIntegerByteIndex);
  650. {$if sizeof(Integer)>=4}
  651. MaxWord = High(TIntegerWordIndex);
  652. MinWord = Low(TIntegerWordIndex);
  653. {$endif}
  654. public
  655. Class Function Size: Integer; inline; static;
  656. Class Function ToString(const AValue: Integer): string; overload; inline; static;
  657. Class Function Parse(const AString: string): Integer; inline; static;
  658. Class Function TryParse(const AString: string; out AValue: Integer): Boolean; inline; static;
  659. protected
  660. function GetBit(const aIndex: TIntegerBitIndex): boolean; inline;
  661. procedure PutBit(const aIndex: TIntegerBitIndex; const aNewValue: boolean); inline;
  662. function GetNibble(const aIndex: TIntegerNibbleIndex): nibble; inline;
  663. procedure PutNibble(const aIndex: TIntegerNibbleIndex; const aNewValue: nibble); inline;
  664. function GetByte(const aIndex: TIntegerByteIndex): byte;
  665. procedure PutByte(const aIndex: TIntegerByteIndex; const aNewValue: byte);
  666. {$if sizeof(Integer)>=4}
  667. function GetWord(const aIndex: TIntegerWordIndex): word;
  668. procedure PutWord(const aIndex: TIntegerWordIndex; const aNewValue: word);
  669. {$endif}
  670. public
  671. Function ToBoolean: Boolean; inline;
  672. Function ToDouble: Double; inline;
  673. Function ToExtended: Extended; inline;
  674. Function ToBinString:string; inline;
  675. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  676. Function ToHexString: string; overload; inline;
  677. Function ToSingle: Single; inline;
  678. Function ToString: string; overload; inline;
  679. Function SetBit(const Index: TIntegerBitIndex) : Integer; inline;
  680. Function ClearBit(const Index: TIntegerBitIndex) : Integer; inline;
  681. Function ToggleBit(const Index: TIntegerBitIndex) : Integer; inline;
  682. Function TestBit(const Index:TIntegerBitIndex):Boolean; inline;
  683. procedure Clear; inline;
  684. function HighestSetBitPos: cardinal; inline;
  685. function LowestSetBitPos: cardinal; inline;
  686. function SetBitsCount: byte; inline;
  687. property Bits [aIndex: TIntegerBitIndex]: boolean read GetBit write PutBit;
  688. property Nibbles[aIndex: TIntegerNibbleIndex]: nibble read GetNibble write PutNibble;
  689. property Bytes [aIndex: TIntegerByteIndex]: byte read GetByte write PutByte;
  690. {$if sizeof(Integer)>=4}
  691. property Words [aIndex: TIntegerWordIndex]: word read GetWord write PutWord;
  692. {$endif}
  693. end;
  694. // TLongIntBitIndex is already defined
  695. TLongIntNibbleIndex = TDwordNibbleIndex;
  696. TLongIntByteIndex = TDwordByteIndex;
  697. TLongIntWordIndex = TDwordWordIndex;
  698. TLongIntOverlay = TDwordOverlay;
  699. TLongIntHelper = Type Helper for LongInt
  700. const
  701. MaxValue = High(LongInt);
  702. MinValue = Low(LongInt);
  703. MaxBit = High(TLongIntBitIndex);
  704. MinBit = Low(TLongIntBitIndex);
  705. MaxNibble = High(TLongIntNibbleIndex);
  706. MinNibble = Low(TLongIntNibbleIndex);
  707. MaxByte = High(TLongIntByteIndex);
  708. MinByte = Low(TLongIntByteIndex);
  709. MaxWord = High(TLongIntWordIndex);
  710. MinWord = Low(TLongIntWordIndex);
  711. public
  712. Class Function Size: Integer; inline; static;
  713. Class Function ToString(const AValue: LongInt): string; overload; inline; static;
  714. Class Function Parse(const AString: string): LongInt; inline; static;
  715. Class Function TryParse(const AString: string; out AValue: LongInt): Boolean; inline; static;
  716. protected
  717. function GetBit(const aIndex: TLongIntBitIndex): boolean; inline;
  718. procedure PutBit(const aIndex: TLongIntBitIndex; const aNewValue: boolean); inline;
  719. function GetNibble(const aIndex: TLongIntNibbleIndex): nibble; inline;
  720. procedure PutNibble(const aIndex: TLongIntNibbleIndex; const aNewValue: nibble); inline;
  721. function GetByte(const aIndex: TLongIntByteIndex): byte;
  722. procedure PutByte(const aIndex: TLongIntByteIndex; const aNewValue: byte);
  723. function GetWord(const aIndex: TLongIntWordIndex): word;
  724. procedure PutWord(const aIndex: TLongIntWordIndex; const aNewValue: word);
  725. public
  726. Function ToBoolean: Boolean; inline;
  727. Function ToDouble: Double; inline;
  728. Function ToExtended: Extended; inline;
  729. Function ToBinString:string; inline;
  730. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  731. Function ToHexString: string; overload; inline;
  732. Function ToSingle: Single; inline;
  733. Function ToString: string; overload; inline;
  734. Function SetBit(const Index: TLongIntBitIndex) : LongInt; inline;
  735. Function ClearBit(const Index: TLongIntBitIndex) : LongInt; inline;
  736. Function ToggleBit(const Index: TLongIntBitIndex) : LongInt; inline;
  737. Function TestBit(const Index:TLongIntBitIndex):Boolean; inline;
  738. procedure Clear; inline;
  739. function HighestSetBitPos: cardinal; inline;
  740. function LowestSetBitPos: cardinal; inline;
  741. function SetBitsCount: byte; inline;
  742. property Bits [aIndex: TLongIntBitIndex]: boolean read GetBit write PutBit;
  743. property Nibbles[aIndex: TLongIntNibbleIndex]: nibble read GetNibble write PutNibble;
  744. property Bytes [aIndex: TLongIntByteIndex]: byte read GetByte write PutByte;
  745. property Words [aIndex: TLongIntWordIndex]: word read GetWord write PutWord;
  746. end;
  747. // TQwordBitIndex is already defined
  748. TQwordNibbleIndex = 0..15;
  749. TQwordByteIndex = 0..7;
  750. TQwordWordIndex = 0..3;
  751. TQwordDwordIndex = 0..1;
  752. TQwordOverlay = bitpacked record case integer of // for fast extraction of longwords, words, bytes and bits
  753. 0: (AsBit: bitpacked array[TQwordBitIndex] of boolean);
  754. 1: (AsNibble: bitpacked array[TQwordNibbleIndex] of nibble);
  755. 2: (AsByte: array[TQwordByteIndex] of byte);
  756. 3: (AsWord: array[TQwordWordIndex] of word);
  757. 4: (AsDword: array[TQwordDwordIndex] of dword);
  758. 5: (AsQword: qword);
  759. // recursive overlays:
  760. 6: (AsByteOverlay: array[TQwordByteIndex] of TByteOverlay);
  761. 7: (AsWordOverlay: array[TQwordWordIndex] of TWordOverlay);
  762. 8: (AsDwordOverlay: array[TQwordDwordIndex] of TDwordOverlay);
  763. end;
  764. TQWordHelper = Type Helper for QWord
  765. const
  766. MaxValue = High(qword);
  767. MinValue = Low(qword);
  768. MaxBit = High(TQwordBitIndex);
  769. MinBit = Low(TQwordBitIndex);
  770. MaxNibble = High(TQwordNibbleIndex);
  771. MinNibble = Low(TQwordNibbleIndex);
  772. MaxByte = High(TQwordByteIndex);
  773. MinByte = Low(TQwordByteIndex);
  774. MaxWord = High(TQwordWordIndex);
  775. MinWord = Low(TQwordWordIndex);
  776. MaxDword = High(TQwordDwordIndex);
  777. MinDword = Low(TQwordDwordIndex);
  778. public
  779. Class Function Parse(const AString: string): Qword; inline; static;
  780. Class Function Size: Integer; inline; static;
  781. Class Function ToString(const AValue: Qword): string; overload; inline; static;
  782. Class Function TryParse(const AString: string; out AValue: Qword): Boolean; inline; static;
  783. protected
  784. function GetBit(const aIndex: TQwordBitIndex): boolean; inline;
  785. procedure PutBit(const aIndex: TQwordBitIndex; const aNewValue: boolean); inline;
  786. function GetNibble(const aIndex: TQwordNibbleIndex): nibble; inline;
  787. procedure PutNibble(const aIndex: TQwordNibbleIndex; const aNewValue: nibble); inline;
  788. function GetByte(const aIndex: TQwordByteIndex): byte;
  789. procedure PutByte(const aIndex: TQwordByteIndex; const aNewValue: byte);
  790. function GetWord(const aIndex: TQwordWordIndex): word;
  791. procedure PutWord(const aIndex: TQwordWordIndex; const aNewValue: word);
  792. function GetDword(const aIndex: TQwordDwordIndex): dword;
  793. procedure PutDword(const aIndex: TQwordDwordIndex; const aNewValue: dword);
  794. public
  795. Function ToBoolean: Boolean; inline;
  796. Function ToDouble: Double; inline;
  797. Function ToExtended: Extended; inline;
  798. Function ToBinString:string; inline;
  799. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  800. Function ToHexString: string; overload; inline;
  801. Function ToSingle: Single; inline;
  802. Function ToString: string; overload; inline;
  803. Function SetBit(const Index: TQwordBitIndex): Qword; inline;
  804. Function ClearBit(const Index: TQwordBitIndex): Qword; inline;
  805. Function ToggleBit(const Index: TQwordBitIndex): Qword; inline;
  806. Function TestBit(const Index:TQwordBitIndex): Boolean; inline;
  807. procedure Clear; inline;
  808. function HighestSetBitPos: cardinal; inline;
  809. function LowestSetBitPos: cardinal; inline;
  810. function SetBitsCount: byte; inline;
  811. property Bits [aIndex: TQwordBitIndex]: boolean read GetBit write PutBit;
  812. property Nibbles[aIndex: TQwordNibbleIndex]: nibble read GetNibble write PutNibble;
  813. property Bytes [aIndex: TQwordByteIndex]: byte read GetByte write PutByte;
  814. property Words [aIndex: TQwordWordIndex]: word read GetWord write PutWord;
  815. property DWords [aIndex: TQwordDwordIndex]: dword read GetDWord write PutDWord;
  816. end;
  817. // TInt64BitIndex is already defined
  818. TInt64NibbleIndex = TQwordNibbleIndex;
  819. TInt64ByteIndex = TQwordByteIndex;
  820. TInt64WordIndex = TQwordWordIndex;
  821. TInt64DwordIndex = TQwordDwordIndex;
  822. TInt64Overlay = TQwordOverlay;
  823. TInt64Helper = Type Helper for Int64
  824. const
  825. MaxValue = High(int64);
  826. MinValue = Low(int64);
  827. MaxBit = High(TInt64BitIndex);
  828. MinBit = Low(TInt64BitIndex);
  829. MaxNibble = High(TInt64NibbleIndex);
  830. MinNibble = Low(TInt64NibbleIndex);
  831. MaxByte = High(TInt64ByteIndex);
  832. MinByte = Low(TInt64ByteIndex);
  833. MaxWord = High(TInt64WordIndex);
  834. MinWord = Low(TInt64WordIndex);
  835. MaxDword = High(TInt64DwordIndex);
  836. MinDword = Low(TInt64DwordIndex);
  837. public
  838. Class Function Parse(const AString: string): Int64; inline; static;
  839. Class Function Size: Integer; inline; static;
  840. Class Function ToString(const AValue: Int64): string; overload; inline; static;
  841. Class Function TryParse(const AString: string; out AValue: Int64): Boolean; inline; static;
  842. protected
  843. function GetBit(const aIndex: TInt64BitIndex): boolean; inline;
  844. procedure PutBit(const aIndex: TInt64BitIndex; const aNewValue: boolean); inline;
  845. function GetNibble(const aIndex: TInt64NibbleIndex): nibble; inline;
  846. procedure PutNibble(const aIndex: TInt64NibbleIndex; const aNewValue: nibble); inline;
  847. function GetByte(const aIndex: TInt64ByteIndex): byte;
  848. procedure PutByte(const aIndex: TInt64ByteIndex; const aNewValue: byte);
  849. function GetWord(const aIndex: TInt64WordIndex): word;
  850. procedure PutWord(const aIndex: TInt64WordIndex; const aNewValue: word);
  851. function GetDword(const aIndex: TInt64DwordIndex): dword;
  852. procedure PutDword(const aIndex: TInt64DwordIndex; const aNewValue: dword);
  853. public
  854. Function ToBoolean: Boolean; inline;
  855. Function ToDouble: Double; inline;
  856. Function ToExtended: Extended; inline;
  857. Function ToBinString:string; inline;
  858. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  859. Function ToHexString: string; overload; inline;
  860. Function ToSingle: Single; inline;
  861. Function ToString: string; overload; inline;
  862. Function SetBit(const Index: TInt64BitIndex) : Int64; inline;
  863. Function ClearBit(const Index: TInt64BitIndex) : Int64; inline;
  864. Function ToggleBit(const Index: TInt64BitIndex) : Int64; inline;
  865. Function TestBit(const Index: TInt64BitIndex): Boolean; inline;
  866. procedure Clear; inline;
  867. function HighestSetBitPos: cardinal; inline;
  868. function LowestSetBitPos: cardinal; inline;
  869. function SetBitsCount: byte; inline;
  870. property Bits [aIndex: TInt64BitIndex]: boolean read GetBit write PutBit;
  871. property Nibbles[aIndex: TInt64NibbleIndex]: nibble read GetNibble write PutNibble;
  872. property Bytes [aIndex: TInt64ByteIndex]: byte read GetByte write PutByte;
  873. property Words [aIndex: TInt64WordIndex]: word read GetWord write PutWord;
  874. property DWords [aIndex: TInt64DwordIndex]: dword read GetDWord write PutDWord;
  875. end;
  876. // TNativeIntBitIndex is already defined
  877. {$if sizeof(NativeInt)=2}
  878. TNativeIntNibbleIndex = TSmallIntNibbleIndex;
  879. TNativeIntByteIndex = TSmallIntByteIndex;
  880. TNativeIntOverlay = TSmallIntOverlay;
  881. {$elseif sizeof(NativeInt)=4}
  882. TNativeIntNibbleIndex = TLongIntNibbleIndex;
  883. TNativeIntByteIndex = TLongIntByteIndex;
  884. TNativeIntWordIndex = TLongIntWordIndex;
  885. TNativeIntOverlay = TLongIntOverlay;
  886. {$elseif sizeof(NativeInt)=8}
  887. TNativeIntNibbleIndex = TInt64NibbleIndex;
  888. TNativeIntByteIndex = TInt64ByteIndex;
  889. TNativeIntWordIndex = TInt64WordIndex;
  890. TNativeIntDwordIndex = TInt64DWordIndex;
  891. TNativeIntOverlay = TInt64Overlay;
  892. {$else}
  893. {$fatal Unsupported NativeInt type size}
  894. {$endif}
  895. TNativeIntHelper = Type Helper for NativeInt
  896. const
  897. MaxValue = High(NativeInt);
  898. MinValue = Low(NativeInt);
  899. MaxBit = High(TNativeIntBitIndex);
  900. MinBit = Low(TNativeIntBitIndex);
  901. MaxNibble = High(TNativeIntNibbleIndex);
  902. MinNibble = Low(TNativeIntNibbleIndex);
  903. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
  904. MaxByte = High(TNativeIntByteIndex);
  905. MinByte = Low(TNativeIntByteIndex);
  906. {$if sizeof(NativeInt)>=4}
  907. MaxWord = High(TNativeIntWordIndex);
  908. MinWord = Low(TNativeIntWordIndex);
  909. {$endif}
  910. {$ifdef sizeof(NativeInt)>=8}
  911. MaxDword = High(TNativeIntDwordIndex);
  912. MinDword = Low(TNativeIntDwordIndex);
  913. {$endif}
  914. public
  915. Class Function Parse(const AString: string): NativeInt; inline; static;
  916. Class Function Size: Integer; inline; static;
  917. Class Function ToString(const AValue: NativeInt): string; overload; inline; static;
  918. Class Function TryParse(const AString: string; out AValue: NativeInt): Boolean; inline; static;
  919. protected
  920. function GetBit(const aIndex: TNativeIntBitIndex): boolean; inline;
  921. procedure PutBit(const aIndex: TNativeIntBitIndex; const aNewValue: boolean); inline;
  922. function GetNibble(const aIndex: TNativeIntNibbleIndex): nibble; inline;
  923. procedure PutNibble(const aIndex: TNativeIntNibbleIndex; const aNewValue: nibble); inline;
  924. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
  925. function GetByte(const aIndex: TNativeIntByteIndex): byte;
  926. procedure PutByte(const aIndex: TNativeIntByteIndex; const aNewValue: byte);
  927. {$if sizeof(NativeInt)>=4}
  928. function GetWord(const aIndex: TNativeIntWordIndex): word;
  929. procedure PutWord(const aIndex: TNativeIntWordIndex; const aNewValue: word);
  930. {$endif}
  931. {$if sizeof(NativeInt)>=8}
  932. function GetDword(const aIndex: TNativeIntDwordIndex): dword;
  933. procedure PutDword(const aIndex: TNativeIntDwordIndex; const aNewValue: dword);
  934. {$endif}
  935. public
  936. Function ToBoolean: Boolean; inline;
  937. Function ToDouble: Double; inline;
  938. Function ToExtended: Extended; inline;
  939. Function ToBinString:string; inline;
  940. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  941. Function ToHexString: string; overload; inline;
  942. Function ToSingle: Single; inline;
  943. Function ToString: string; overload; inline;
  944. Function SetBit(const Index: TNativeIntBitIndex) : NativeInt; inline;
  945. Function ClearBit(const Index: TNativeIntBitIndex) : NativeInt; inline;
  946. Function ToggleBit(const Index: TNativeIntBitIndex) : NativeInt; inline;
  947. Function TestBit(const Index:TNativeIntBitIndex):Boolean; inline;
  948. procedure Clear; inline;
  949. function HighestSetBitPos: cardinal; inline;
  950. function LowestSetBitPos: cardinal; inline;
  951. function SetBitsCount: byte; inline;
  952. property Bits [aIndex: TNativeIntBitIndex]: boolean read GetBit write PutBit;
  953. property Nibbles [aIndex: TNativeIntNibbleIndex]: nibble read GetNibble write PutNibble;
  954. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
  955. property Bytes [aIndex: TNativeIntByteIndex]: byte read GetByte write PutByte;
  956. {$if sizeof(NativeInt)>=4}
  957. property Words [aIndex: TNativeIntWordIndex]: word read GetWord write PutWord;
  958. {$endif}
  959. {$if sizeof(NativeInt)>=8}
  960. property DWords [aIndex: TNativeIntDwordIndex]: dword read GetDWord write PutDWord;
  961. {$endif}
  962. end;
  963. // TNativeUIntBitIndex is already defined
  964. {$if sizeof(NativeUInt)=2}
  965. TNativeUIntNibbleIndex = TWordNibbleIndex;
  966. TNativeUIntByteIndex = TWordByteIndex;
  967. TNativeUIntOverlay = TWordOverlay;
  968. {$elseif sizeof(NativeUInt)=4}
  969. TNativeUIntNibbleIndex = TDwordNibbleIndex;
  970. TNativeUIntByteIndex = TDwordByteIndex;
  971. TNativeUIntWordIndex = TDwordWordIndex;
  972. TNativeUIntOverlay = TDwordOverlay;
  973. {$elseif sizeof(NativeUInt)=8}
  974. TNativeUIntNibbleIndex = TQwordNibbleIndex;
  975. TNativeUIntByteIndex = TQwordByteIndex;
  976. TNativeUIntWordIndex = TQwordWordIndex;
  977. TNativeUIntDwordIndex = TQwordDWordIndex;
  978. TNativeUIntOverlay = TQwordOverlay;
  979. {$else}
  980. {$fatal Unsupported NativeUInt type size}
  981. {$endif}
  982. TNativeUIntHelper = Type Helper for NativeUInt
  983. const
  984. MaxValue = High(NativeUInt);
  985. MinValue = 0;
  986. MaxBit = High(TNativeUIntBitIndex);
  987. MinBit = Low(TNativeUIntBitIndex);
  988. MaxNibble = High(TNativeUIntNibbleIndex);
  989. MinNibble = Low(TNativeUIntNibbleIndex);
  990. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need MaxByte and MinByte
  991. MaxByte = High(TNativeUIntByteIndex);
  992. MinByte = Low(TNativeUIntByteIndex);
  993. {$if sizeof(NativeUInt)>=4}
  994. MaxWord = High(TNativeUIntWordIndex);
  995. MinWord = Low(TNativeUIntWordIndex);
  996. {$endif}
  997. {$if sizeof(NativeUInt)>=8}
  998. MaxDword = High(TNativeUIntDwordIndex);
  999. MinDword = Low(TNativeUIntDwordIndex);
  1000. {$endif}
  1001. public
  1002. Class Function Parse(const AString: string): NativeUInt; inline; static;
  1003. Class Function Size: Integer; inline; static;
  1004. Class Function ToString(const AValue: NativeUInt): string; overload; inline; static;
  1005. Class Function TryParse(const AString: string; out AValue: NativeUInt): Boolean; inline; static;
  1006. protected
  1007. function GetBit(const aIndex: TNativeUIntBitIndex): boolean; inline;
  1008. procedure PutBit(const aIndex: TNativeUIntBitIndex; const aNewValue: boolean); inline;
  1009. function GetNibble(const aIndex: TNativeUIntNibbleIndex): nibble; inline;
  1010. procedure PutNibble(const aIndex: TNativeUIntNibbleIndex; const aNewValue: nibble); inline;
  1011. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need GetByte and PutByte
  1012. function GetByte(const aIndex: TNativeUIntByteIndex): byte;
  1013. procedure PutByte(const aIndex: TNativeUIntByteIndex; const aNewValue: byte);
  1014. {$if sizeof(NativeUInt)>=4}
  1015. function GetWord(const aIndex: TNativeUIntWordIndex): word;
  1016. procedure PutWord(const aIndex: TNativeUIntWordIndex; const aNewValue: word);
  1017. {$endif}
  1018. {$if sizeof(NativeUInt)>=8}
  1019. function GetDword(const aIndex: TNativeUIntDwordIndex): dword;
  1020. procedure PutDword(const aIndex: TNativeUIntDwordIndex; const aNewValue: dword);
  1021. {$endif}
  1022. public
  1023. Function ToBoolean: Boolean; inline;
  1024. Function ToDouble: Double; inline;
  1025. Function ToExtended: Extended; inline;
  1026. Function ToBinString:string; inline;
  1027. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  1028. Function ToHexString: string; overload; inline;
  1029. Function ToSingle: Single; inline;
  1030. Function ToString: string; overload; inline;
  1031. Function SetBit(const Index: TNativeUIntBitIndex) : NativeUint; inline;
  1032. Function ClearBit(const Index: TNativeUIntBitIndex): NativeUint; inline;
  1033. Function ToggleBit(const Index: TNativeUIntBitIndex) : NativeUint; inline;
  1034. Function TestBit(const Index:TNativeUIntBitIndex) :Boolean; inline;
  1035. procedure Clear; inline;
  1036. function HighestSetBitPos: cardinal; inline;
  1037. function LowestSetBitPos: cardinal; inline;
  1038. function SetBitsCount: byte; inline;
  1039. property Bits [aIndex: TNativeUIntBitIndex]: boolean read GetBit write PutBit;
  1040. property Nibbles [aIndex: TNativeUIntNibbleIndex]: nibble read GetNibble write PutNibble;
  1041. // NativeInt on cpu16 has 2 bytes, so no need for cpu16 check because we always need Byte[]
  1042. property Bytes [aIndex: TNativeUIntByteIndex]: byte read GetByte write PutByte;
  1043. {$if sizeof(NativeUInt)>=4}
  1044. property Words [aIndex: TNativeUIntWordIndex]: word read GetWord write PutWord;
  1045. {$endif}
  1046. {$if sizeof(NativeUInt)>=8}
  1047. property DWords [aIndex: TNativeUIntDwordIndex]: dword read GetDWord write PutDWord;
  1048. {$endif}
  1049. end;
  1050. {$SCOPEDENUMS ON}
  1051. TUseBoolStrs = (False, True);
  1052. {$SCOPEDENUMS OFF}
  1053. TBooleanHelper = Type Helper for Boolean
  1054. public
  1055. Class Function Parse(const S: string): Boolean; inline; static;
  1056. Class Function Size: Integer; inline; static;
  1057. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  1058. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  1059. Public
  1060. Function ToInteger: Integer; inline;
  1061. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  1062. end;
  1063. TByteBoolHelper = Type Helper for ByteBool
  1064. public
  1065. Class Function Parse(const S: string): Boolean; inline; static;
  1066. Class Function Size: Integer; inline; static;
  1067. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  1068. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  1069. Public
  1070. Function ToInteger: Integer; inline;
  1071. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  1072. end;
  1073. TWordBoolHelper = Type Helper for WordBool
  1074. public
  1075. Class Function Parse(const S: string): Boolean; inline; static;
  1076. Class Function Size: Integer; inline; static;
  1077. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  1078. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  1079. Public
  1080. Function ToInteger: Integer; inline;
  1081. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  1082. end;
  1083. TLongBoolHelper = Type Helper for LongBool
  1084. public
  1085. Class Function Parse(const S: string): Boolean; inline; static;
  1086. Class Function Size: Integer; inline; static;
  1087. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  1088. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  1089. public
  1090. Function ToInteger: Integer; inline;
  1091. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  1092. end;