1 #Region "Microsoft.VisualBasic::20ea60c9b109c4aabcfa50322fa1bdb0, Microsoft.VisualBasic.Core\Language\Language\UnixBash\Shell\Text\Grep.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module TextAPI
35     
36     '         Properties: [in], f, grep, i, inv
37     '                     iv, n, nv, v
38     
39     '     Enum GrepOptions
40     
41     
42     '  
43     
44     
45     
46     '     Class GrepRegx
47     
48     '         Constructor: (+4 OverloadsSub New
49     '         Function: __grep, __out, Clone, ToString
50     '         Operators: -, *, <<, <=, >=
51     
52     
53     ' /********************************************************************************/
54
55 #End Region
56
57 Imports System.Text.RegularExpressions
58 Imports Microsoft.VisualBasic.Language.UnixBash.FileSystem
59 Imports Microsoft.VisualBasic.Linq
60 Imports Microsoft.VisualBasic.Serialization.JSON
61
62 Namespace Language.UnixBash
63
64     Public Module TextAPI
65
66         ''' <summary>
67         ''' grep (global search regular expression(RE) and print out the line
68         ''' </summary>
69         ''' <returns></returns>
70         Public ReadOnly Property grep As New GrepRegx
71
72         Public ReadOnly Property a As GrepOptions = GrepOptions.a
73         Public ReadOnly Property c As GrepOptions = GrepOptions.c
74
75         Public ReadOnly Property i As GrepOptions = GrepOptions.i
76         Public ReadOnly Property n As GrepOptions = GrepOptions.n
77         Public ReadOnly Property v As GrepOptions = GrepOptions.v
78         Public ReadOnly Property [in] As GrepOptions = i Or n
79         Public ReadOnly Property inv As GrepOptions = [in] Or v
80         Public ReadOnly Property iv As GrepOptions = i Or v
81         Public ReadOnly Property nv As GrepOptions = n Or v
82
83         ''' <summary>
84         ''' Text source of the grep operation is a file.
85         ''' </summary>
86         ''' <returns></returns>
87         Public ReadOnly Property f As Boolean = True
88     End Module
89
90     Public Enum GrepOptions As Integer
91         null = 0
92         ''' <summary>
93         ''' 将 binary 文件以 text 文件的方式搜寻数据
94         ''' </summary>
95         a = 2
96         ''' <summary>
97         ''' 计算找到 '搜寻字符串' 的次数
98         ''' </summary>
99         c = 4
100         ''' <summary>
101         ''' 忽略大小写的不同,所以大小写视为相同
102         ''' </summary>
103         i = 8
104         ''' <summary>
105         ''' 顺便输出行号
106         ''' </summary>
107         n = 16
108         ''' <summary>
109         ''' 反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!
110         ''' </summary>
111         v = 32
112     End Enum
113
114     ''' <summary>
115     ''' grep -acinv * "pattern" &lt;= "filename or text" |
116     ''' grep -acinv * "pattern" &lt;&lt; "filename"
117     ''' </summary>
118     Public Class GrepRegx : Implements ICloneable
119
120         Dim __opt As GrepOptions
121         Dim __regx As String
122         Dim __isFile As Boolean = False
123
124         Sub New()
125         End Sub
126
127         Sub New(g As GrepRegx, opt As GrepOptions)
128             __regx = g.__regx
129             __opt = opt
130             __isFile = g.__isFile
131         End Sub
132
133         Sub New(g As GrepRegx, pattern As String)
134             __regx = pattern
135             __opt = g.__opt
136             __isFile = g.__isFile
137         End Sub
138
139         Sub New(g As GrepRegx, f As Boolean)
140             __regx = g.__regx
141             __opt = g.__opt
142             __isFile = f
143         End Sub
144
145         Public Overrides Function ToString() As String
146             Return New With {
147                 __opt,
148                 __regx,
149                 __isFile
150             }.GetJson
151         End Function
152
153         Public Function Clone() As Object Implements ICloneable.Clone
154             Return New GrepRegx With {
155                 .__opt = __opt,
156                 .__regx = __regx
157             }
158         End Function
159
160         Public Shared Operator -(grep As GrepRegx, opt As GrepOptions) As GrepRegx
161             Return New GrepRegx(grep, opt)
162         End Operator
163
164         Public Shared Operator *(grep As GrepRegx, pattern As StringAs GrepRegx
165             Return New GrepRegx(grep, pattern)
166         End Operator
167
168         Private Iterator Function __grep(source As IEnumerable(Of String)) As IEnumerable(Of String)
169             Dim options As RegexOptions = RegexOptions.Singleline
170             Dim reversed As Boolean = __opt.HasFlag(GrepOptions.v)
171             Dim i As Boolean = __opt.HasFlag(GrepOptions.n)
172
173             If __opt.HasFlag(GrepOptions.i) Then
174                 options = options Or RegexOptions.IgnoreCase
175             End If
176
177             For Each line As SeqValue(Of StringIn source.SeqIterator
178                 If Regex.Match(__regx, line.value, options).Success Then
179                     If Not reversed Then
180                         Yield __out(line.value, i, line.i)
181                     End If
182                 Else
183                     If reversed Then
184                         Yield __out(line.value, i, line.i)
185                     End If
186                 End If
187             Next
188         End Function
189
190         Private Shared Function __out(s As String, n As Boolean, i As IntegerAs String
191             If n Then
192                 Return $"{i}:{s}"
193             Else
194                 Return s
195             End If
196         End Function
197
198         Public Shared Operator <=(grep As GrepRegx, source As StringAs IEnumerable(Of String)
199             If grep.__isFile Then
200                 Return grep.__grep(source.IterateAllLines)
201             Else
202                 Return grep.__grep(source.LineTokens)
203             End If
204         End Operator
205
206         Public Shared Operator <<(grep As GrepRegx, file As IntegerAs IEnumerable(Of String)
207             Return grep.__grep(FileHandles.__getHandle(file).FileName.IterateAllLines)
208         End Operator
209
210         Public Shared Operator >=(grep As GrepRegx, source As StringAs IEnumerable(Of String)
211             Throw New NotSupportedException
212         End Operator
213     End Class
214 End Namespace