1 |
// Upgraded to Delphi 2009: Sebastian Zierer
|
2 |
// TODO-UNICODE - Upgrade to Unicode (BM asm code is a blocking issue)
|
3 |
|
4 |
(* ***** BEGIN LICENSE BLOCK *****
|
5 |
* Version: MPL 1.1
|
6 |
*
|
7 |
* The contents of this file are subject to the Mozilla Public License Version
|
8 |
* 1.1 (the "License"); you may not use this file except in compliance with
|
9 |
* the License. You may obtain a copy of the License at
|
10 |
* http://www.mozilla.org/MPL/
|
11 |
*
|
12 |
* Software distributed under the License is distributed on an "AS IS" basis,
|
13 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
14 |
* for the specific language governing rights and limitations under the
|
15 |
* License.
|
16 |
*
|
17 |
* The Original Code is TurboPower SysTools
|
18 |
*
|
19 |
* The Initial Developer of the Original Code is
|
20 |
* TurboPower Software
|
21 |
*
|
22 |
* Portions created by the Initial Developer are Copyright (C) 1996-2002
|
23 |
* the Initial Developer. All Rights Reserved.
|
24 |
*
|
25 |
* Contributor(s):
|
26 |
*
|
27 |
* ***** END LICENSE BLOCK ***** *)
|
28 |
|
29 |
{*********************************************************}
|
30 |
{* SysTools: StOStr.pas 4.04 *}
|
31 |
{*********************************************************}
|
32 |
{* SysTools: String class *}
|
33 |
{*********************************************************}
|
34 |
|
35 |
{$I StDefine.inc}
|
36 |
|
37 |
unit StOStr;
|
38 |
|
39 |
interface
|
40 |
|
41 |
uses
|
42 |
Windows, SysUtils, Classes,
|
43 |
StConst, StBase, StStrZ;
|
44 |
|
45 |
const
|
46 |
{.Z+}
|
47 |
DefAllocSize = 8;
|
48 |
DefDelimiters = ' ';
|
49 |
DefQuote = '''';
|
50 |
DefRepeatValue = 1;
|
51 |
DefResetRepeat = True;
|
52 |
DefTabSize = 8;
|
53 |
DefWrap = 80;
|
54 |
{.Z-}
|
55 |
|
56 |
type
|
57 |
|
58 |
TStString = class(TPersistent)
|
59 |
private
|
60 |
{.Z+}
|
61 |
protected {private}
|
62 |
FAlloc : Cardinal;
|
63 |
FBM : BTable;
|
64 |
FBMString : PAnsiChar;
|
65 |
FCursor : PAnsiChar;
|
66 |
FDelimiters : PAnsiChar;
|
67 |
FEnableCursor : Boolean;
|
68 |
FItems : TStringList;
|
69 |
FOneBased : Boolean;
|
70 |
FRepeatValue : Cardinal;
|
71 |
FResetRepeat : Boolean;
|
72 |
FQuote : AnsiChar;
|
73 |
FString : PAnsiChar;
|
74 |
FTabSize : Byte;
|
75 |
FTemp : PAnsiChar;
|
76 |
FTempAlloc : Cardinal;
|
77 |
FWrap : Cardinal;
|
78 |
FLineTermChar: AnsiChar;
|
79 |
FLineTerminator: TStLineTerminator;
|
80 |
protected
|
81 |
procedure SetLineTerm(const Value: TStLineTerminator);
|
82 |
procedure SetLineTermChar(const Value: AnsiChar);
|
83 |
procedure AddIntToList(Num : LongInt);
|
84 |
procedure AllocTemp(Size : Cardinal);
|
85 |
procedure BMMakeTable(S : PAnsiChar);
|
86 |
procedure CheckAlloc(Size : Cardinal);
|
87 |
function DesiredCursor : PAnsiChar;
|
88 |
procedure FixCursor(Pos, Size : Cardinal; Delete : Boolean);
|
89 |
function Get(Index : Cardinal) : AnsiChar;
|
90 |
function GetAsciiCount : Cardinal;
|
91 |
function GetAsShortStr : ShortString;
|
92 |
function GetCursorPos : Cardinal;
|
93 |
function GetDelimiters : AnsiString;
|
94 |
function GetLength : Cardinal;
|
95 |
function GetRelativePos(Pos : Cardinal) : Cardinal;
|
96 |
function GetSoundex : AnsiString;
|
97 |
function GetWordCount : Cardinal;
|
98 |
procedure Put(Index : Cardinal; Item : AnsiChar);
|
99 |
procedure SetAllocLength(Value : Cardinal);
|
100 |
procedure SetAsShortStr(Value : ShortString);
|
101 |
procedure SetCursorPos(Value : Cardinal);
|
102 |
procedure SetDelimiters(Value : AnsiString);
|
103 |
procedure SetItems(Value : TStringList);
|
104 |
function SuggestSize(Size : Cardinal) : Cardinal;
|
105 |
procedure TempToString;
|
106 |
procedure UpdateCursor(Pos : Cardinal);
|
107 |
function GetAsLongStr : AnsiString;
|
108 |
procedure SetAsLongStr(Value : AnsiString);
|
109 |
function GetAsVariant : Variant;
|
110 |
procedure SetAsVariant(Value : Variant);
|
111 |
function MakeTerminator(var Terminator : PAnsiChar) : Integer; {!!.01}
|
112 |
|
113 |
{.Z-}
|
114 |
public
|
115 |
constructor Create;
|
116 |
constructor CreateAlloc(Size : Cardinal); virtual;
|
117 |
constructor CreateS(const S : AnsiString); virtual;
|
118 |
constructor CreateZ(const S : PAnsiChar); virtual;
|
119 |
constructor CreateV(const S : Variant); virtual;
|
120 |
destructor Destroy; override;
|
121 |
procedure AppendPChar(S : PAnsiChar);
|
122 |
procedure AppendString(S : AnsiString);
|
123 |
function AsciiPosition(N : Cardinal; var Pos : Cardinal) : Boolean;
|
124 |
function BMSearch(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
125 |
function BMSearchUC(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
126 |
procedure Center(Size : Cardinal);
|
127 |
procedure CenterCh(const C : AnsiChar; Size : Cardinal);
|
128 |
function CharCount(const C : AnsiChar) : Cardinal;
|
129 |
function CharExists(const C : AnsiChar) : boolean;
|
130 |
procedure CharStr(const C : AnsiChar; Size : Cardinal);
|
131 |
procedure ClearItems;
|
132 |
procedure CursorNextWord;
|
133 |
procedure CursorNextWordPrim;
|
134 |
procedure CursorPrevWord;
|
135 |
procedure CursorPrevWordPrim;
|
136 |
procedure CursorToEnd;
|
137 |
procedure DeleteAsciiAtCursor;
|
138 |
procedure DeleteAtCursor(Length : Cardinal);
|
139 |
procedure DeleteWordAtCursor;
|
140 |
procedure Detab;
|
141 |
procedure Entab;
|
142 |
function ExtractAscii(N : Cardinal) : AnsiString;
|
143 |
function ExtractWord(N : Cardinal) : AnsiString;
|
144 |
procedure Filter(const Filters : PAnsiChar);
|
145 |
function GetAsciiAtCursor : AnsiString;
|
146 |
function GetAsciiAtCursorZ(Dest : PAnsiChar) : PAnsiChar;
|
147 |
function GetAsPChar(Dest : PAnsiChar) : PAnsiChar;
|
148 |
function GetWordAtCursor : AnsiString;
|
149 |
function GetWordAtCursorZ(Dest : PAnsiChar) : PAnsiChar;
|
150 |
procedure InsertLineTerminatorAtCursor;
|
151 |
procedure InsertLineTerminator(Pos : Cardinal);
|
152 |
procedure InsertPCharAtCursor(S : PAnsiChar);
|
153 |
procedure InsertStringAtCursor(S : AnsiString);
|
154 |
procedure ItemsToString;
|
155 |
procedure LeftPad(Size : Cardinal);
|
156 |
procedure LeftPadCh(const C : AnsiChar; Size : Cardinal);
|
157 |
function MakeLetterSet : LongInt;
|
158 |
procedure MoveCursor(Delta : Integer);
|
159 |
procedure Pack;
|
160 |
procedure Pad(Size : Cardinal);
|
161 |
procedure PadCh(const C : AnsiChar; Size : Cardinal);
|
162 |
procedure ResetCursor;
|
163 |
procedure Scramble(const Key : AnsiString);
|
164 |
procedure SetAsPChar(S : PAnsiChar);
|
165 |
function SizeAsciiAtCursor(InclTrailers : Boolean) : Cardinal;
|
166 |
function SizeWordAtCursor(InclTrailers : Boolean) : Cardinal;
|
167 |
procedure StrChDelete(Pos : Cardinal);
|
168 |
procedure StrChInsert(const C : AnsiChar; Pos : Cardinal);
|
169 |
function StrChPos(const C : AnsiChar; var Pos : Cardinal) : Boolean;
|
170 |
procedure StringToItems;
|
171 |
procedure StripLineTerminators;
|
172 |
procedure StrStDelete(const Pos, Length : Cardinal);
|
173 |
procedure StrStInsert(const S : AnsiString; Pos : Cardinal);
|
174 |
function StrStPos(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
175 |
procedure Substitute(FromStr, ToStr : PAnsiChar);
|
176 |
procedure Trim;
|
177 |
procedure TrimLead;
|
178 |
procedure TrimSpaces;
|
179 |
procedure TrimTrail;
|
180 |
function WordPosition(N : Cardinal; var Pos : Cardinal) : Boolean;
|
181 |
procedure WrapToItems;
|
182 |
|
183 |
property AllocLength : Cardinal
|
184 |
read FAlloc write SetAllocLength;
|
185 |
property AsciiCount : Cardinal
|
186 |
read GetAsciiCount;
|
187 |
property AsLongStr : AnsiString
|
188 |
read GetAsLongStr write SetAsLongStr;
|
189 |
property AsVariant : Variant
|
190 |
read GetAsVariant write SetAsVariant;
|
191 |
property AsShortStr : ShortString
|
192 |
read GetAsShortStr write SetAsShortStr;
|
193 |
property AtIndex[Index: Cardinal]: AnsiChar
|
194 |
read Get write Put; default;
|
195 |
property CursorPos : Cardinal
|
196 |
read GetCursorPos write SetCursorPos;
|
197 |
property Delimiters : AnsiString
|
198 |
read GetDelimiters write SetDelimiters;
|
199 |
property EnableCursor : Boolean
|
200 |
read FEnableCursor write FEnableCursor;
|
201 |
property Length : Cardinal
|
202 |
read GetLength;
|
203 |
property LineTermChar : AnsiChar
|
204 |
read FLineTermChar write SetLineTermChar default #10;
|
205 |
property LineTerminator : TStLineTerminator
|
206 |
read FLineTerminator write SetLineTerm default ltCRLF;
|
207 |
property Items : TStringList
|
208 |
read FItems write SetItems;
|
209 |
property OneBased : Boolean
|
210 |
read FOneBased write FOneBased;
|
211 |
property RepeatValue : Cardinal
|
212 |
read FRepeatValue write FRepeatValue;
|
213 |
property ResetRepeat : Boolean
|
214 |
read FResetRepeat write FResetRepeat;
|
215 |
property Soundex : AnsiString
|
216 |
read GetSoundex;
|
217 |
property Quote : AnsiChar
|
218 |
read FQuote write FQuote;
|
219 |
property TabSize : Byte
|
220 |
read FTabSize write FTabSize;
|
221 |
property WordCount : Cardinal
|
222 |
read GetWordCount;
|
223 |
property WrapColumn : Cardinal
|
224 |
read FWrap write FWrap;
|
225 |
end;
|
226 |
|
227 |
implementation
|
228 |
|
229 |
{$IFNDEF UNICODE}
|
230 |
function AnsiStrAlloc(Size: Cardinal): PAnsiChar;
|
231 |
begin
|
232 |
Result := StrAlloc(Size);
|
233 |
end;
|
234 |
{$ENDIF}
|
235 |
|
236 |
constructor TStString.Create;
|
237 |
{- Create nil string object. }
|
238 |
begin
|
239 |
inherited Create;
|
240 |
SetDelimiters(DefDelimiters);
|
241 |
FItems := TStringList.Create;
|
242 |
FTabSize := DefTabSize;
|
243 |
FQuote := DefQuote;
|
244 |
FRepeatValue := DefRepeatValue;
|
245 |
FResetRepeat := DefResetRepeat;
|
246 |
FWrap := DefWrap;
|
247 |
|
248 |
FLineTerminator := ltCRLF;
|
249 |
FLineTermChar := #10;
|
250 |
|
251 |
|
252 |
end;
|
253 |
|
254 |
constructor TStString.CreateAlloc(Size : Cardinal);
|
255 |
{- Create string object allocated to given size. }
|
256 |
var
|
257 |
AllocSize : Cardinal;
|
258 |
begin
|
259 |
Create;
|
260 |
AllocSize := SuggestSize(Size);
|
261 |
FString := AnsiStrAlloc(AllocSize);
|
262 |
FString[0] := #0;
|
263 |
FAlloc := AllocSize;
|
264 |
ResetCursor;
|
265 |
end;
|
266 |
|
267 |
constructor TStString.CreateV(const S : Variant);
|
268 |
{- Create string object and copy variant into it. }
|
269 |
var
|
270 |
Len : Cardinal;
|
271 |
Temp : AnsiString;
|
272 |
begin
|
273 |
Create;
|
274 |
Temp := S;
|
275 |
Len := System.Length(Temp);
|
276 |
FString := AnsiStrAlloc(SuggestSize(Len));
|
277 |
if Assigned(FString) then begin
|
278 |
FAlloc := SuggestSize(Len);
|
279 |
StrCopy(FString, PAnsiChar(Temp));
|
280 |
end;
|
281 |
ResetCursor;
|
282 |
end;
|
283 |
|
284 |
constructor TStString.CreateS(const S : AnsiString);
|
285 |
{- Create string object and copy string into it. }
|
286 |
begin
|
287 |
Create;
|
288 |
FString := AnsiStrAlloc(SuggestSize(System.Length(S)));
|
289 |
if Assigned(FString) then begin
|
290 |
FAlloc := SuggestSize(System.Length(S));
|
291 |
StrPCopy(FString, S);
|
292 |
end;
|
293 |
ResetCursor;
|
294 |
end;
|
295 |
|
296 |
constructor TStString.CreateZ(const S : PAnsiChar);
|
297 |
{- Create string object and copy PChar into it. }
|
298 |
begin
|
299 |
Create;
|
300 |
FString := AnsiStrAlloc(SuggestSize(StrLen(S)));
|
301 |
if Assigned(FString) then begin
|
302 |
StrCopy(FString, S);
|
303 |
FAlloc := SuggestSize(StrLen(S));
|
304 |
end;
|
305 |
ResetCursor;
|
306 |
end;
|
307 |
|
308 |
destructor TStString.Destroy;
|
309 |
{- Dispose string object. }
|
310 |
begin
|
311 |
FItems.Free;
|
312 |
StrDispose(FBMString);
|
313 |
StrDispose(FDelimiters);
|
314 |
StrDispose(FString);
|
315 |
inherited Destroy;
|
316 |
end;
|
317 |
|
318 |
procedure TStString.AppendPChar(S : PAnsiChar);
|
319 |
{- Appends PChar to end of string. }
|
320 |
var
|
321 |
Temp : PAnsiChar;
|
322 |
begin
|
323 |
CheckAlloc(StrLen(S) + GetLength);
|
324 |
Temp := StrEnd(FString);
|
325 |
StrCopy(Temp, S);
|
326 |
end;
|
327 |
|
328 |
procedure TStString.AppendString(S : AnsiString);
|
329 |
{- Appends string to end of string. }
|
330 |
var
|
331 |
Temp : PAnsiChar;
|
332 |
begin
|
333 |
CheckAlloc(System.Length(S) + LongInt(GetLength));
|
334 |
Temp := StrEnd(FString);
|
335 |
StrPCopy(Temp, S);
|
336 |
end;
|
337 |
|
338 |
function TStString.AsciiPosition(N : Cardinal; var Pos : Cardinal) : Boolean;
|
339 |
{- Returns the Pos of the Nth word using ASCII rules. }
|
340 |
var
|
341 |
I, Num : Cardinal;
|
342 |
begin
|
343 |
Result := False;
|
344 |
Num := N;
|
345 |
ClearItems;
|
346 |
for I := 1 to FRepeatValue do begin
|
347 |
if AsciiPositionZ(Num, DesiredCursor, FDelimiters, FQuote, Pos) then begin
|
348 |
if Result = False then Inc(Num);
|
349 |
Pos := GetRelativePos(Pos);
|
350 |
Result := True;
|
351 |
UpdateCursor(Pos);
|
352 |
if FOneBased then Inc(Pos);
|
353 |
AddIntToList(Pos);
|
354 |
end
|
355 |
else Break;
|
356 |
end;
|
357 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
358 |
end;
|
359 |
|
360 |
function TStString.BMSearch(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
361 |
{- Performs case sensitive BM search on string. }
|
362 |
var
|
363 |
I : Cardinal;
|
364 |
Temp : PAnsiChar;
|
365 |
begin
|
366 |
Result := False;
|
367 |
ClearItems;
|
368 |
Temp := AnsiStrAlloc(Succ(System.Length(S)));
|
369 |
try
|
370 |
StrPCopy(Temp, S);
|
371 |
BMMakeTable(Temp);
|
372 |
for I := 1 to FRepeatValue do begin
|
373 |
if BMSearchZ(DesiredCursor^, StrLen(DesiredCursor), FBM, Temp, Pos) then begin
|
374 |
Result := True;
|
375 |
Pos := GetRelativePos(Pos);
|
376 |
UpdateCursor(Pos);
|
377 |
if FOneBased then Inc(Pos);
|
378 |
AddIntToList(Pos);
|
379 |
Inc(FCursor);
|
380 |
end else Break;
|
381 |
end;
|
382 |
if Result then Dec(FCursor);
|
383 |
finally
|
384 |
StrDispose(Temp);
|
385 |
end;
|
386 |
end;
|
387 |
|
388 |
function TStString.BMSearchUC(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
389 |
{- Performs case insensitive BM search on string. }
|
390 |
var
|
391 |
I : Cardinal;
|
392 |
Temp : PAnsiChar;
|
393 |
begin
|
394 |
Result := False;
|
395 |
ClearItems;
|
396 |
Temp := AnsiStrAlloc(Succ(System.Length(S)));
|
397 |
try
|
398 |
StrPCopy(Temp, S);
|
399 |
StrUpper(Temp);
|
400 |
BMMakeTable(Temp);
|
401 |
for I := 1 to FRepeatValue do begin
|
402 |
if BMSearchUCZ(DesiredCursor^, StrLen(DesiredCursor), FBM, Temp, Pos) then begin
|
403 |
Result := True;
|
404 |
Pos := GetRelativePos(Pos);
|
405 |
UpdateCursor(Pos);
|
406 |
if FOneBased then Inc(Pos);
|
407 |
AddIntToList(Pos);
|
408 |
Inc(FCursor);
|
409 |
end else Break;
|
410 |
end;
|
411 |
if Result then Dec(FCursor);
|
412 |
finally
|
413 |
StrDispose(Temp);
|
414 |
end;
|
415 |
end;
|
416 |
|
417 |
procedure TStString.Center(Size : Cardinal);
|
418 |
{- Centers string to Size. }
|
419 |
begin
|
420 |
CheckAlloc(Size);
|
421 |
CenterPrimZ(FString, Size);
|
422 |
ResetCursor;
|
423 |
end;
|
424 |
|
425 |
procedure TStString.CenterCh(const C : AnsiChar; Size : Cardinal);
|
426 |
{- Centers string with 'Ch' to Size. }
|
427 |
begin
|
428 |
CheckAlloc(Size);
|
429 |
CenterChPrimZ(FString, C, Size);
|
430 |
ResetCursor;
|
431 |
end;
|
432 |
|
433 |
function TStString.CharExists(const C : AnsiChar) : boolean;
|
434 |
{- Determines whether C exists in string. }
|
435 |
begin
|
436 |
Result := CharExistsZ(DesiredCursor, C);
|
437 |
end;
|
438 |
|
439 |
function TStString.CharCount(const C : AnsiChar) : Cardinal;
|
440 |
{- Counts C in string. }
|
441 |
begin
|
442 |
Result := CharCountZ(DesiredCursor, C);
|
443 |
end;
|
444 |
|
445 |
procedure TStString.CharStr(const C : AnsiChar; Size : Cardinal);
|
446 |
{- Fills string to Size with C. }
|
447 |
begin
|
448 |
CheckAlloc(Size);
|
449 |
FString := CharStrZ(FString, C, Size);
|
450 |
ResetCursor;
|
451 |
end;
|
452 |
|
453 |
procedure TStString.AddIntToList(Num : LongInt);
|
454 |
{- Adds integer value to Items -- as both numeric value and numeric string. }
|
455 |
begin
|
456 |
FItems.AddObject(IntToStr(Num), TObject(Num));
|
457 |
end;
|
458 |
|
459 |
procedure TStString.AllocTemp(Size : Cardinal);
|
460 |
{- Allocates FTemp to Size. }
|
461 |
begin
|
462 |
FTemp := AnsiStrAlloc(Size);
|
463 |
FTempAlloc := Size;
|
464 |
end;
|
465 |
|
466 |
procedure TStString.BMMakeTable(S : PAnsiChar);
|
467 |
{- Checks whether table needs to be made -- and makes it. }
|
468 |
begin
|
469 |
if Assigned(FBMString) then
|
470 |
if StrComp(S, FBMString) = 0 then Exit;
|
471 |
StrDispose(FBMString);
|
472 |
FBMString := StrNew(S);
|
473 |
BMMakeTableZ(FBMString, FBM);
|
474 |
end;
|
475 |
|
476 |
procedure TStString.CheckAlloc(Size : Cardinal);
|
477 |
{- Sets allocated length for string if needed size is > current size. }
|
478 |
begin
|
479 |
if FAlloc = 0 then begin
|
480 |
FString := AnsiStrAlloc(SuggestSize(Size));
|
481 |
FAlloc := SuggestSize(Size);
|
482 |
FString[0] := #0;
|
483 |
ResetCursor;
|
484 |
end else if Succ(Size) > FAlloc then
|
485 |
SetAllocLength(Succ(Size));
|
486 |
end;
|
487 |
|
488 |
procedure TStString.ClearItems;
|
489 |
{- Clears Items list. }
|
490 |
begin
|
491 |
FItems.Clear;
|
492 |
end;
|
493 |
|
494 |
procedure TStString.CursorNextWord;
|
495 |
{- Moves cursor to the beginning of the next word, or terminating null. }
|
496 |
var
|
497 |
I : Cardinal;
|
498 |
begin
|
499 |
for I := 1 to FRepeatValue do begin
|
500 |
CursorNextWordPrim;
|
501 |
end;
|
502 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
503 |
end;
|
504 |
|
505 |
procedure TStString.CursorNextWordPrim;
|
506 |
{- Moves cursor to the beginning of the next word, or terminating null. }
|
507 |
var
|
508 |
Ch : AnsiChar;
|
509 |
begin
|
510 |
Ch := FCursor^;
|
511 |
while (Ch <> #0) and (not CharExistsZ(FDelimiters, Ch)) do begin
|
512 |
Inc(FCursor);
|
513 |
Ch := FCursor^;
|
514 |
end;
|
515 |
while (Ch <> #0) and (CharExistsZ(FDelimiters, Ch)) do begin
|
516 |
Inc(FCursor);
|
517 |
Ch := FCursor^;
|
518 |
end;
|
519 |
end;
|
520 |
|
521 |
procedure TStString.CursorPrevWord;
|
522 |
{- Move Cursor to beginning of prev word, or first word in string. }
|
523 |
var
|
524 |
I : Cardinal;
|
525 |
begin
|
526 |
for I := 1 to FRepeatValue do begin
|
527 |
CursorPrevWordPrim;
|
528 |
end;
|
529 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
530 |
end;
|
531 |
|
532 |
|
533 |
procedure TStString.CursorPrevWordPrim;
|
534 |
{- Move Cursor to beginning of prev word, or first word in string. }
|
535 |
var
|
536 |
Ch : AnsiChar;
|
537 |
i : integer;
|
538 |
begin
|
539 |
Ch := FCursor^;
|
540 |
{go around twice to get to the previous word, not the current word}
|
541 |
for i := 1 to 2 do begin
|
542 |
while (FCursor <> FString) and (CharExistsZ(FDelimiters, Ch)) do begin
|
543 |
Dec(FCursor);
|
544 |
Ch := FCursor^;
|
545 |
end;
|
546 |
while (FCursor <> FString) and (not CharExistsZ(FDelimiters, Ch)) do begin
|
547 |
Dec(FCursor);
|
548 |
Ch := FCursor^;
|
549 |
end;
|
550 |
end;
|
551 |
{now get off the delimiter }
|
552 |
if (FCursor <> FString) then
|
553 |
Inc(FCursor);
|
554 |
end;
|
555 |
|
556 |
procedure TStString.CursorToEnd;
|
557 |
{- Set cursor to null terminator at the end of string. }
|
558 |
begin
|
559 |
FCursor := StrEnd(FString);
|
560 |
end;
|
561 |
|
562 |
procedure TStString.DeleteAsciiAtCursor;
|
563 |
{- Deletes word (and any trailing delimiters) at cursor following ASCII rules. }
|
564 |
var
|
565 |
I : Cardinal;
|
566 |
begin
|
567 |
for I := 1 to FRepeatValue do begin
|
568 |
StrStDeletePrimZ(FCursor, 0, SizeAsciiAtCursor(True));
|
569 |
end;
|
570 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
571 |
end;
|
572 |
|
573 |
procedure TStString.DeleteAtCursor(Length : Cardinal);
|
574 |
{- Deletes Length number of characters at cursor. }
|
575 |
begin
|
576 |
StrStDeletePrimZ(FCursor, 0, Length);
|
577 |
end;
|
578 |
|
579 |
procedure TStString.DeleteWordAtCursor;
|
580 |
{- Deletes word (and any trailing delimiters) at cursor. }
|
581 |
var
|
582 |
I : Cardinal;
|
583 |
begin
|
584 |
for I := 1 to FRepeatValue do begin
|
585 |
StrStDeletePrimZ(FCursor, 0, SizeWordAtCursor(True));
|
586 |
end;
|
587 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
588 |
end;
|
589 |
|
590 |
procedure TStString.Detab;
|
591 |
{- Detabs string. }
|
592 |
begin
|
593 |
AllocTemp(SuggestSize(FAlloc + (CharCountZ(FString, #9)*FTabSize)));
|
594 |
if Assigned(FTemp) then begin
|
595 |
DetabZ(FTemp, FString, FTabSize);
|
596 |
TempToString;
|
597 |
end;
|
598 |
ResetCursor;
|
599 |
end;
|
600 |
|
601 |
procedure TStString.Entab;
|
602 |
{- Entabs string. }
|
603 |
begin
|
604 |
AllocTemp(FAlloc);
|
605 |
if Assigned(FTemp) then begin
|
606 |
EntabZ(FTemp, FString, FTabsize);
|
607 |
TempToString;
|
608 |
end;
|
609 |
ResetCursor;
|
610 |
end;
|
611 |
|
612 |
function TStString.ExtractAscii(N : Cardinal) : AnsiString;
|
613 |
{- Extracts the N'th word in string. }
|
614 |
var
|
615 |
OldCursor : PAnsiChar;
|
616 |
Position : Cardinal;
|
617 |
begin
|
618 |
Result := '';
|
619 |
OldCursor := FCursor;
|
620 |
if AsciiPositionZ(N, FString, FDelimiters, FQuote, Position) then
|
621 |
FCursor := FString + Position
|
622 |
else Exit;
|
623 |
Result := GetAsciiAtCursor;
|
624 |
FCursor := OldCursor;
|
625 |
end;
|
626 |
|
627 |
function TStString.ExtractWord(N : Cardinal) : AnsiString;
|
628 |
{- Extracts the N'th word in string. }
|
629 |
var
|
630 |
OldCursor : PAnsiChar;
|
631 |
Position : Cardinal;
|
632 |
begin
|
633 |
Result := '';
|
634 |
OldCursor := FCursor;
|
635 |
if WordPositionZ(N, FString, FDelimiters, Position) then
|
636 |
FCursor := FString + Position
|
637 |
else Exit;
|
638 |
Result := GetWordAtCursor;
|
639 |
FCursor := OldCursor;
|
640 |
end;
|
641 |
|
642 |
procedure TStString.Filter(const Filters : PAnsiChar);
|
643 |
{- Filters characters from string. }
|
644 |
begin
|
645 |
AllocTemp(FAlloc);
|
646 |
FilterZ(FTemp, DesiredCursor, Filters);
|
647 |
TempToString;
|
648 |
ResetCursor;
|
649 |
end;
|
650 |
|
651 |
procedure TStString.FixCursor(Pos, Size : Cardinal; Delete : Boolean);
|
652 |
{- Fixes cursor position following an insertion or deletion. }
|
653 |
begin
|
654 |
if (FCursor - FString) < LongInt(Pos) then
|
655 |
Exit;
|
656 |
if Delete then begin
|
657 |
FCursor := FCursor - Size;
|
658 |
if (FCursor - FString) < LongInt(Pos) then
|
659 |
FCursor := FString + Pos;
|
660 |
end else begin
|
661 |
if (FCursor - FString) = LongInt(Pos) then
|
662 |
Exit;
|
663 |
FCursor := FCursor + Size;
|
664 |
end;
|
665 |
end;
|
666 |
|
667 |
function TStString.MakeLetterSet : LongInt;
|
668 |
{- Performs MakeLetterSetZ on the word at the Cursor. }
|
669 |
var
|
670 |
Temp : PAnsiChar;
|
671 |
begin
|
672 |
Temp := AnsiStrAlloc(Succ(SizeWordAtCursor(False)));
|
673 |
try
|
674 |
GetWordAtCursorZ(Temp);
|
675 |
Result := MakeLetterSetZ(Temp);
|
676 |
finally
|
677 |
StrDispose(Temp);
|
678 |
end;
|
679 |
end;
|
680 |
|
681 |
procedure TStString.MoveCursor(Delta : Integer);
|
682 |
{- Moves Cursor by Delta characters. }
|
683 |
begin
|
684 |
Inc(FCursor, Delta);
|
685 |
if FCursor < FString then
|
686 |
FCursor := FString;
|
687 |
if FCursor > (FString + Succ(GetLength)) then
|
688 |
CursorToEnd;
|
689 |
end;
|
690 |
|
691 |
function TStString.GetAsciiAtCursor : AnsiString;
|
692 |
{- Gets the word that the Cursor is pointing to -- returns string. }
|
693 |
var
|
694 |
I, Size : Longint;
|
695 |
begin
|
696 |
Size := SizeAsciiAtCursor(False);
|
697 |
SetLength(Result, Size);
|
698 |
for I := 0 to Pred(Size) do
|
699 |
Result[Succ(I)] := FCursor[I];
|
700 |
end;
|
701 |
|
702 |
function TStString.GetAsciiAtCursorZ(Dest : PAnsiChar) : PAnsiChar;
|
703 |
{- Gets the word that the Cursor is pointing to -- returns PChar. }
|
704 |
var
|
705 |
I, Size : Longint;
|
706 |
begin
|
707 |
Size := SizeAsciiAtCursor(False);
|
708 |
Result := Dest;
|
709 |
for I := 0 to Pred(Size) do
|
710 |
Dest[I] := FCursor[I];
|
711 |
Dest[Size] := #0;
|
712 |
end;
|
713 |
|
714 |
function TStString.GetAsPChar(Dest : PAnsiChar) : PAnsiChar;
|
715 |
{- Exports string as a null-terminated string. }
|
716 |
begin
|
717 |
Result := Dest;
|
718 |
StrCopy(Result, FString);
|
719 |
end;
|
720 |
|
721 |
function TStString.GetWordAtCursor : AnsiString;
|
722 |
{- Gets the word that the Cursor is pointing to -- returns string. }
|
723 |
var
|
724 |
I, Size : Longint;
|
725 |
begin
|
726 |
Size := SizeWordAtCursor(False);
|
727 |
SetLength(Result, Size);
|
728 |
for I := 0 to Pred(Size) do
|
729 |
Result[Succ(I)] := FCursor[I];
|
730 |
end;
|
731 |
|
732 |
function TStString.GetWordAtCursorZ(Dest : PAnsiChar) : PAnsiChar;
|
733 |
{- Gets the word that the Cursor is pointing to -- returns PChar. }
|
734 |
var
|
735 |
I, Size : Longint;
|
736 |
begin
|
737 |
Size := SizeWordAtCursor(False);
|
738 |
Result := Dest;
|
739 |
for I := 0 to Pred(Size) do
|
740 |
Dest[I] := FCursor[I];
|
741 |
Dest[Size] := #0;
|
742 |
end;
|
743 |
|
744 |
procedure TStString.Pack;
|
745 |
{- Sets string allocation to minimum size. }
|
746 |
var
|
747 |
StrLen : Cardinal;
|
748 |
begin
|
749 |
StrLen := GetLength;
|
750 |
if SuggestSize(StrLen) < FAlloc then
|
751 |
SetAllocLength(StrLen);
|
752 |
end;
|
753 |
|
754 |
procedure TStString.Pad(Size : Cardinal);
|
755 |
{- Pads string. }
|
756 |
begin
|
757 |
CheckAlloc(Size);
|
758 |
PadPrimZ(FString, Size);
|
759 |
ResetCursor;
|
760 |
end;
|
761 |
|
762 |
|
763 |
function TStString.MakeTerminator(var Terminator : PAnsiChar) : Integer; {!!.01}
|
764 |
begin
|
765 |
Result := 0;
|
766 |
case self.LineTerminator of
|
767 |
ltNone :;
|
768 |
ltCR, ltLF, ltOther :begin
|
769 |
Result := 2;
|
770 |
GetMem(Terminator, Result);
|
771 |
case LineTerminator of
|
772 |
ltCR : StrCopy(Terminator, #13);
|
773 |
ltLF : StrCopy(Terminator, #10);
|
774 |
ltOther : begin
|
775 |
Terminator[0] := FLineTermChar;
|
776 |
Terminator[1] := #0;
|
777 |
end;
|
778 |
end;
|
779 |
end;
|
780 |
ltCRLF : begin
|
781 |
Result := 3;
|
782 |
GetMem(Terminator, Result);
|
783 |
StrCopy(Terminator, #13#10);
|
784 |
end;
|
785 |
end;
|
786 |
end;
|
787 |
|
788 |
procedure TStString.InsertLineTerminatorAtCursor;
|
789 |
{- Inserts line termintor at cursor position. }
|
790 |
var
|
791 |
Pos : Cardinal;
|
792 |
Terminator : PAnsiChar;
|
793 |
TermSiz : Integer;
|
794 |
begin
|
795 |
Terminator := nil;
|
796 |
TermSiz := MakeTerminator(Terminator);
|
797 |
CheckAlloc(GetLength + 2);
|
798 |
Pos := FCursor - FString;
|
799 |
StrStInsertPrimZ(FString, Terminator, Pos);
|
800 |
FreeMem(Terminator, TermSiz);
|
801 |
end;
|
802 |
|
803 |
procedure TStString.InsertLineTerminator(Pos : Cardinal);
|
804 |
{- Inserts line terminator at given position. }
|
805 |
var
|
806 |
AdjPos : Cardinal;
|
807 |
Terminator : PAnsiChar;
|
808 |
TermSiz : Integer;
|
809 |
begin
|
810 |
Terminator := nil;
|
811 |
TermSiz := MakeTerminator(Terminator);
|
812 |
CheckAlloc(GetLength + 2);
|
813 |
AdjPos := Pos;
|
814 |
if FOneBased then Dec(AdjPos);
|
815 |
StrStInsertPrimZ(FString, Terminator, AdjPos);
|
816 |
FreeMem(Terminator, TermSiz);
|
817 |
end;
|
818 |
|
819 |
|
820 |
procedure TStString.InsertPCharAtCursor(S : PAnsiChar);
|
821 |
{- Inserts null-terminated string at cursor position. }
|
822 |
var
|
823 |
Len, Pos : Cardinal;
|
824 |
begin
|
825 |
Len := StrLen(S);
|
826 |
Pos := FCursor - FString;
|
827 |
CheckAlloc(GetLength + Len);
|
828 |
StrStInsertPrimZ(FString, S, Pos);
|
829 |
end;
|
830 |
|
831 |
procedure TStString.InsertStringAtCursor(S : AnsiString);
|
832 |
{- Inserts string at cursor position. }
|
833 |
var
|
834 |
Pos, Len : Cardinal;
|
835 |
Temp : PAnsiChar;
|
836 |
begin
|
837 |
Pos := FCursor - FString;
|
838 |
Len := System.Length(S);
|
839 |
Temp := AnsiStrAlloc(Succ(Len));
|
840 |
try
|
841 |
StrPCopy(Temp, S);
|
842 |
CheckAlloc(GetLength + Len);
|
843 |
StrStInsertPrimZ(FString, Temp, Pos);
|
844 |
finally
|
845 |
StrDispose(Temp);
|
846 |
end;
|
847 |
end;
|
848 |
|
849 |
procedure TStString.ItemsToString;
|
850 |
{- Copies items strings to string. }
|
851 |
var
|
852 |
Temp : AnsiString;
|
853 |
begin
|
854 |
Temp := FItems.Text;
|
855 |
SetAsPChar(PAnsiChar(AnsiString(Temp)));
|
856 |
end;
|
857 |
|
858 |
procedure TStString.LeftPad(Size : Cardinal);
|
859 |
{- Left pad string. }
|
860 |
begin
|
861 |
CheckAlloc(Size);
|
862 |
LeftPadPrimZ(FString, Size);
|
863 |
ResetCursor;
|
864 |
end;
|
865 |
|
866 |
procedure TStString.LeftPadCh(const C : AnsiChar; Size : Cardinal);
|
867 |
{- Left pad string with C. }
|
868 |
begin
|
869 |
CheckAlloc(Size);
|
870 |
LeftPadChPrimZ(FString, C, Size);
|
871 |
ResetCursor;
|
872 |
end;
|
873 |
|
874 |
procedure TStString.PadCh(const C : AnsiChar; Size : Cardinal);
|
875 |
{- Pad string with C. }
|
876 |
begin
|
877 |
CheckAlloc(Size);
|
878 |
PadChPrimZ(FString, C, Size);
|
879 |
ResetCursor;
|
880 |
end;
|
881 |
|
882 |
procedure TStString.ResetCursor;
|
883 |
{- Resets Cursor to beginning of string. }
|
884 |
begin
|
885 |
FCursor := FString;
|
886 |
end;
|
887 |
|
888 |
procedure TStString.Scramble(const Key : AnsiString);
|
889 |
{- Encrypts / Decrypts string. }
|
890 |
var
|
891 |
Temp : PAnsiChar;
|
892 |
begin
|
893 |
Temp := AnsiStrAlloc(Succ(System.Length(Key)));
|
894 |
try
|
895 |
StrPCopy(Temp, Key);
|
896 |
ScramblePrimZ(FString, Temp);
|
897 |
finally
|
898 |
StrDispose(Temp);
|
899 |
end;
|
900 |
end;
|
901 |
|
902 |
procedure TStString.SetAsPChar(S : PAnsiChar);
|
903 |
{- Sets string to PChar. }
|
904 |
begin
|
905 |
CheckAlloc(StrLen(S));
|
906 |
StrCopy(FString, S);
|
907 |
ResetCursor;
|
908 |
end;
|
909 |
|
910 |
function TStString.SizeAsciiAtCursor(InclTrailers : Boolean) : Cardinal;
|
911 |
{- Get the size of the word that the Cursor on (follows ASCII rules). }
|
912 |
var
|
913 |
TempPtr : PAnsiChar;
|
914 |
Ch : AnsiChar;
|
915 |
InQuote : Boolean;
|
916 |
begin
|
917 |
InQuote := False;
|
918 |
TempPtr := FCursor;
|
919 |
Ch := TempPtr^;
|
920 |
while (Ch <> #0) and ((InQuote) or (not CharExistsZ(FDelimiters, Ch))) do begin
|
921 |
if Ch = FQuote then
|
922 |
InQuote := not InQuote;
|
923 |
Inc(TempPtr);
|
924 |
Ch := TempPtr^;
|
925 |
end;
|
926 |
if InclTrailers then begin
|
927 |
while (Ch <> #0) and CharExistsZ(FDelimiters, Ch) do begin
|
928 |
Inc(TempPtr);
|
929 |
Ch := TempPtr^;
|
930 |
end;
|
931 |
end;
|
932 |
Result := TempPtr - FCursor;
|
933 |
end;
|
934 |
|
935 |
function TStString.SizeWordAtCursor(InclTrailers : Boolean) : Cardinal;
|
936 |
{- Get the size of the word that the Cursor is pointing to. }
|
937 |
var
|
938 |
TempPtr : PAnsiChar;
|
939 |
Ch : AnsiChar;
|
940 |
begin
|
941 |
TempPtr := FCursor;
|
942 |
Ch := TempPtr^;
|
943 |
while (Ch <> #0) and (not CharExistsZ(FDelimiters, Ch)) do begin
|
944 |
Inc(TempPtr);
|
945 |
Ch := TempPtr^;
|
946 |
end;
|
947 |
if InclTrailers then begin
|
948 |
while (Ch <> #0) and CharExistsZ(FDelimiters, Ch) do begin
|
949 |
Inc(TempPtr);
|
950 |
Ch := TempPtr^;
|
951 |
end;
|
952 |
end;
|
953 |
Result := TempPtr - FCursor;
|
954 |
end;
|
955 |
|
956 |
procedure TStString.StrChDelete(Pos : Cardinal);
|
957 |
{- Delete character at Pos. }
|
958 |
var
|
959 |
AdjPos : Cardinal;
|
960 |
begin
|
961 |
AdjPos := Pos;
|
962 |
if FOneBased then Dec(AdjPos);
|
963 |
StrChDeletePrimZ(FString, AdjPos);
|
964 |
FixCursor(AdjPos, 1, True);
|
965 |
end;
|
966 |
|
967 |
procedure TStString.StrChInsert(const C : AnsiChar; Pos : Cardinal);
|
968 |
{- Insert character at Pos. }
|
969 |
var
|
970 |
AdjPos : Cardinal;
|
971 |
begin
|
972 |
AdjPos := Pos;
|
973 |
CheckAlloc(Succ(GetLength));
|
974 |
if FOneBased then Dec(AdjPos);
|
975 |
StrChInsertPrimZ(FString, C, AdjPos);
|
976 |
FixCursor(AdjPos, 1, False);
|
977 |
end;
|
978 |
|
979 |
function TStString.StrChPos(const C : AnsiChar; var Pos : Cardinal) : Boolean;
|
980 |
{- Search string for character. }
|
981 |
var
|
982 |
I : Cardinal;
|
983 |
begin
|
984 |
Result := False;
|
985 |
ClearItems;
|
986 |
for I := 1 to FRepeatValue do begin
|
987 |
if StrChPosZ(DesiredCursor, C, Pos) then begin
|
988 |
Result := True;
|
989 |
Pos := GetRelativePos(Pos);
|
990 |
UpdateCursor(Pos);
|
991 |
if FOneBased then Inc(Pos);
|
992 |
AddIntToList(Pos);
|
993 |
Inc(FCursor);
|
994 |
end else Break;
|
995 |
end;
|
996 |
if Result then Dec(FCursor);
|
997 |
end;
|
998 |
|
999 |
|
1000 |
procedure TStString.StripLineTerminators;
|
1001 |
{- Strip all line terminators from string, replacing them with a space. }
|
1002 |
var
|
1003 |
Terminator : PAnsiChar;
|
1004 |
TermSiz : Integer;
|
1005 |
begin
|
1006 |
Terminator := nil;
|
1007 |
if self.FLineTerminator = ltCRLF then begin
|
1008 |
Filter(#13);
|
1009 |
Substitute(#10, ' ');
|
1010 |
end
|
1011 |
else begin
|
1012 |
TermSiz := MakeTerminator(Terminator);
|
1013 |
Substitute(Terminator, ' ');
|
1014 |
FreeMem(Terminator, TermSiz);
|
1015 |
end;
|
1016 |
ResetCursor;
|
1017 |
end;
|
1018 |
|
1019 |
|
1020 |
procedure TStString.StrStDelete(const Pos, Length : Cardinal);
|
1021 |
{- Delete substring from string at Pos. }
|
1022 |
var
|
1023 |
AdjPos : Cardinal;
|
1024 |
begin
|
1025 |
AdjPos := Pos;
|
1026 |
if FOneBased then Dec(AdjPos);
|
1027 |
StrStDeletePrimZ(FString, AdjPos, Length);
|
1028 |
FixCursor(AdjPos, Length, True);
|
1029 |
end;
|
1030 |
|
1031 |
procedure TStString.StrStInsert(const S : AnsiString; Pos : Cardinal);
|
1032 |
{- Insert string at Pos. }
|
1033 |
var
|
1034 |
AdjPos, Len : Cardinal;
|
1035 |
Temp : PAnsiChar;
|
1036 |
begin
|
1037 |
AdjPos := Pos;
|
1038 |
Len := System.Length(S);
|
1039 |
Temp := AnsiStrAlloc(Succ(Len));
|
1040 |
try
|
1041 |
StrPCopy(Temp, S);
|
1042 |
if FOneBased then Dec(AdjPos);
|
1043 |
CheckAlloc(GetLength + Len);
|
1044 |
StrStInsertPrimZ(FString, Temp, AdjPos);
|
1045 |
finally
|
1046 |
StrDispose(Temp);
|
1047 |
end;
|
1048 |
FixCursor(AdjPos, Len, False);
|
1049 |
end;
|
1050 |
|
1051 |
function TStString.StrStPos(const S : AnsiString; var Pos : Cardinal) : Boolean;
|
1052 |
{- Search for substring in string. }
|
1053 |
var
|
1054 |
I : Cardinal;
|
1055 |
Temp : PAnsiChar;
|
1056 |
begin
|
1057 |
Result := False;
|
1058 |
ClearItems;
|
1059 |
Temp := AnsiStrAlloc(Succ(System.Length(S)));
|
1060 |
try
|
1061 |
StrPCopy(Temp, S);
|
1062 |
for I := 1 to FRepeatValue do begin
|
1063 |
if StrStPosZ(DesiredCursor, Temp, Pos) then begin
|
1064 |
Result := True;
|
1065 |
Pos := GetRelativePos(Pos);
|
1066 |
UpdateCursor(Pos);
|
1067 |
if FOneBased then inc(Pos);
|
1068 |
AddIntToList(Pos);
|
1069 |
Inc(FCursor);
|
1070 |
end else Break;
|
1071 |
end;
|
1072 |
if Result then Dec(FCursor);
|
1073 |
finally
|
1074 |
StrDispose(Temp);
|
1075 |
end;
|
1076 |
end;
|
1077 |
|
1078 |
procedure TStString.Substitute(FromStr, ToStr : PAnsiChar);
|
1079 |
{- Substitute characters in string. }
|
1080 |
var
|
1081 |
CursorDelta : Cardinal;
|
1082 |
begin
|
1083 |
AllocTemp(FAlloc);
|
1084 |
CursorDelta := FCursor - FString;
|
1085 |
if Assigned(FTemp) then begin
|
1086 |
SubstituteZ(FTemp, FString, FromStr, ToStr);
|
1087 |
TempToString;
|
1088 |
end;
|
1089 |
FCursor := FString + CursorDelta;
|
1090 |
end;
|
1091 |
|
1092 |
procedure TStString.Trim;
|
1093 |
{- Trim string. }
|
1094 |
begin
|
1095 |
TrimPrimZ(FString);
|
1096 |
ResetCursor;
|
1097 |
end;
|
1098 |
|
1099 |
procedure TStString.TrimLead;
|
1100 |
{- Trim leading whitespace from string. }
|
1101 |
begin
|
1102 |
TrimLeadPrimZ(FString);
|
1103 |
ResetCursor;
|
1104 |
end;
|
1105 |
|
1106 |
procedure TStString.TrimSpaces;
|
1107 |
{- Trim spaces from string. }
|
1108 |
begin
|
1109 |
TrimSpacesPrimZ(FString);
|
1110 |
ResetCursor;
|
1111 |
end;
|
1112 |
|
1113 |
procedure TStString.TrimTrail;
|
1114 |
{- Trim trailing whitespace from string. }
|
1115 |
begin
|
1116 |
TrimTrailPrimZ(FString);
|
1117 |
ResetCursor;
|
1118 |
end;
|
1119 |
|
1120 |
function TStString.WordPosition(N : Cardinal; var Pos : Cardinal) : Boolean;
|
1121 |
{- Return the position of the N'th word. }
|
1122 |
var
|
1123 |
I, Temp, Num : Cardinal;
|
1124 |
begin
|
1125 |
Result := False;
|
1126 |
Num := N;
|
1127 |
ClearItems;
|
1128 |
for I := 1 to FRepeatValue do begin
|
1129 |
if WordPositionZ(Num, DesiredCursor, FDelimiters, Temp) then begin
|
1130 |
if Result = False then Inc(Num);
|
1131 |
Pos := GetRelativePos(Temp);
|
1132 |
Result := True;
|
1133 |
UpdateCursor(Pos);
|
1134 |
if FOneBased then Inc(Pos);
|
1135 |
AddIntToList(Pos);
|
1136 |
end else
|
1137 |
Break;
|
1138 |
end;
|
1139 |
if FResetRepeat then FRepeatValue := DefRepeatValue;
|
1140 |
end;
|
1141 |
|
1142 |
|
1143 |
procedure TStString.WrapToItems;
|
1144 |
{- Copy string to items with word wrap. }
|
1145 |
var
|
1146 |
I, J : Cardinal;
|
1147 |
Anchor, Cur, EndTemp : PAnsiChar;
|
1148 |
InWord, EndFound : Boolean;
|
1149 |
|
1150 |
Terminator, TermPlusSpace : PAnsiChar;
|
1151 |
TermSiz : Integer;
|
1152 |
begin
|
1153 |
Terminator := nil;
|
1154 |
TermSiz := MakeTerminator(Terminator);
|
1155 |
GetMem(TermPlusSpace, TermSiz + 1);
|
1156 |
StrCopy(TermPlusSpace, Terminator);
|
1157 |
StrCat(TermPlusSpace, ' ');
|
1158 |
|
1159 |
if GetLength > FWrap then begin
|
1160 |
EndFound := False;
|
1161 |
AllocTemp(SuggestSize(GetLength + (GetLength div FWrap * 2)));
|
1162 |
FTemp^ := #0;
|
1163 |
Anchor := FString;
|
1164 |
Cur := FString;
|
1165 |
repeat
|
1166 |
I := 0;
|
1167 |
J := 0;
|
1168 |
InWord := False;
|
1169 |
while (Cur^ <> #0) and (I < Succ(FWrap)) do begin
|
1170 |
// if CharExistsZ(' '#13#10, Cur^) then begin
|
1171 |
if CharExistsZ(TermPlusSpace, Cur^) then begin
|
1172 |
if InWord then begin
|
1173 |
InWord := False;
|
1174 |
J := I;
|
1175 |
end;
|
1176 |
if Cur^ <> ' ' then Break;
|
1177 |
end else begin
|
1178 |
InWord := True;
|
1179 |
end;
|
1180 |
Inc(I);
|
1181 |
Inc(Cur);
|
1182 |
end;
|
1183 |
if Cur^ = #0 then begin
|
1184 |
EndFound := True;
|
1185 |
J := I;
|
1186 |
end;
|
1187 |
EndTemp := StrEnd(FTemp);
|
1188 |
if InWord and (J = 0) then
|
1189 |
J := FWrap;
|
1190 |
StrLCopy(EndTemp, Anchor, J);
|
1191 |
if not EndFound then begin
|
1192 |
// StrCat(FTemp, #13#10);
|
1193 |
StrCat(FTemp, Terminator);
|
1194 |
Anchor := Anchor + J;
|
1195 |
while Anchor^ = ' ' do
|
1196 |
Inc(Anchor);
|
1197 |
if FLineTerminator = ltCRLF then begin
|
1198 |
if Anchor^ = #13 then Inc(Anchor);
|
1199 |
if Anchor^ = #10 then Inc(Anchor);
|
1200 |
end else begin
|
1201 |
if Anchor^ = Terminator[0] then Inc(Anchor);
|
1202 |
end;
|
1203 |
Cur := Anchor;
|
1204 |
end;
|
1205 |
until EndFound;
|
1206 |
FItems.Text := FTemp;
|
1207 |
StrDispose(FTemp);
|
1208 |
end else begin
|
1209 |
StringToItems;
|
1210 |
end;
|
1211 |
FreeMem(Terminator, TermSiz);
|
1212 |
FreeMem(TermPlusSpace, TermSiz + 1);
|
1213 |
end;
|
1214 |
|
1215 |
|
1216 |
function TStString.DesiredCursor : PAnsiChar;
|
1217 |
{- Returns FString or FCursor. }
|
1218 |
begin
|
1219 |
if FEnableCursor then
|
1220 |
Result := FCursor
|
1221 |
else
|
1222 |
Result := FString;
|
1223 |
end;
|
1224 |
|
1225 |
function TStString.Get(Index : Cardinal) : AnsiChar;
|
1226 |
{- Get character from position Index within string. }
|
1227 |
begin
|
1228 |
if FOneBased then begin
|
1229 |
if (Index = 0) or (Index > GetLength) then
|
1230 |
RaiseStError(EStStringError, stscOutOfBounds);
|
1231 |
Result := FString[Index - 1]
|
1232 |
end else begin
|
1233 |
if Index > (GetLength-1) then
|
1234 |
RaiseStError(EStStringError, stscOutOfBounds);
|
1235 |
Result := FString[Index];
|
1236 |
end;
|
1237 |
end;
|
1238 |
|
1239 |
function TStString.GetAsciiCount : Cardinal;
|
1240 |
{- Count words following ASCII rules. }
|
1241 |
begin
|
1242 |
Result := AsciiCountZ(FString, FDelimiters, FQuote);
|
1243 |
end;
|
1244 |
|
1245 |
function TStString.GetAsShortStr : ShortString;
|
1246 |
{- Provide short string output. }
|
1247 |
begin
|
1248 |
Result := StrPas(FString);
|
1249 |
end;
|
1250 |
|
1251 |
function TStString.GetCursorPos : Cardinal;
|
1252 |
{- Return the position of the Cursor relative to the beginning of the string. }
|
1253 |
begin
|
1254 |
Result := FCursor - FString;
|
1255 |
if FOneBased then Inc(Result);
|
1256 |
end;
|
1257 |
|
1258 |
function TStString.GetDelimiters : AnsiString;
|
1259 |
{- Return string with current delimiters. }
|
1260 |
begin
|
1261 |
Result := StrPas(FDelimiters);
|
1262 |
end;
|
1263 |
|
1264 |
function TStString.GetLength : Cardinal;
|
1265 |
{- Return the length of the string. }
|
1266 |
begin
|
1267 |
if Assigned(FString) then
|
1268 |
Result := StrLen(FString)
|
1269 |
else
|
1270 |
Result := 0;
|
1271 |
end;
|
1272 |
|
1273 |
function TStString.GetRelativePos(Pos : Cardinal) : Cardinal;
|
1274 |
{- Return position relative to FString. }
|
1275 |
begin
|
1276 |
if FEnableCursor then
|
1277 |
Result := Pos + FCursor - FString
|
1278 |
else
|
1279 |
Result := Pos;
|
1280 |
end;
|
1281 |
|
1282 |
function TStString.GetSoundex : AnsiString;
|
1283 |
{- Return Soundex for word at Cursor. }
|
1284 |
var
|
1285 |
I : Integer;
|
1286 |
Temp, Dest : PAnsiChar;
|
1287 |
begin
|
1288 |
ClearItems;
|
1289 |
Dest := AnsiStrAlloc(5);
|
1290 |
try
|
1291 |
for I := 1 to FRepeatValue do begin
|
1292 |
if FCursor^ = #0 then Exit;
|
1293 |
Temp := AnsiStrAlloc(Succ(SizeWordAtCursor(False)));
|
1294 |
try
|
1295 |
GetWordAtCursorZ(Temp);
|
1296 |
Result := StrPas(SoundexZ(Dest, Temp));
|
1297 |
FItems.Add(Result);
|
1298 |
finally
|
1299 |
StrDispose(Temp);
|
1300 |
end;
|
1301 |
if FRepeatValue > 1 then CursorNextWordPrim;
|
1302 |
end;
|
1303 |
finally
|
1304 |
StrDispose(Dest);
|
1305 |
end;
|
1306 |
end;
|
1307 |
|
1308 |
function TStString.GetWordCount : Cardinal;
|
1309 |
{- Count words in string. }
|
1310 |
begin
|
1311 |
Result := WordCountZ(FString, FDelimiters);
|
1312 |
end;
|
1313 |
|
1314 |
procedure TStString.Put(Index : Cardinal; Item : AnsiChar);
|
1315 |
{- Put character at position Index within string. }
|
1316 |
begin
|
1317 |
if FOneBased then begin
|
1318 |
if (Index = 0) or (Index > GetLength) then
|
1319 |
RaiseStError(EStStringError, stscOutOfBounds);
|
1320 |
FString[Index - 1] := Item;
|
1321 |
end else begin
|
1322 |
if Index > (GetLength-1) then
|
1323 |
RaiseStError(EStStringError, stscOutOfBounds);
|
1324 |
FString[Index] := Item;
|
1325 |
end;
|
1326 |
end;
|
1327 |
|
1328 |
procedure TStString.SetAllocLength(Value : Cardinal);
|
1329 |
{- Sets allocated length for string - including the terminating null. }
|
1330 |
begin
|
1331 |
if Value <> FAlloc then begin
|
1332 |
AllocTemp(SuggestSize(Value));
|
1333 |
if Assigned(FTemp) then begin
|
1334 |
if Assigned(FString) then begin
|
1335 |
StrLCopy(FTemp, FString, Value);
|
1336 |
end;
|
1337 |
TempToString;
|
1338 |
end;
|
1339 |
end;
|
1340 |
end;
|
1341 |
|
1342 |
procedure TStString.SetAsShortStr(Value : ShortString);
|
1343 |
{- Copy short string into string object. }
|
1344 |
begin
|
1345 |
CheckAlloc(Byte(Value[0]));
|
1346 |
StrPCopy(FString, Value);
|
1347 |
ResetCursor;
|
1348 |
end;
|
1349 |
|
1350 |
procedure TStString.SetCursorPos(Value : Cardinal);
|
1351 |
{- Sets the position of the cursor. }
|
1352 |
begin
|
1353 |
FCursor := FString + Value;
|
1354 |
if FOneBased then Dec(FCursor);
|
1355 |
end;
|
1356 |
|
1357 |
procedure TStString.SetDelimiters(Value : AnsiString);
|
1358 |
{- Set the delimiters. }
|
1359 |
begin
|
1360 |
StrDispose(FDelimiters);
|
1361 |
FDelimiters := AnsiStrAlloc(Succ(System.Length(Value)));
|
1362 |
if Assigned(FDelimiters) then
|
1363 |
StrPCopy(FDelimiters, Value);
|
1364 |
end;
|
1365 |
|
1366 |
procedure TStString.SetItems(Value: TStringList);
|
1367 |
{- Sets Items. }
|
1368 |
begin
|
1369 |
FItems.Assign(Value);
|
1370 |
end;
|
1371 |
|
1372 |
procedure TStString.StringToItems;
|
1373 |
{- Copies string into items -- respects line terminators. }
|
1374 |
begin
|
1375 |
FItems.Text := FString;
|
1376 |
end;
|
1377 |
|
1378 |
function TStString.SuggestSize(Size : Cardinal) : Cardinal;
|
1379 |
{- Internal method -- returns recommended size for allocation. }
|
1380 |
var
|
1381 |
AdjSize, Delta : Cardinal;
|
1382 |
begin
|
1383 |
AdjSize := Succ(Size);
|
1384 |
Delta := AdjSize mod DefAllocSize;
|
1385 |
Result := AdjSize - Delta + DefAllocSize;
|
1386 |
end;
|
1387 |
|
1388 |
procedure TStString.TempToString;
|
1389 |
{- Internal method -- copys temp to string. }
|
1390 |
begin
|
1391 |
FAlloc := FTempAlloc;
|
1392 |
FCursor := (FCursor - FString) + FTemp;
|
1393 |
StrDispose(FString);
|
1394 |
FString := FTemp;
|
1395 |
FTemp := nil;
|
1396 |
end;
|
1397 |
|
1398 |
procedure TStString.UpdateCursor(Pos : Cardinal);
|
1399 |
{- Internal method -- updates cursor position if necessary. }
|
1400 |
begin
|
1401 |
if EnableCursor then
|
1402 |
FCursor := FString + Pos;
|
1403 |
end;
|
1404 |
|
1405 |
function TStString.GetAsLongStr : AnsiString;
|
1406 |
{- Provide output as long string. }
|
1407 |
begin
|
1408 |
Result := FString;
|
1409 |
end;
|
1410 |
|
1411 |
procedure TStString.SetAsLongStr(Value : AnsiString);
|
1412 |
{- Copy long string into string object. }
|
1413 |
begin
|
1414 |
CheckAlloc(System.Length(Value));
|
1415 |
StrCopy(FString, PAnsiChar(Value));
|
1416 |
ResetCursor;
|
1417 |
end;
|
1418 |
|
1419 |
function TStString.GetAsVariant : Variant;
|
1420 |
{- Provide output as variant. }
|
1421 |
begin
|
1422 |
Result := StrPas(FString);
|
1423 |
end;
|
1424 |
|
1425 |
procedure TStString.SetAsVariant(Value : Variant);
|
1426 |
{- Copy variant into string object. }
|
1427 |
var
|
1428 |
Temp : AnsiString;
|
1429 |
begin
|
1430 |
Temp := Value;
|
1431 |
CheckAlloc(System.Length(Temp));
|
1432 |
StrCopy(FString, PAnsiChar(Temp));
|
1433 |
ResetCursor;
|
1434 |
end;
|
1435 |
|
1436 |
|
1437 |
procedure TStString.SetLineTerm(const Value: TStLineTerminator);
|
1438 |
begin
|
1439 |
FLineTerminator := Value;
|
1440 |
end;
|
1441 |
|
1442 |
procedure TStString.SetLineTermChar(const Value: AnsiChar);
|
1443 |
begin
|
1444 |
FLineTermChar := Value;
|
1445 |
end;
|
1446 |
|
1447 |
|
1448 |
end.
|