[fpc-pascal] Record helpers on built in types?
Anthony Walter
sysrpl at gmail.com
Wed Feb 18 04:13:59 CET 2015
If anyone was interested in the subject, here is what I came up with after
merging my string centric functions into record helpers:
type
StringArrayHelper = record helper for StringArray
{ Join a string array into a string using a separator }
function Join(const Separator: string): string;
end
{ IntArrayHelper }
IntArrayHelper = record helper for StringArray
{ Join an int array into a string using a separator }
function Join(const Separator: string): string;
end
{ StringHelper }
StringHelper = record helper for string
private
function GetIsEmpty: Boolean;
function GetIsWhitespace: Boolean;
function GetIsIdentifier: Boolean;
function GetIsAttribute: Boolean;
function GetLength: Integer;
procedure SetLength(Value: Integer);
public
{ Repeat a character a given length a into string }
procedure CharInto(C: Char; Len: Integer);
{ Copy a memory buffer into string }
procedure CopyInto(P: Pointer; Len: Integer);
{ Inserts a substring at a position into string }
procedure InsertInto(const SubStr: string; Position: Integer);
{ Returns true if a string matches a case insensitive value }
function Equals(const Value: string): Boolean; overload;
{ Returns true if a string matches any in a set of case insensitive
values }
function Equals(const Values: array of string): Boolean; overload;
{ Compares two strings optionally ignoring case returning -1 if string
comes before
before value, 1 if string comes after value, ord 0 if string and
value are equal }
function Compare(const Value: string; IgnoreCase: Boolean = False):
Integer;
{ Convert a string to uppercase }
function ToUpper: string;
{ Convert a string to lowercase }
function ToLower: string;
{ Copies a substring given a start and length }
function Copy(Start: Integer; Len: Integer = 0): string;
{ Searches a string for a substring optionally ignoring case }
function IndexOf(const SubStr: string; IgnoreCase: Boolean = False):
Integer; overload;
{ Searches a string for a substring from a start position optionally
ignoring case }
function IndexOf(const SubStr: string; Start: Integer; IgnoreCase:
Boolean = False): Integer; overload;
{ Returns the number of a substring matches within a string }
function MatchCount(const SubStr: string; IgnoreCase: Boolean = False):
Integer;
{ Returns an array of indices of a substring matches within a string }
function MatchIndices(const SubStr: string; IgnoreCase: Boolean =
False): IntArray;
{ Replaces every instance of a pattern in a string }
function Replace(const OldPattern, NewPattern: string; IgnoreCase:
Boolean = False): string;
{ Replaces the first instance of a pattern in a string }
function ReplaceOne(const OldPattern, NewPattern: string; IgnoreCase:
Boolean = False): string;
{ Replaces everything aftger the first instance of a pattern in a
string }
function ReplaceAfter(const OldPattern, NewPattern: string; IgnoreCase:
Boolean = False): string;
{ Trims white space from both sides of a string }
function Trim: string;
{ Returns the index of a string in a string array or -1 if there is no
match }
function ArrayIndex(const Values: array of string): Integer;
{ Splits a string into a string array using a separator }
function Split(Separator: string): StringArray;
{ Splits a string into a int array using a separator }
function SplitInt(const Separator: string): IntArray;
{ Splits a string into a int64 array using a separator }
function SplitInt64(const S, Separator: string): Int64Array;
{ Returns the first subsection of a string if it were split using a
separator }
function FirstOf(Separator: string): string;
{ Returns the second subsection of a string if it were split using a
separator }
function SecondOf(Separator: string): string;
{ Returns the last subsection of a string if it were split using a
separator }
function LastOf(Separator: string): string;
{ Search a string for a substring optionally ignoring case }
function Contains(SubStr: string; IgnoreCase: Boolean = False): Boolean;
{ Returns true if a string begins with a substring while optionally
ignoring case }
function BeginsWith(SubStr: string; IgnoreCase: Boolean = False):
Boolean;
{ Returns true if a string end with a substring while optionally
ignoring case }
function EndsWith(SubStr: string; IgnoreCase: Boolean = False): Boolean;
{ Returns a string made to fit a given length padded on the left with a
character }
function PadLeft(C: Char; Len: Integer): string;
{ Returns a string made to fit a given length padded on the right with
a character }
function PadRight(C: Char; Len: Integer): string;
{ Returns a string surrounded by quotes if it contains whitespace }
function Quote: string;
{ Analyze a string and find its line break style }
function LineBreakStyle: TTextLineBreakStyle;
{ Converts the line break style of a string to a the desired style }
function AdjustLineBreaks(Style: TTextLineBreakStyle): string; overload;
{ Converts the line break style of a string to the system preferred
defined style }
function AdjustLineBreaks: string; overload;
{ Gets true if a string contains only whitespace characters }
property IsEmpty: Boolean read GetIsEmpty;
{ Gets true if a string contains only whitespace characters }
property IsWhitespace: Boolean read GetIsWhitespace;
{ Gets true if a string matches to rules of an identifier }
property IsIdentifier: Boolean read GetIsIdentifier;
{ Gets true if a string matches to rules of an attribute }
property IsAttribute: Boolean read GetIsAttribute;
{ Gets or sets the length allocated for the string }
property Length: Boolean read GetLength write SetLength;
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20150217/3769f2c3/attachment.html>
More information about the fpc-pascal
mailing list