File: syneditregexsearch.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (142 lines) | stat: -rw-r--r-- 4,001 bytes parent folder | download | duplicates (2)
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
{-------------------------------------------------------------------------------
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: SynEditRegexSearch.pas, released 2002-07-26.

Original Code by Eduardo Mauro, Gerald Nunn and Flávio Etrusco.
All Rights Reserved.

Contributors to the SynEdit project are listed in the Contributors.txt file.

Alternatively, the contents of this file may be used under the terms of the
GNU General Public License Version 2 or later (the "GPL"), in which case
the provisions of the GPL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms
of the GPL and not to allow others to use your version of this file
under the MPL, indicate your decision by deleting the provisions above and
replace them with the notice and other provisions required by the GPL.
If you do not delete the provisions above, a recipient may use your version
of this file under either the MPL or the GPL.

$Id: syneditregexsearch.pas 56392 2017-11-13 17:33:31Z juha $

You may retrieve the latest version of this file at the SynEdit home page,
located at http://SynEdit.SourceForge.net

Known Issues:
-------------------------------------------------------------------------------}

unit SynEditRegexSearch;

{$I SynEdit.inc}

interface

uses
  Classes, RegExpr, IntegerList,
  SynEditTypes,
  SynEditMiscClasses;

type
  TSynEditRegexSearch = class(TSynEditSearchCustom)
  private
    fRegex : TRegExpr;
    fPositions: TIntegerList;
    fLengths: TIntegerList;
  protected
    function GetPattern: string; override;
    procedure SetPattern(const Value: string); override;
    procedure SetOptions(const Value: TSynSearchOptions); override;
    function GetLength(aIndex: integer): integer; override;
    function GetResult(aIndex: integer): integer; override;
    function GetResultCount: integer; override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function FindAll(const NewText: string): integer; override;
  end;

implementation

{ TSynEditRegexSearch }

constructor TSynEditRegexSearch.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fRegex := TRegExpr.Create;
  fPositions := TIntegerList.Create;
  fLengths := TIntegerList.Create;
end;

destructor TSynEditRegexSearch.Destroy;
begin
  fRegex.Free;
  fPositions.Free;
  fLengths.Free;
  inherited;
end;

function TSynEditRegexSearch.FindAll(const NewText: string): integer;

  procedure AddResult(const aPos, aLength: integer);
  begin
    fPositions.Add(aPos);
    fLengths.Add(aLength);
  end;

begin
  fPositions.Clear;
  fLengths.Clear;
  if fRegex.Exec( NewText ) then
  begin
    AddResult( fRegex.MatchPos[0], fRegex.MatchLen[0] );
    Result := 1;
    while fRegex.ExecNext do
    begin
      AddResult( fRegex.MatchPos[0], fRegex.MatchLen[0] );
      Inc( Result );
    end;
  end
  else
    Result := 0;
end;

function TSynEditRegexSearch.GetLength(aIndex: integer): integer;
begin
  Result := fLengths[aIndex];
end;

function TSynEditRegexSearch.GetPattern: string;
begin
  Result := fRegex.Expression;
end;

function TSynEditRegexSearch.GetResult(aIndex: integer): integer;
begin
  Result := fPositions[aIndex];
end;

function TSynEditRegexSearch.GetResultCount: integer;
begin
  Result := fPositions.Count;
end;

procedure TSynEditRegexSearch.SetOptions(const Value: TSynSearchOptions);
begin
  fRegex.ModifierI := not( ssoMatchCase in Value );
end;

procedure TSynEditRegexSearch.SetPattern(const Value: string);
begin
  fRegex.Expression := Value;
end;

end.