Various string handling rountines, coded in speed optimized Pascal.
Pascal code is not by default slower than assembler code. The functions in this unit are just as fast than or even faster as those coded in assembler. However, due to the intricacies of assembler, they are easier to read and more readily accessible to most Delphi programmers than their assembler counterparts.
None.
function AnsiAdjustHyphens(const s: AnsiString): AnsiString; |
Adjusts hyphens.
function AnsiBufferSameCI(const Buffer: Pointer; const BufferLength: Cardinal; const s2: AnsiString): Boolean; |
function AnsiBufferSameCS(const Buffer: Pointer; const BufferLength: Cardinal; const s2: AnsiString): Boolean; |
function AnsiBufferScanSetCS(const Buffer: PAnsiChar; const BufferLength: Cardinal; const Search: TAnsiCharSet; const Start: Cardinal = 0): Integer; |
Scans the Buffer for the first Character in set Search. The comparison is not based on the current locale and is case-sensitive.
function AnsiCompareCI(const s1, s2: AnsiString): Integer; |
Compares two strings by ordinal value without case sensitivity.
AnsiCompareCI
compares S1 and S2 and returns 0 if they are equal. If S1 is greater than S2, AnsiCompareCI
returns an integer greater than 0. If S1 is less than S2, AnsiCompareCI
returns an integer less than 0. AnsiCompareCI
is not case sensitive and is not affected by the current Windows locale.
Thus for example, AnsiCompareCI
will indicate that the string 'ABC' is less than 'aaa' because 'A' is less than 'a' in ANSI order. This is in contrast to a case-insensitive comparison, where the 'B' would make the first string larger, or a locale-based comparison (most locales consider capital letters larger than small).
See also: AnsiCompareCS
.
function AnsiCompareCS(const s1, s2: AnsiString): Integer; |
Compares two strings case sensitively.
CompareStr compares S1 to S2, with case-sensitivity. The return value is less than 0 if S1 is less than S2, 0 if S1 equals S2, or greater than 0 if S1 is greater than S2. The compare operation is based on the 8-bit ordinal value of each character and is not affected by the current Windows locale.
See also: AnsiCompareCI
.
procedure AnsiCompressWhiteSpaceInPlace(var s: AnsiString); |
Removes all trailing or leading whitespace and control characters, or both, from a string. Also compresses all occurances of multiple whitespace or control characters within the string to one single space character (#32).
procedure AnsiExcludeTrailingSlashInPlace(var s: AnsiString); |
Trims all white space from the end of s and deletes a trailing PATH_SEPARATOR if present.
See also: AnsiIncludeTrailingSlashInPlace
.
function AnsiExistsCharCS(const s: AnsiString; const c: AnsiChar; const Start: Cardinal = 1): Boolean; |
Returns True
if character c exists in string s.
function AnsiExtractWord(const Number: Cardinal; const s: AnsiString; const Delimiters: TAnsiCharSet = ANSI_WHITE_SPACE): AnsiString; |
Extracts the Number-th word from a string. Words are delimited by Delimiters
, where each delimiter starts a new word. If two or more delimiters directly follow each other, empty strings will be returned.
function AnsiFirstCharsFromWords(const s: AnsiString; const MaxCharCount: Cardinal; const WordSeparators: TAnsiCharSet = ANSI_WHITE_SPACE): AnsiString; |
Extracts the first characters of each word in s, up to MaxCharCount and concatenates them to the result string.
Example: AnsiFirstCharsFromWords
('This is a test',2)
will result in 'Thisate'
procedure AnsiIncludeTrailingSlashInPlace(var s: AnsiString); |
Trims all white space from the end of s and adds a trailing PATH_SEPARATOR if one is not already present.
See also: AnsiExcludeTrailingSlashInPlace
.
function AnsiIsEmptyString(const s: AnsiString): Boolean; |
Returns True
is a given string is empty or contains white space or control characters only.
function AnsiLowerCase(const s: AnsiString): AnsiString; |
Returns a string that is a copy of the given string converted to lower case. The conversion does not use the current locale.
function AnsiMatchCI(const Search, Source: AnsiString; const Start: Cardinal = 1): Boolean; |
Matches Search against Source starting at position Start. Comparison is not based on the current locale and is case-insensitive.
function AnsiMatchCS(const Search, Source: AnsiString; const Start: Cardinal = 1): Boolean; |
Matches Search against Source starting at position Start. Comparison is not based on the current locale and is case-sensitive.
function AnsiMatchWildCS(const Source, Mask: AnsiString; const WildChar: AnsiChar = '*'; const MaskChar: AnsiChar = '?'): Boolean; |
Indicates whether Source string conforms to the format specified Mask string.
Each literal character must match a single character in the string. The comparison to literal characters is case-sensitive.
Wildcards are WildChar (*) and MaskChar (?). A WildChar matches any number of characters. A MaskChar matches a single arbitrary character.
AnsiMatchWildCS
returns True
if the string matches the mask and False
if it doesn't.
function AnsiPadLeft(const Source: AnsiString; const Count: Cardinal; const c: AnsiChar = #32): AnsiString; |
Appends characters (c) to left of Source as required to increase length to Count.
function AnsiPadRight(const Source: AnsiString; const Count: Cardinal; const c: AnsiChar = #32): AnsiString; |
Appends characters (c) to right of Source as required to increase length to Count.
function AnsiPosBackCI(const Search, Source: AnsiString; Start: Cardinal = 0): Cardinal; |
AnsiPosBackCI
searches for a substring, Search, in a string, Source, and returns a cardinal value that is the index of the first character of Search within Source. AnsiPosBackCI
starts searching at the end of Source and is case-sensitive. If Search is not found, AnsiPosBackCI
returns zero.
function AnsiPosCI(const Search, Source: AnsiString; const StartPos: Cardinal = 1): Cardinal; |
AnsiPosCI
searches for a substring, Search, in a string, Source, and returns a cardinal value that is the index of the first character of Search within Source. AnsiPosCI
starts searching at the beginning of Source and is case-insensitive. If Search is not found, AnsiPosCI
returns zero.
function AnsiPosCS(const Search, Source: AnsiString; const Start: Cardinal = 1): Cardinal; |
AnsiPosCS
searches for a substring, Search, in a string, Source, and returns a cardinal value that is the index of the first character of Search within Source. AnsiPosCS
starts searching at the beginning of Source and is case-sensitive. If Search is not found, AnsiPosCS
returns zero.
function AnsiProperCase(const s: AnsiString): AnsiString; |
Upper case the first alpha character in each word, lower case all other characters.
procedure AnsiRemoveFromToCI(var Source: AnsiString; const FromString, ToString: AnsiString); |
Repeatedly searches Source for FromString followed by ToString and removes all characters from the beginning of FromString up to the end of ToString from it.
AnsiRemoveFromToCI
can be used, for example, to remove all image tags from a HTML string with the following command:
AnsiRemoveFromToCI
(MyHtmlString,'<IMG', '>')
procedure AnsiReplaceCharCSInPlace(var Source: AnsiString; const SearchChar, ReplaceChar: AnsiChar); |
Replaces all characters SearchChar in Source with character ReplaceChar. The comparison is case sensitive.
function AnsiReplaceCI(const Source, Search, Replace: AnsiString): AnsiString; |
Returns a string with occurrences of one substring replaced by another substring. AnsiReplaceCI
replaces all occurrences of the substring specified by Search with the substring specified by Replace. The entire string will be searched once and all occurrences of Search will be replaced. The comparison operation is case insensitive.
function AnsiReplaceCILoop(const Source, Search, Replace: AnsiString): AnsiString; |
Returns a string with occurrences of one substring replaced by another substring. AnsiReplaceCILoop
replaces all occurrences of the substring specified by Search with the substring specified by Replace. The entire string will be searched multiple times until all occurrences of Search will have been replaced and search is not any longer found in Source. The comparison operation is case insensitive.
function AnsiReplaceCS(const Source, Search, Replace: AnsiString): AnsiString; |
Returns a string with occurrences of one substring replaced by another substring. AnsiReplaceCS
replaces all occurrences of the substring specified by Search with the substring specified by Replace. The entire string will be searched once and all occurrences of Search will be replaced. The comparison operation is case sensitive.
function AnsiReplaceCSLoop(const Source, Search, Replace: AnsiString): AnsiString; |
Returns a string with occurrences of one substring replaced by another substring. AnsiReplaceCSLoop
replaces all occurrences of the substring specified by Search with the substring specified by Replace. The entire string will be searched multiple times until all occurrences of Search will have been replaced and search is not any longer found in Source. The comparison operation is case sensitive.
function AnsiSameCI(const s1, s2: AnsiString): Boolean; |
Compares two AnsiStrings and returns True
if both strings are equal. The comparison is not based on the current locale and is case-insensitive.
function AnsiSameCS(const s1, s2: AnsiString): Boolean; |
Compares two AnsiStrings and returns True
if both strings are equal. The comparison is not based on the current locale and is case-sensitive.
function AnsiSameFrontCI(const s1, s2: AnsiString): Boolean; |
Compares the characters of two strings until the end of the shorter string is reached. Returns True
if the characters up to that point are equal. Comparison is case-insensitive.
function AnsiSameMemCI(const Buffer1, Buffer2: Pointer; const Length: Cardinal): Boolean; |
function AnsiSameMemCS(const Buffer1, Buffer2: Pointer; const Length: Cardinal): Boolean; |
function AnsiScanBackCharCS(const Source: AnsiString; const c: AnsiChar; const Start: Cardinal = 0): Cardinal; |
Backward/reverse scan from Start location (1 = First Char., 0 = String End) looking for single character, c. Returns: Position where/if found; otherwise, 0.
function AnsiScanBackNotSetCS(const Source: AnsiString; const Search: TAnsiCharSet; const Start: Cardinal = 0): Cardinal; |
Backward/reverse scan from Start location (1 = First Char., 0 = String End) looking for first char not in set Search. Returns: Position where/if found; otherwise, 0.
function AnsiScanBackSetCS(const Source: AnsiString; const Search: TAnsiCharSet; const Start: Cardinal = 0): Cardinal; |
Backward/reverse scan from Start location (1 = First Char., 0 = String End) looking for first char in set Search. Returns: Position where/if found; otherwise, 0.
function AnsiScanCharCS(const Source: AnsiString; const c: AnsiChar; const Start: Cardinal = 1): Cardinal; |
Forward scan from Start looking for next matching character (c). Returns: Position where/if found; otherwise, 0.
function AnsiScanNotSetCS(const Source: AnsiString; const Search: TAnsiCharSet; const Start: Cardinal = 1): Cardinal; |
Forward scan from Start looking for next matching character not in set Search. Returns: Position where/if found; otherwise, 0.
function AnsiScanSetCS(const Source: AnsiString; const Search: TAnsiCharSet; const Start: Cardinal = 1): Cardinal; |
Forward scan from Start looking for next matching character in set Search. Returns: Position where/if found; otherwise, 0.
procedure AnsiTomCatBuffer(const Source: Pointer; const SourceLength: Cardinal; var d: AnsiString; var InUse: Cardinal); |
Adds SourceLength bytes of the buffer pointed to by Source to string d.
See AnsiTomCatString
for details on how to use AnsiTomCatBuffer
.
procedure AnsiTomCatChar(const c: AnsiChar; var d: AnsiString; var InUse: Cardinal); |
Adds character c to string d.
See AnsiTomCatString
for details on how to use AnsiTomCatChar
.
procedure AnsiTomCatString(const s: AnsiString; var d: AnsiString; var InUse: Cardinal); |
Appends string s to string d.
String concatenation with smart memory allocation. Offers a speed advantage when building a long resultant string (d) from many small string fragments.
InUse
is a user-supplied variable which is updated by the procedure to track the portion of d
actually "in use" at any time (typically less than the allocated length). Initialize to zero or Length(d)
as appropriate at the outset but do not manually alter otherwise.
Once concatenation is finished, use SetLength(d,InUse)
to trim any unused excess from the resultant.
See also: AnsiTomCatChar
, AnsiTomCatBuffer
.
function AnsiTrimCharCS(const Source: AnsiString; const c: AnsiChar): AnsiString; |
Trims leading and trailing characters c from a string.
function AnsiTrimSetCS(const Source: AnsiString; const s: TAnsiCharSet): AnsiString; |
Trims leading and trailing characters in set s from a string.
function AnsiUpperCase(const s: AnsiString): AnsiString; |
Returns a string that is a copy of the given string converted to upper case. The conversion does not use the current locale.
function GetLastErrorString: AnsiString; |
Retunrs a string representation of the GetLastError
Windows API function. Uses SystemMessageString
to convert the error code into a string.
procedure SkipAnsiSet(var p: PAnsiChar; const Search: TAnsiCharSet); |
Advances p, skipping all characters in set search until character is not in Search any more. Does not stop at #0.
procedure SkipNotAnsiSet(var p: PAnsiChar; const Search: TAnsiCharSet); |
Advances p, skipping all characters not in set search until character is in Search. Does not stop at #0.
function SystemMessageString(const MessageID: Cardinal): AnsiString; |
SystemMessageString
returns an error message string that corresponds to the specified Win32 API error code. It is a wrapper around the FormatMessage Windows API function. SystemMessageString
can be used to convert Windows error codes into strings.
function ZAnsiScanNotSetCS(const Source: PAnsiChar; const Search: TAnsiCharSet): Integer; |
Searches Source for a character in set Search. Source is a zero-terminated string. Search stops on match or at the end of string character (#0). If found, ZAnsiScanNotSetCS
returns the 0-based position of character in Source. If not found, ZAnsiScanNotSetCS
returns -1.
None.
Name | Description |
---|---|
ANSI_ALPHAS | ANSI_LOW_ALPHAS + ANSI_HIGH_ALPHAS |
ANSI_ALPHA_NUMS | ANSI_ALPHAS + ANSI_DIGITS |
ANSI_DIGITS | [AnsiChar('0')..AnsiChar('9')] |
ANSI_HIGH_ALPHAS | [AnsiChar('A')..AnsiChar('Z'), AnsiChar('Ä'), AnsiChar('Ö'), AnsiChar('Ü')] |
ANSI_LOWERS | [AnsiChar(#0)..AnsiChar(#255)] - ANSI_UPPERS |
ANSI_LOWER_CHAR_TABLE | Table of corresponding Ansi lower characters. ANSI_LOWER_CHAR_TABLE ['A'] returns 'a'. |
ANSI_LOW_ALPHAS | [AnsiChar('a')..AnsiChar('z'), AnsiChar('ä'), AnsiChar('ö'), AnsiChar('ü'), AnsiChar('ß')] |
ANSI_PATH_SEPARATOR | AnsiChar('\') |
ANSI_PUNCTS | [AnsiChar('.'), AnsiChar(','), AnsiChar(':'), AnsiChar(';')] |
ANSI_QUOTES | [AnsiChar('"'), AnsiChar('''')] |
ANSI_REVERSE_CHAR_TABLE | Table of corresponding Ansi reverse characters. ANSI_REVERSE_CHAR_TABLE ['A'] returns 'a' and ANSI_REVERSE_CHAR_TABLE ['a'] returns 'A'. |
ANSI_UPPERS | [AnsiChar(#1)..AnsiChar('`'), AnsiChar('{')..AnsiChar('™'), AnsiChar('›'), AnsiChar('Ÿ'), AnsiChar('£')..AnsiChar('´'), AnsiChar('¶')..AnsiChar('Ý')] |
ANSI_UPPER_CHAR_TABLE | Table of corresponding Ansi upper characters. ANSI_UPPER_CHAR_TABLE ['a'] returns 'A'. |
ANSI_WHITE_SPACE | [AnsiChar(#1)..AnsiChar(#32)] |
ANSI_WORD_SEPARATORS | ANSI_WHITE_SPACE + ANSI_DIGITS + ANSI_PUNCTS + [AnsiChar('"'), AnsiChar('-'), AnsiChar('/')] |
ANSI_XDIGITS | ANSI_DIGITS + [AnsiChar('A')..AnsiChar('F'), AnsiChar('a')..AnsiChar('f')] |
None.
Ralf Junker -- delphi@zeitungsjunge.de