Menu

[r1446]: / branches / v4-dev / Src / UStrUtils.pas  Maximize  Restore  History

Download this file

289 lines (234 with data), 8.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
{
* UStrUtils.pas
*
* String utility routines.
*
* Some of the routines are simply wrappers around SysUtils and StrUtils string
* routines with the purpose of (a) collecting them together in one place and
* (b) providing a standardised parameter order.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is UStrUtils.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2011 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit UStrUtils;
interface
/// <summary>Checks if string Haystack contains string Needle. Case sensitive.
/// </summary>
function StrContainsStr(const Needle, Haystack: string): Boolean;
/// <summary>Returns index of string Needle in string Haystack or 0 if Needle
/// is not in Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: string): Integer; overload;
/// <summary>Returns index of string Needle in string Haystack, beginning the
/// search at Offset. Returns 0 if Haystack is not found or if Offset is less
/// than 1 or greater than length of Haystack. Case sensitive.</summary>
function StrPos(const Needle, Haystack: string; const Offset: Integer): Integer;
overload;
/// <summary>Returns index of last occurence of string Needle in string
/// Haystack or 0 if Needle is not in Haystack. Case sensitive.</summary>
function StrLastPos(const Needle, Haystack: string): Integer;
/// <summary>Compares Left and Right strings, ignoring case. Returns 0 if both
/// strings are the same, -ve if Left is less than Right or +ve if Left is
/// greater than Right.</summary>
function StrCompareText(const Left, Right: string): Integer;
/// <summary>Converts a string to upper case.</summary>
function StrToUpper(const Str: string): string;
/// <summary>Converts a string to lower case.</summary>
function StrToLower(const Str: string): string;
/// <summary>Returns a substring of Str starting at index StartIdx of length
/// Count.</summary>
/// <remarks>
/// <para>If Count specifies more than the available number of characters, the
/// substring from StartIdx to the end of the string is returned.</para>
/// <para>If StartIdx is beyond the end of Str the empty string is returned.
/// </para>
/// </remarks>
function StrSlice(const Str: string; const StartIdx, Count: Integer): string;
/// <summary>Returns the leading characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceLeft(const Str: string; const Count: Integer): string;
/// <summary>Returns the trailing characters of Str up to a length of Count
/// characters.</summary>
/// <remarks>If Count is greater than the length of Str the whole string is
/// returned.</remarks>
function StrSliceRight(const Str: string; const Count: Integer): string;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// sensitive.</summary>
function StrStartsStr(const SubStr, Str: string): Boolean;
/// <summary>Checks if string Str begins with sub string SubStr. Case
/// insensitive.</summary>
function StrStartsText(const SubStr, Str: string): Boolean;
/// <summary>Replaces all occurences of FindStr in Str with ReplaceStr.
/// </summary>
function StrReplace(const Str, FindStr, ReplaceStr: string): string;
/// <summary>Trims leading and trailing space characters from a string.
/// </summary>
function StrTrimSpaces(const Str: string): string;
/// <summary>Trims leading space characters from a string.</summary>
function StrTrimLeftSpaces(const Str: string): string;
/// <summary>Trims trailing space characters from a string.</summary>
function StrTrimRightSpaces(const Str: string): string;
/// <summary>Trims leading and trailing characters C from string Str.</summary>
function StrTrimChars(const Str: string; const C: Char): string;
/// <summary>Trims leading characters C from string Str.</summary>
function StrTrimLeftChars(const Str: string; const C: Char): string;
/// <summary>Trims trailing characters C from string Str.</summary>
function StrTrimRightChars(const Str: string; const C: Char): string;
/// <summary>Ensures line breaks in a string are in Windows format (CRLF).
/// </summary>
/// <remarks>Converts both Unix (LF) and Macintosh (CR) to CRLF.</remarks>
function StrWindowsLineBreaks(const Str: string): string;
/// <summary>Ensures line breaks in a string are in Unix format (LF).
/// </summary>
/// <remarks>Converts both Windows (CRLF) and Macintosh (CR) to LF.</remarks>
function StrUnixLineBreaks(const Str: string): string;
implementation
uses
// Delphi
SysUtils, StrUtils,
// Project
UConsts;
function StrCompareText(const Left, Right: string): Integer;
begin
Result := SysUtils.AnsiCompareText(Left, Right);
end;
function StrContainsStr(const Needle, Haystack: string): Boolean;
begin
Result := StrUtils.AnsiContainsStr(Haystack, Needle);
end;
function StrLastPos(const Needle, Haystack: string): Integer;
var
Idx: Integer; // an index of Needle in Haystack
begin
Result := 0;
Idx := StrPos(Needle, Haystack);
if Idx = 0 then
Exit;
while Idx > 0 do
begin
Result := Idx;
Idx := StrPos(Needle, Haystack, Idx + 1);
end;
end;
function StrPos(const Needle, Haystack: string): Integer;
begin
Result := SysUtils.AnsiPos(Needle, Haystack);
end;
function StrPos(const Needle, Haystack: string; const Offset: Integer): Integer;
begin
Result := StrUtils.PosEx(Needle, Haystack, Offset);
end;
function StrReplace(const Str, FindStr, ReplaceStr: string): string;
begin
Result := StrUtils.AnsiReplaceStr(Str, FindStr, ReplaceStr);
end;
function StrSlice(const Str: string; const StartIdx, Count: Integer): string;
begin
Result := StrUtils.AnsiMidStr(Str, StartIdx, Count);
end;
function StrSliceLeft(const Str: string; const Count: Integer): string;
begin
Result := StrUtils.AnsiLeftStr(Str, Count);
end;
function StrSliceRight(const Str: string; const Count: Integer): string;
begin
Result := StrUtils.AnsiRightStr(Str, Count);
end;
function StrStartsStr(const SubStr, Str: string): Boolean;
begin
Result := StrUtils.AnsiStartsStr(SubStr, Str);
end;
function StrStartsText(const SubStr, Str: string): Boolean;
begin
Result := StrUtils.AnsiStartsText(SubStr, Str);
end;
function StrToLower(const Str: string): string;
begin
Result := SysUtils.AnsiLowerCase(Str);
end;
function StrToUpper(const Str: string): string;
begin
Result := SysUtils.AnsiUpperCase(Str);
end;
function StrTrimChars(const Str: string; const C: Char): string;
begin
Result := StrTrimLeftChars(StrTrimRightChars(Str, C), C);
end;
function StrTrimLeftChars(const Str: string; const C: Char): string;
var
Idx: Integer; // index into string
begin
Idx := 1;
while (Idx <= Length(Str)) and (Str[Idx] = C) do
Inc(Idx);
if Idx > 1 then
Result := Copy(Str, Idx, MaxInt)
else
Result := Str;
end;
function StrTrimLeftSpaces(const Str: string): string;
begin
Result := SysUtils.TrimLeft(Str);
end;
function StrTrimRightChars(const Str: string; const C: Char): string; overload;
var
Idx: Integer; // index into string
begin
Idx := Length(Str);
while (Idx >= 1) and (Str[Idx] = C) do
Dec(Idx);
if Idx < Length(Str) then
Result := Copy(Str, 1, Idx)
else
Result := Str;
end;
function StrTrimRightSpaces(const Str: string): string;
begin
Result := SysUtils.TrimRight(Str);
end;
function StrTrimSpaces(const Str: string): string;
begin
Result := SysUtils.Trim(Str);
end;
function StrUnixLineBreaks(const Str: string): string;
begin
// Replace any CRLF (MSDOS/Windows) line ends with LF
Result := StrReplace(Str, CRLF, LF);
// Replace any remaining CR (Mac) line ends with LF
Result := StrReplace(Result, CR, LF);
end;
function StrWindowsLineBreaks(const Str: string): string;
begin
// First convert to Unix to get rid of all CR characters (CRs could come from
// existing Windows CRLF or from Max CR.
Result := StrUnixLineBreaks(Str);
// Now have only LFs - convert them all to CRLFs
Result := StrReplace(Result, LF, CRLF);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.