syshelph.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. {%mainunit syshelpers.pp}
  2. Type
  3. generic TArray<T> = array of T;
  4. TStringArray = Array of string;
  5. TCharArray = Array of char;
  6. {$SCOPEDENUMS ON}
  7. TEndian = (Little,Big);
  8. {$SCOPEDENUMS OFF}
  9. Const
  10. CPUEndian = {$IFDEF FPC_LITTLE_ENDIAN}TEndian.Little{$ELSE}TEndian.Big{$ENDIF};
  11. Type
  12. TGuidHelper = record helper for TGUID
  13. Class function Create(const Data; BigEndian: Boolean): TGUID; overload; static;
  14. class function Create(const Data: array of Byte; AStartIndex: Cardinal; BigEndian: Boolean): TGUID; overload; static;
  15. Class Function Create(const Data; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  16. Class Function Create(const B: TBytes; DataEndian: TEndian = CPUEndian): TGUID; overload; static; inline;
  17. Class Function Create(const B: TBytes; AStartIndex: Cardinal; DataEndian: TEndian = CPUEndian): TGUID; overload; static;
  18. Class Function Create(const S: string): TGUID; overload; static;
  19. Class Function Create(A: Integer; B: SmallInt; C: SmallInt; const D: TBytes): TGUID; overload; static;
  20. Class Function Create(A: Integer; B: SmallInt; C: SmallInt; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  21. Class Function Create(A: Cardinal; B: Word; C: Word; D, E, F, G, H, I, J, K: Byte): TGUID; overload; static;
  22. Class Function NewGuid: TGUID; static;
  23. Function ToByteArray(DataEndian: TEndian = CPUEndian): TBytes;
  24. Function ToString(SkipBrackets: Boolean = False): string;
  25. end;
  26. TCompareOption = system.TCompareOption;
  27. TCompareOptions = system.TCompareOptions;
  28. {$SCOPEDENUMS ON}
  29. TStringSplitOptions = (None, ExcludeEmpty);
  30. {$SCOPEDENUMS OFF}
  31. { TStringHelper }
  32. TStringHelper = Type Helper for AnsiString
  33. Private
  34. Function GetChar(AIndex : Integer) : Char;
  35. Function GetLength : Integer;
  36. public
  37. const Empty = '';
  38. // Methods
  39. Class Function Compare(const A: string; const B: string): Integer; overload; static; //inline;
  40. Class Function Compare(const A: string; const B: string; IgnoreCase: Boolean): Integer; overload; static; //inline; //deprecated 'Use same with TCompareOptions';
  41. Class Function Compare(const A: string; const B: string; Options: TCompareOptions): Integer; overload; static; // inline;
  42. Class Function Compare(const A: string; IndexA: Integer; const B: string; IndexB: Integer; ALen: Integer): Integer; overload; static; // inline;
  43. Class Function Compare(const A: string; IndexA: Integer; const B: string; IndexB: Integer; ALen: Integer; IgnoreCase: Boolean): Integer; overload; static; // inline; //deprecated 'Use same with TCompareOptions';
  44. Class Function Compare(const A: string; IndexA: Integer; const B: string; IndexB: Integer; ALen: Integer; Options: TCompareOptions): Integer; overload; static;// inline;
  45. Class Function CompareOrdinal(const A: string; const B: string): Integer; overload; static;
  46. Class Function CompareOrdinal(const A: string; IndexA: Integer; const B: string; IndexB: Integer; ALen: Integer): Integer; overload; static;
  47. Class Function CompareText(const A: string; const B: string): Integer; static; inline;
  48. Class Function Copy(const Str: string): string; inline; static;
  49. Class Function Create(AChar: Char; ACount: Integer): string; overload; inline; static;
  50. Class Function Create(const AValue: array of Char): string; overload; static;
  51. Class Function Create(const AValue: array of Char; StartIndex: Integer; ALen: Integer): string; overload; static;
  52. Class Function EndsText(const ASubText, AText: string): Boolean; static;
  53. Class Function Equals(const a: string; const b: string): Boolean; overload; static;
  54. Class Function Format(const AFormat: string; const args: array of const): string; overload; static;
  55. Class Function IsNullOrEmpty(const AValue: string): Boolean; static;
  56. Class Function IsNullOrWhiteSpace(const AValue: string): Boolean; static;
  57. Class Function Join(const Separator: string; const Values: array of const): string; overload; static;
  58. Class Function Join(const Separator: string; const Values: array of string): string; overload; static;
  59. Class Function Join(const Separator: string; const Values: array of string; StartIndex: Integer; ACount: Integer): string; overload; static;
  60. Class Function LowerCase(const S: string): string; overload; static; inline;
  61. Class Function Parse(const AValue: Boolean): string; overload; static; inline;
  62. Class Function Parse(const AValue: Extended): string; overload; static;inline;
  63. Class Function Parse(const AValue: Int64): string; overload; static; inline;
  64. Class Function Parse(const AValue: Integer): string; overload; static; inline;
  65. Class Function ToBoolean(const S: string): Boolean; overload; static; inline;
  66. Class Function ToDouble(const S: string): Double; overload; static; inline;
  67. Class Function ToExtended(const S: string): Extended; overload; static; inline;
  68. Class Function ToInt64(const S: string): Int64; overload; static; inline;
  69. Class Function ToInteger(const S: string): Integer; overload; static; inline;
  70. Class Function ToSingle(const S: string): Single; overload; static; inline;
  71. Class Function UpperCase(const S: string): string; overload; static; inline;
  72. Function CompareTo(const B: string): Integer;
  73. Function Contains(const AValue: string): Boolean;
  74. procedure CopyTo(SourceIndex: Integer; var destination: array of Char; DestinationIndex: Integer; ACount: Integer);
  75. Function CountChar(const C: Char): Integer;
  76. Function DeQuotedString: string; overload;
  77. Function DeQuotedString(const AQuoteChar: Char): string; overload;
  78. Function EndsWith(const AValue: string): Boolean; overload; inline;
  79. Function EndsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
  80. Function Equals(const AValue: string): Boolean; overload;
  81. Function Format(const args: array of const): string; overload;
  82. Function GetHashCode: Integer;
  83. Function IndexOf(AValue: Char): Integer; overload; inline;
  84. Function IndexOf(const AValue: string): Integer; overload; inline;
  85. Function IndexOf(AValue: Char; StartIndex: Integer): Integer; overload;
  86. Function IndexOf(const AValue: string; StartIndex: Integer): Integer; overload;
  87. Function IndexOf(AValue: Char; StartIndex: Integer; ACount: Integer): Integer; overload;
  88. Function IndexOf(const AValue: string; StartIndex: Integer; ACount: Integer): Integer; overload;
  89. Function IndexOfUnQuoted(const AValue: string; StartQuote, EndQuote: Char; StartIndex: Integer = 0): Integer; overload;
  90. Function IndexOfAny(const AnyOf: array of Char): Integer; overload;
  91. Function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer): Integer; overload;
  92. Function IndexOfAny(const AnyOf: array of Char; StartIndex: Integer; ACount: Integer): Integer; overload;
  93. Function IndexOfAny(const AnyOf: array of String): Integer; overload;
  94. Function IndexOfAny(const AnyOf: array of String; StartIndex: Integer): Integer; overload;
  95. Function IndexOfAny(const AnyOf: array of String; StartIndex: Integer; ACount: Integer): Integer; overload;
  96. Function IndexOfAny(const AnyOf: array of String; StartIndex: Integer; ACount: Integer; Out AMatch : Integer): Integer; overload;
  97. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char): Integer; overload;
  98. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char; StartIndex: Integer): Integer; overload;
  99. Function IndexOfAnyUnquoted(const AnyOf: array of Char; StartQuote, EndQuote: Char; StartIndex: Integer; ACount: Integer): Integer; overload;
  100. function IndexOfAnyUnquoted(const AnyOf: array of string; StartQuote, EndQuote: Char; StartIndex: Integer; Out Matched: Integer): Integer; overload;
  101. Function Insert(StartIndex: Integer; const AValue: string): string;
  102. Function IsDelimiter(const Delimiters: string; Index: Integer): Boolean;
  103. Function IsEmpty: Boolean;
  104. Function LastDelimiter(const Delims: string): Integer;
  105. Function LastIndexOf(AValue: Char): Integer; overload;
  106. Function LastIndexOf(const AValue: string): Integer; overload;
  107. Function LastIndexOf(AValue: Char; AStartIndex: Integer): Integer; overload;
  108. Function LastIndexOf(const AValue: string; AStartIndex: Integer): Integer; overload;
  109. Function LastIndexOf(AValue: Char; AStartIndex: Integer; ACount: Integer): Integer; overload;
  110. Function LastIndexOf(const AValue: string; AStartIndex: Integer; ACount: Integer): Integer; overload;
  111. Function LastIndexOfAny(const AnyOf: array of Char): Integer; overload;
  112. Function LastIndexOfAny(const AnyOf: array of Char; AStartIndex: Integer): Integer; overload;
  113. Function LastIndexOfAny(const AnyOf: array of Char; AStartIndex: Integer; ACount: Integer): Integer; overload;
  114. Function PadLeft(ATotalWidth: Integer): string; overload; inline;
  115. Function PadLeft(ATotalWidth: Integer; PaddingChar: Char): string; overload; inline;
  116. Function PadRight(ATotalWidth: Integer): string; overload; inline;
  117. Function PadRight(ATotalWidth: Integer; PaddingChar: Char): string; overload; inline;
  118. Function QuotedString: string; overload;
  119. Function QuotedString(const AQuoteChar: Char): string; overload;
  120. Function Remove(StartIndex: Integer): string; overload; inline;
  121. Function Remove(StartIndex: Integer; ACount: Integer): string; overload; inline;
  122. Function Replace(OldChar: Char; NewChar: Char): string; overload;
  123. Function Replace(OldChar: Char; NewChar: Char; ReplaceFlags: TReplaceFlags): string; overload;
  124. Function Replace(const OldValue: string; const NewValue: string): string; overload;
  125. Function Replace(const OldValue: string; const NewValue: string; ReplaceFlags: TReplaceFlags): string; overload;
  126. Function Split(const Separators: array of Char): TStringArray; overload;
  127. Function Split(const Separators: array of Char; ACount: Integer): TStringArray; overload;
  128. Function Split(const Separators: array of Char; Options: TStringSplitOptions): TStringArray; overload;
  129. Function Split(const Separators: array of Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
  130. Function Split(const Separators: array of string): TStringArray; overload;
  131. Function Split(const Separators: array of string; ACount: Integer): TStringArray; overload;
  132. Function Split(const Separators: array of string; Options: TStringSplitOptions): TStringArray; overload;
  133. Function Split(const Separators: array of string; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
  134. Function Split(const Separators: array of Char; AQuote: Char): TStringArray; overload;
  135. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
  136. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
  137. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: Integer): TStringArray; overload;
  138. Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
  139. Function Split(const Separators: array of string; AQuote: Char): TStringArray; overload;
  140. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
  141. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
  142. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: Integer): TStringArray; overload;
  143. Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
  144. Function StartsWith(const AValue: string): Boolean; overload; inline;
  145. Function StartsWith(const AValue: string; IgnoreCase: Boolean): Boolean; overload;
  146. Function Substring(AStartIndex: Integer): string; overload;
  147. Function Substring(AStartIndex: Integer; ALen: Integer): string; overload;
  148. Function ToBoolean: Boolean; overload; inline;
  149. Function ToInteger: Integer; overload; inline;
  150. Function ToInt64: Int64; overload; inline;
  151. Function ToSingle: Single; overload; inline;
  152. Function ToDouble: Double; overload; inline;
  153. Function ToExtended: Extended; overload; inline;
  154. Function ToCharArray: TCharArray; overload;
  155. Function ToCharArray(AStartIndex: Integer; ALen: Integer): TCharArray; overload;
  156. Function ToLower: string; overload; inline;
  157. Function ToLowerInvariant: string;
  158. Function ToUpper: string; overload; inline;
  159. Function ToUpperInvariant: string; inline;
  160. Function Trim: string; overload;
  161. Function TrimLeft: string; overload;
  162. Function TrimRight: string; overload;
  163. Function Trim(const ATrimChars: array of Char): string; overload;
  164. Function TrimLeft(const ATrimChars: array of Char): string; overload;
  165. Function TrimRight(const ATrimChars: array of Char): string; overload;
  166. Function TrimEnd(const ATrimChars: array of Char): string; deprecated 'Use TrimRight';
  167. Function TrimStart(const ATrimChars: array of Char): string; deprecated 'Use TrimLeft';
  168. property Chars[AIndex: Integer]: Char read GetChar;
  169. property Length: Integer read GetLength;
  170. end;
  171. {$IFDEF FPC_HAS_TYPE_SINGLE}
  172. TSingleHelper = Type Helper for Single
  173. Private
  174. Function GetB(AIndex: Cardinal): Byte;
  175. Function GetW(AIndex: Cardinal): Word;
  176. Function GetE: QWord; inline;
  177. Function GetF: QWord; inline;
  178. Function GetS: Boolean; inline;
  179. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  180. procedure SetW(AIndex: Cardinal; const AValue: Word);
  181. procedure SetE(AValue: QWord);
  182. procedure SetF(AValue: QWord);
  183. procedure SetS(AValue: Boolean);
  184. public
  185. {$push}
  186. {$R-}
  187. {$Q-}
  188. const
  189. Epsilon : Single = 1.4012984643248170709e-45;
  190. MaxValue : Single = 340282346638528859811704183484516925440.0;
  191. MinValue : Single = -340282346638528859811704183484516925440.0;
  192. PositiveInfinity : Single = 1.0/0.0;
  193. NegativeInfinity : Single = -1.0/0.0;
  194. NaN : Single = 0.0/0.0;
  195. {$POP}
  196. Class Function IsNan(const AValue: Single): Boolean; overload; inline; static;
  197. Class Function IsInfinity(const AValue: Single): Boolean; overload; inline; static;
  198. Class Function IsNegativeInfinity(const AValue: Single): Boolean; overload; inline; static;
  199. Class Function IsPositiveInfinity(const AValue: Single): Boolean; overload; inline; static;
  200. Class Function Parse(const AString: string): Single; overload; inline; static;
  201. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Single; overload; inline; static;
  202. Class Function Size: Integer; inline; static;
  203. Class Function ToString(const AValue: Single): string; overload; inline; static;
  204. Class Function ToString(const AValue: Single; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  205. Class Function ToString(const AValue: Single; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  206. Class Function ToString(const AValue: Single; const AFormat: TFloatFormat; const APrecision, ADigits: Integer;
  207. const AFormatSettings: TFormatSettings): string; overload; inline; static;
  208. Class Function TryParse(const AString: string; out AValue: Single): Boolean; overload; inline; static;
  209. Class Function TryParse(const AString: string; out AValue: Single; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  210. Procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; const AExponent: Integer);
  211. Function Exponent: Integer;
  212. Function Fraction: Extended;
  213. Function IsInfinity: Boolean; overload; inline;
  214. Function IsNan: Boolean; overload; inline;
  215. Function IsNegativeInfinity: Boolean; overload; inline;
  216. Function IsPositiveInfinity: Boolean; overload; inline;
  217. Function Mantissa: QWord;
  218. Function SpecialType: TFloatSpecial;
  219. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  220. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  221. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  222. Function ToString: string; overload; inline;
  223. property Bytes[AIndex: Cardinal]: Byte read GetB write SetB;
  224. property Words[AIndex: Cardinal]: Word read GetW write SetW;
  225. property Sign: Boolean read GetS write SetS;
  226. property Exp: QWord read GetE write SetE;
  227. property Frac: QWord read GetF write SetF;
  228. end;
  229. {$ENDIF FPC_HAS_TYPE_SINGLE}
  230. {$IFDEF FPC_HAS_TYPE_DOUBLE}
  231. TDoubleHelper = Type Helper for Double
  232. private
  233. Function GetB(AIndex: Cardinal): Byte;
  234. Function GetW(AIndex: Cardinal): Word;
  235. Function GetE: QWord; inline;
  236. Function GetF: QWord; inline;
  237. Function GetS: Boolean; inline;
  238. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  239. procedure SetW(AIndex: Cardinal; const AValue: Word);
  240. procedure SetE(AValue: QWord);
  241. procedure SetF(AValue: QWord);
  242. procedure SetS(AValue: Boolean);
  243. public
  244. const
  245. {$push}
  246. {$R-}
  247. {$Q-}
  248. Epsilon : Double = 4.9406564584124654418e-324;
  249. MaxValue : Double = 1.7976931348623157081e+308;
  250. MinValue : Double = -1.7976931348623157081e+308;
  251. PositiveInfinity : Double = 1.0/0.0;
  252. NegativeInfinity : Double = -1.0/0.0;
  253. NaN : Double = 0.0/0.0;
  254. {$POP}
  255. Class Function IsInfinity(const AValue: Double): Boolean; overload; inline; static;
  256. Class Function IsNan(const AValue: Double): Boolean; overload; inline; static;
  257. Class Function IsNegativeInfinity(const AValue: Double): Boolean; overload; inline; static;
  258. Class Function IsPositiveInfinity(const AValue: Double): Boolean; overload; inline; static;
  259. Class Function Parse(const AString: string): Double; overload; inline; static;
  260. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Double; overload; inline; static;
  261. Class Function Size: Integer; inline; static;
  262. Class Function ToString(const AValue: Double): string; overload; inline; static;
  263. Class Function ToString(const AValue: Double; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  264. Class Function ToString(const AValue: Double; const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  265. Class Function ToString(const AValue: Double; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  266. Class Function TryParse(const AString: string; out AValue: Double): Boolean; overload; inline; static;
  267. Class Function TryParse(const AString: string; out AValue: Double; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  268. Procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; const AExponent: Integer);
  269. Function Exponent: Integer;
  270. Function Fraction: Extended;
  271. Function IsInfinity: Boolean; overload; inline;
  272. Function IsNan: Boolean; overload; inline;
  273. Function IsNegativeInfinity: Boolean; overload; inline;
  274. Function IsPositiveInfinity: Boolean; overload; inline;
  275. Function Mantissa: QWord;
  276. Function SpecialType: TFloatSpecial;
  277. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  278. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  279. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  280. Function ToString: string; overload; inline;
  281. property Bytes[AIndex: Cardinal]: Byte read GetB write SetB; // 0..7
  282. property Words[AIndex: Cardinal]: Word read GetW write SetW; // 0..3
  283. property Sign: Boolean read GetS write SetS;
  284. property Exp: QWord read GetE write SetE;
  285. property Frac: QWord read GetF write SetF;
  286. end;
  287. {$ENDIF FPC_HAS_TYPE_DOUBLE}
  288. {$ifdef FPC_HAS_TYPE_EXTENDED}
  289. TExtendedHelper = Type Helper for Extended
  290. private
  291. Function GetB(AIndex: Cardinal): Byte;
  292. Function GetW(AIndex: Cardinal): Word;
  293. Function GetE: QWord; inline;
  294. Function GetF: QWord; inline;
  295. Function GetS: Boolean; inline;
  296. procedure SetB(AIndex: Cardinal; const AValue: Byte);
  297. procedure SetW(AIndex: Cardinal; const AValue: Word);
  298. procedure SetE(AValue: QWord);
  299. procedure SetF(AValue: QWord);
  300. procedure SetS(AValue: Boolean);
  301. public
  302. {$push}
  303. {$R-}
  304. {$Q-}
  305. const
  306. Epsilon : Extended = 3.64519953188247460253e-4951;
  307. MaxValue : Extended = 1.18973149535723176505e+4932;
  308. MinValue : Extended = -1.18973149535723176505e+4932;
  309. PositiveInfinity : Extended = 1.0/0.0;
  310. NegativeInfinity : Extended = -1.0/0.0;
  311. NaN : Extended = 0.0/0.0;
  312. {$POP}
  313. Class Function ToString(const AValue: Extended): string; overload; inline; static;
  314. Class Function ToString(const AValue: Extended; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  315. Class Function ToString(const AValue: Extended; const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline; static;
  316. Class Function ToString(const AValue: Extended; const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline; static;
  317. Class Function Parse(const AString: string): Extended; overload; inline; static;
  318. Class Function Parse(const AString: string; const AFormatSettings: TFormatSettings): Extended; overload; inline; static;
  319. Class Function TryParse(const AString: string; out AValue: Extended): Boolean; overload; inline; static;
  320. Class Function TryParse(const AString: string; out AValue: Extended; const AFormatSettings: TFormatSettings): Boolean; overload; inline; static;
  321. Class Function IsNan(const AValue: Extended): Boolean; overload; inline; static;
  322. Class Function IsInfinity(const AValue: Extended): Boolean; overload; inline; static;
  323. Class Function IsNegativeInfinity(const AValue: Extended): Boolean; overload; inline; static;
  324. Class Function IsPositiveInfinity(const AValue: Extended): Boolean; overload; inline; static;
  325. Class Function Size: Integer; inline; static;
  326. procedure BuildUp(const ASignFlag: Boolean; const AMantissa: QWord; Const AExponent: Integer);
  327. Function Exponent: Integer;
  328. Function Fraction: Extended;
  329. Function IsInfinity: Boolean; overload; inline;
  330. Function IsNan: Boolean; overload; inline;
  331. Function IsNegativeInfinity: Boolean; overload; inline;
  332. Function IsPositiveInfinity: Boolean; overload; inline;
  333. Function Mantissa: QWord;
  334. Function SpecialType: TFloatSpecial;
  335. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer): string; overload; inline;
  336. Function ToString(const AFormat: TFloatFormat; const APrecision, ADigits: Integer; const AFormatSettings: TFormatSettings): string; overload; inline;
  337. Function ToString(const AFormatSettings: TFormatSettings): string; overload; inline;
  338. Function ToString: string; overload; inline;
  339. property Bytes[AIndex: Cardinal]: Byte read GetB write SetB;
  340. property Words[AIndex: Cardinal]: Word read GetW write SetW;
  341. property Sign: Boolean read GetS write SetS;
  342. property Exp: QWord read GetE write SetE;
  343. property Frac: QWord read GetF write SetF;
  344. end;
  345. {$ENDIF FPC_HAS_TYPE_EXTENDED}
  346. TByteHelper = Type Helper for Byte
  347. public
  348. const
  349. MaxValue = 255;
  350. MinValue = 0;
  351. public
  352. Class Function Parse(const AString: string): Byte; inline; static;
  353. Class Function Size: Integer; inline; static;
  354. Class Function ToString(const AValue: Byte): string; overload; inline; static;
  355. Class Function TryParse(const AString: string; out AValue: Byte): Boolean; inline; static;
  356. Public
  357. Function ToBoolean: Boolean; inline;
  358. Function ToDouble: Double; inline;
  359. Function ToExtended: Extended; inline;
  360. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  361. Function ToHexString: string; overload; inline;
  362. Function ToSingle: Single; inline;
  363. Function ToString: string; overload; inline;
  364. end;
  365. TShortIntHelper = Type Helper for ShortInt
  366. public
  367. const
  368. MaxValue = 127;
  369. MinValue = -128;
  370. public
  371. Class Function Parse(const AString: string): ShortInt; inline; static;
  372. Class Function Size: Integer; inline; static;
  373. Class Function ToString(const AValue: ShortInt): string; overload; inline; static;
  374. Class Function TryParse(const AString: string; out AValue: ShortInt): Boolean; inline; static;
  375. public
  376. Function ToBoolean: Boolean; inline;
  377. Function ToDouble: Double; inline;
  378. Function ToExtended: Extended; inline;
  379. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  380. Function ToHexString: string; overload; inline;
  381. Function ToSingle: Single; inline;
  382. Function ToString: string; overload; inline;
  383. end;
  384. TSmallIntHelper = Type Helper for SmallInt
  385. public
  386. const
  387. MaxValue = 32767;
  388. MinValue = -32768;
  389. public
  390. Class Function Parse(const AString: string): SmallInt; inline; static;
  391. Class Function Size: Integer; inline; static;
  392. Class Function ToString(const AValue: SmallInt): string; overload; inline; static;
  393. Class Function TryParse(const AString: string; out AValue: SmallInt): Boolean; inline; static;
  394. public
  395. Function ToString: string; overload; inline;
  396. Function ToBoolean: Boolean; inline;
  397. Function ToHexString: string; overload; inline;
  398. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  399. Function ToSingle: Single; inline;
  400. Function ToDouble: Double; inline;
  401. Function ToExtended: Extended; inline;
  402. end;
  403. TWordHelper = Type Helper for Word
  404. public
  405. const
  406. MaxValue = 65535;
  407. MinValue = 0;
  408. Public
  409. Class Function Parse(const AString: string): Word; inline; static;
  410. Class Function Size: Integer; inline; static;
  411. Class Function ToString(const AValue: Word): string; overload; inline; static;
  412. Class Function TryParse(const AString: string; out AValue: Word): Boolean; inline; static;
  413. Public
  414. Function ToBoolean: Boolean; inline;
  415. Function ToDouble: Double; inline;
  416. Function ToExtended: Extended; inline;
  417. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  418. Function ToHexString: string; overload; inline;
  419. Function ToSingle: Single; inline;
  420. Function ToString: string; overload; inline;
  421. end;
  422. TCardinalHelper = Type Helper for Cardinal { for LongWord Type too }
  423. public
  424. const
  425. MaxValue = 4294967295;
  426. MinValue = 0;
  427. Public
  428. Class Function Parse(const AString: string): Cardinal; inline; static;
  429. Class Function Size: Integer; inline; static;
  430. Class Function ToString(const AValue: Cardinal): string; overload; inline; static;
  431. Class Function TryParse(const AString: string; out AValue: Cardinal): Boolean; inline; static;
  432. Public
  433. Function ToBoolean: Boolean; inline;
  434. Function ToDouble: Double; inline;
  435. Function ToExtended: Extended; inline;
  436. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  437. Function ToHexString: string; overload; inline;
  438. Function ToSingle: Single; inline;
  439. Function ToString: string; overload; inline;
  440. end;
  441. TIntegerHelper = Type Helper for Integer { for LongInt Type too }
  442. public
  443. const
  444. MaxValue = 2147483647;
  445. MinValue = -2147483648;
  446. Public
  447. Class Function Size: Integer; inline; static;
  448. Class Function ToString(const AValue: Integer): string; overload; inline; static;
  449. Class Function Parse(const AString: string): Integer; inline; static;
  450. Class Function TryParse(const AString: string; out AValue: Integer): Boolean; inline; static;
  451. Public
  452. Function ToBoolean: Boolean; inline;
  453. Function ToDouble: Double; inline;
  454. Function ToExtended: Extended; inline;
  455. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  456. Function ToHexString: string; overload; inline;
  457. Function ToSingle: Single; inline;
  458. Function ToString: string; overload; inline;
  459. end;
  460. TInt64Helper = Type Helper for Int64
  461. public
  462. const
  463. MaxValue = 9223372036854775807;
  464. MinValue = -9223372036854775808;
  465. Public
  466. Class Function Parse(const AString: string): Int64; inline; static;
  467. Class Function Size: Integer; inline; static;
  468. Class Function ToString(const AValue: Int64): string; overload; inline; static;
  469. Class Function TryParse(const AString: string; out AValue: Int64): Boolean; inline; static;
  470. Public
  471. Function ToBoolean: Boolean; inline;
  472. Function ToDouble: Double; inline;
  473. Function ToExtended: Extended; inline;
  474. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  475. Function ToHexString: string; overload; inline;
  476. Function ToSingle: Single; inline;
  477. Function ToString: string; overload; inline;
  478. end;
  479. TQWordHelper = Type Helper for QWord
  480. public
  481. const
  482. MaxValue = 18446744073709551615;
  483. MinValue = 0;
  484. Public
  485. Class Function Parse(const AString: string): QWord; inline; static;
  486. Class Function Size: Integer; inline; static;
  487. Class Function ToString(const AValue: QWord): string; overload; inline; static;
  488. Class Function TryParse(const AString: string; out AValue: QWord): Boolean; inline; static;
  489. Public
  490. Function ToBoolean: Boolean; inline;
  491. Function ToDouble: Double; inline;
  492. Function ToExtended: Extended; inline;
  493. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  494. Function ToHexString: string; overload; inline;
  495. Function ToSingle: Single; inline;
  496. Function ToString: string; overload; inline;
  497. end;
  498. TNativeIntHelper = Type Helper for NativeInt
  499. public
  500. const
  501. {$IFDEF CPU64BITS}
  502. MaxValue = 9223372036854775807;
  503. MinValue = -9223372036854775808;
  504. {$ELSE !CPU64BITS}
  505. MaxValue = 2147483647;
  506. MinValue = -2147483648;
  507. {$ENDIF CPU64BITS}
  508. Public
  509. Class Function Parse(const AString: string): NativeInt; inline; static;
  510. Class Function Size: Integer; inline; static;
  511. Class Function ToString(const AValue: NativeInt): string; overload; inline; static;
  512. Class Function TryParse(const AString: string; out AValue: NativeInt): Boolean; inline; static;
  513. Public
  514. Function ToBoolean: Boolean; inline;
  515. Function ToDouble: Double; inline;
  516. Function ToExtended: Extended; inline;
  517. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  518. Function ToHexString: string; overload; inline;
  519. Function ToSingle: Single; inline;
  520. Function ToString: string; overload; inline;
  521. end;
  522. TNativeUIntHelper = Type Helper for NativeUInt
  523. public
  524. const
  525. {$IFDEF CPU64BITS}
  526. MaxValue = 18446744073709551615;
  527. {$ELSE !CPU64BITS}
  528. MaxValue = 4294967295;
  529. {$ENDIF CPU64BITS}
  530. MinValue = 0;
  531. Public
  532. Class Function Parse(const AString: string): NativeUInt; inline; static;
  533. Class Function Size: Integer; inline; static;
  534. Class Function ToString(const AValue: NativeUInt): string; overload; inline; static;
  535. Class Function TryParse(const AString: string; out AValue: NativeUInt): Boolean; inline; static;
  536. Public
  537. Function ToBoolean: Boolean; inline;
  538. Function ToDouble: Double; inline;
  539. Function ToExtended: Extended; inline;
  540. Function ToHexString(const AMinDigits: Integer): string; overload; inline;
  541. Function ToHexString: string; overload; inline;
  542. Function ToSingle: Single; inline;
  543. Function ToString: string; overload; inline;
  544. end;
  545. {$SCOPEDENUMS ON}
  546. TUseBoolStrs = (False, True);
  547. {$SCOPEDENUMS OFF}
  548. TBooleanHelper = Type Helper for Boolean
  549. public
  550. Class Function Parse(const S: string): Boolean; inline; static;
  551. Class Function Size: Integer; inline; static;
  552. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  553. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  554. Public
  555. Function ToInteger: Integer; inline;
  556. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  557. end;
  558. TByteBoolHelper = Type Helper for ByteBool
  559. public
  560. Class Function Parse(const S: string): Boolean; inline; static;
  561. Class Function Size: Integer; inline; static;
  562. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  563. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  564. Public
  565. Function ToInteger: Integer; inline;
  566. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  567. end;
  568. TWordBoolHelper = Type Helper for WordBool
  569. public
  570. Class Function Parse(const S: string): Boolean; inline; static;
  571. Class Function Size: Integer; inline; static;
  572. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  573. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  574. Public
  575. Function ToInteger: Integer; inline;
  576. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  577. end;
  578. TLongBoolHelper = Type Helper for LongBool
  579. public
  580. Class Function Parse(const S: string): Boolean; inline; static;
  581. Class Function Size: Integer; inline; static;
  582. Class Function ToString(const AValue: Boolean; UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline; static;
  583. Class Function TryToParse(const S: string; out AValue: Boolean): Boolean; inline; static;
  584. public
  585. Function ToInteger: Integer; inline;
  586. Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  587. end;