-
Notifications
You must be signed in to change notification settings - Fork 4
/
uTextProc.pas
131 lines (110 loc) · 3.15 KB
/
uTextProc.pas
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
unit uTextProc;
interface
uses
System.SysUtils, System.Generics.Collections, uNamedList;
type
TStrArray = array of String;
{Çíà÷åíèÿ äëÿ áûñòðîãî äîñòóïà}
TStrAndPos = record
Text: string;
StartPos, EndPos: Integer;
end;
TValueStack = class(TNamedList<Double>);
function PosFromRight(const ASubStr, AStr: string; AOffset: Integer = 0): Integer;
function PosFromLeft(const ASubStr, AStr: string; AOffset: Integer = 1): Integer;
function ListOfPos(const ASubstr, AStr: String): TList<Integer>;
function ExplodeBy(s, s1: string): tStrArray; // Ðàçáèâàåò êàê â ïõï
implementation
// Ðàçáèâàåò êàê â ïõï
function Explodeby(s, s1: string): tStrArray;
var
i, ls1, ls, tempPos: integer;
st, newC: string;
res: tStrArray;
Begin
ls1:=length(s1);
ls:=length(s);
tempPos := 1;
for i := 1 to ls do
begin
st := copy(s, i, ls1);
if st = s1 then
begin
newC := copy(s, tempPos, i - tempPos);
setlength(res, length(res) + 1);
res[high(res)] := newC;
tempPos := i + ls1;
end;
end;
if tempPos <= ls then
begin
setlength(res, length(res) + 1);
res[ high(res)] := copy(s, tempPos, ls - tempPos + 1);
end;
result := res;
End;
// Äàåò ñïèñîê ïîèçèöèé âõîæäåíèÿ ïîäñòðîêè
function ListOfPos(const ASubstr, AStr: String): TList<Integer>;
var
vPos: Integer;
vRes: TList<Integer>;
// vN, vArr: Integer;
begin
vPos := 0;
vRes := TList<Integer>.Create;
repeat
vPos := Pos(ASubstr, AStr, vPos + 1);
if vPos > 0 then
vRes.Add(vPos);
until vPos <= 0;
Result := vRes;
end;
// Âûäàåò òå æå çíà÷åíèÿ, ÷òî è Pos, íî èùåò îò îôôñåòà âêëþ÷èòåëüíî âëåâî
// Ïåðâûé ñèìâîë òàê æå èìååò çíà÷åíè 1, à ïîñëåäíèé Length(s);
// Åñëè îôôñåò áîëüøå äëèíû, âñå íîðì, ò.ê. ñòðèíã êàê-òî êîíòðîëëèðóåò âñ¸
function PosFromRight(const ASubStr, AStr: string; AOffset: Integer = 0): Integer;
var
vStrLen, vSubStrLen: Integer;
vStrI, vSubStrI: Integer; // Ñ÷åò÷èêè
begin
vStrLen := Length(AStr);
vSubStrLen := Length(ASubStr);
if AOffset = 0 then
AOffset := vStrLen;
for vStrI := AOffset downto vSubStrLen do
begin
for vSubStrI := vSubStrLen downto 1 do
begin
// Åñëè íå ñîâïàäàåò, òî ïðîïóñêàåì èòåðàöèþ
if (AStr[vStrI + vSubStrI - vSubStrLen] <> ASubStr[vSubStrI]) then
Break;
// Åñëè íîâîå çíà÷åíèå è âñ¸ îê, òî âûõîäèì ñî çíà÷åíèåì ïîçèöèè
if vSubStrI = 1 then
Exit(vStrI - vSubStrLen + 1);
end;
end;
Result := 0;
end;
// Ðàáîòàåò òàê æå êàê Pos, íî â ñëó÷àå,åñëè óêàçàí îôôñåò 0, âûäàåò ðåçóëüòàò
function PosFromLeft(const ASubStr, AStr: string; AOffset: Integer = 1): Integer;
var
vStrLen, vSubStrLen: Integer;
vStrI, vSubStrI: Integer; // Ñ÷åò÷èêè
begin
vStrLen := Length(AStr);
vSubStrLen := Length(ASubStr);
for vStrI := AOffset to vStrLen - vSubStrLen do
begin
for vSubStrI := 1 to vSubStrLen do
begin
// Åñëè íå ñîâïàäàåò, òî ïðîïóñêàåì èòåðàöèþ
if (AStr[vStrI + vSubStrI - vSubStrLen] <> ASubStr[vSubStrI]) then
Break;
// Åñëè íîâîå çíà÷åíèå è âñ¸ îê, òî âûõîäèì ñî çíà÷åíèåì ïîçèöèè
if vSubStrI = 1 then
Exit(vStrI - vSubStrLen + 1);
end;
end;
Result := 0;
end;
end.