1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . IO ;
4
+ using System . Runtime . Serialization ;
5
+ using System . Runtime . Serialization . Formatters . Binary ;
6
+
7
+ namespace Microsoft . Automata
8
+ {
9
+ [ Serializable ]
10
+ public class Regex
11
+ {
12
+ private static readonly CharSetSolver solver ;
13
+ private static readonly RegexToAutomatonConverter < BDD > converter ;
14
+ static Regex ( )
15
+ {
16
+ solver = new CharSetSolver ( ) ;
17
+ converter = new RegexToAutomatonConverter < BDD > ( solver ) ;
18
+ }
19
+
20
+ private IMatcher matcher ;
21
+
22
+ public Regex ( string pattern ) : this ( pattern , RegexOptions . None ) { }
23
+
24
+ public Regex ( string pattern , RegexOptions options )
25
+ {
26
+ var root = converter . ConvertToSymbolicRegex ( pattern , options , keepAnchors : true ) ;
27
+ var partition = root . ComputeMinterms ( ) ;
28
+ if ( partition . Length > 64 )
29
+ {
30
+ //more than 64 bits needed to represent a set
31
+ matcher = new SymbolicRegexBV ( root , solver , converter . srBuilder , partition ) ;
32
+ }
33
+ else
34
+ {
35
+ //enough to use 64 bits
36
+ matcher = new SymbolicRegexUInt64 ( root , solver , converter . srBuilder , partition ) ;
37
+ }
38
+ }
39
+
40
+ /// <summary>
41
+ /// Returns true iff the input string matches.
42
+ /// <param name="input">given iput string</param>
43
+ /// <param name="startat">start position in the input</param>
44
+ /// <param name="endat">end position in the input, -1 means that the value is unspecified and taken to be input.Length-1</param>
45
+ /// </summary>
46
+ public bool IsMatch ( string input , int startat = 0 , int endat = - 1 )
47
+ => matcher . IsMatch ( input , startat , endat ) ;
48
+
49
+ /// <summary>
50
+ /// Returns all matches as pairs (startindex, length) in the input string.
51
+ /// </summary>
52
+ /// <param name="input">given iput string</param>
53
+ /// <param name="limit">as soon as this many matches have been found the search terminates, 0 or negative value means that there is no bound, default is 0</param>
54
+ /// <param name="startat">start position in the input, default is 0</param>
55
+ /// <param name="endat">end position in the input, -1 means that the value is unspecified and taken to be input.Length-1</param>
56
+ public List < Match > Matches ( string input , int limit = 0 , int startat = 0 , int endat = - 1 )
57
+ => matcher . Matches ( input , limit , startat , endat ) ;
58
+
59
+ /// <summary>
60
+ /// Serialize this symbolic regex matcher to the given file.
61
+ /// If formatter is null then an instance of
62
+ /// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter is used.
63
+ /// </summary>
64
+ /// <param name="file">file where the serialization is stored</param>
65
+ /// <param name="formatter">given formatter</param>
66
+ public void Serialize ( string file , IFormatter formatter = null )
67
+ {
68
+ var stream = new FileStream ( file , FileMode . Create , FileAccess . Write , FileShare . None ) ;
69
+ Serialize ( stream , formatter ) ;
70
+ stream . Close ( ) ;
71
+ }
72
+
73
+ /// <summary>
74
+ /// Serialize this symbolic regex matcher to the given file.
75
+ /// If formatter is null then an instance of
76
+ /// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter is used.
77
+ /// </summary>
78
+ /// <param name="stream">stream where the serialization is stored</param>
79
+ /// <param name="formatter">given formatter</param>
80
+ public void Serialize ( Stream stream , IFormatter formatter = null )
81
+ {
82
+ if ( formatter == null )
83
+ formatter = new BinaryFormatter ( ) ;
84
+ formatter . Serialize ( stream , this ) ;
85
+ }
86
+
87
+ /// <summary>
88
+ /// Deserialize the matcher of a symblic regex from the given file using the given formatter.
89
+ /// If formatter is null then an instance of
90
+ /// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter is used.
91
+ /// </summary>
92
+ /// <param name="file">source file of the serialized matcher</param>
93
+ /// <param name="formatter">given formatter</param>
94
+ /// <returns></returns>
95
+ public static Regex Deserialize ( string file , IFormatter formatter = null )
96
+ {
97
+ Stream stream = new FileStream ( file , FileMode . Open , FileAccess . Read , FileShare . None ) ;
98
+ Regex matcher = Deserialize ( stream , formatter ) ;
99
+ stream . Close ( ) ;
100
+ return matcher ;
101
+ }
102
+
103
+ /// <summary>
104
+ /// Deserialize the matcher of a symblic regex from the given stream using the given formatter.
105
+ /// If formatter is null then an instance of
106
+ /// System.Runtime.Serialization.Formatters.Binary.BinaryFormatter is used.
107
+ /// </summary>
108
+ /// <param name="stream">source stream of the serialized matcher</param>
109
+ /// <param name="formatter">given formatter</param>
110
+ /// <returns></returns>
111
+ public static Regex Deserialize ( Stream stream , IFormatter formatter = null )
112
+ {
113
+ if ( formatter == null )
114
+ formatter = new BinaryFormatter ( ) ;
115
+ Regex matcher = ( Regex ) formatter . Deserialize ( stream ) ;
116
+ return matcher ;
117
+ }
118
+ }
119
+ }
0 commit comments