2 // System.Data.ProviderBase.regex.cs
\r
5 // Konstantin Triger <kostat@mainsoft.com>
\r
7 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
\r
11 // Permission is hereby granted, free of charge, to any person obtaining
\r
12 // a copy of this software and associated documentation files (the
\r
13 // "Software"), to deal in the Software without restriction, including
\r
14 // without limitation the rights to use, copy, modify, merge, publish,
\r
15 // distribute, sublicense, and/or sell copies of the Software, and to
\r
16 // permit persons to whom the Software is furnished to do so, subject to
\r
17 // the following conditions:
\r
19 // The above copyright notice and this permission notice shall be
\r
20 // included in all copies or substantial portions of the Software.
\r
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
32 using System.Data.Common;
\r
33 using System.Data.OleDb;
\r
35 namespace System.Data.ProviderBase {
\r
36 public abstract class SimpleRegex {
\r
37 abstract protected internal SimpleMatch Match(string input, int beginning, int length);
\r
38 protected internal SimpleMatch Match(string input) {
\r
39 return Match(input, 0, input.Length);
\r
42 protected bool IsWordChar(char c) { //regexp w ([a-zA-Z_0-9]) + #@$
\r
44 return (c >= '0' && c <= '9') ||
\r
45 (c == '#' || c == '$');
\r
48 return (c <= 'Z') ||
\r
49 (c <= 'z' && c >= '_' && c != '`');
\r
53 public class SimpleCapture {
\r
54 readonly int _index;
\r
55 readonly int _length;
\r
56 readonly string _input;
\r
58 protected SimpleCapture(int index, int length, string input) {
\r
64 protected internal int Index {
\r
70 protected internal int Length {
\r
76 protected internal string Value {
\r
78 return Input.Substring(Index, Length);
\r
82 protected string Input {
\r
89 public class SimpleMatch : SimpleCapture {
\r
90 readonly bool _success;
\r
91 readonly SimpleRegex _regex;
\r
92 readonly int _total;
\r
95 public SimpleMatch(SimpleRegex regex, int total, bool success, int index, int length, string input)
\r
96 : this(regex, total, success, index, length, 0, input) {}
\r
98 public SimpleMatch(SimpleRegex regex, int total, bool success, int index, int length, int skip, string input)
\r
99 : base(index, length, input) {
\r
100 _success = success;
\r
106 protected internal SimpleMatch NextMatch() {
\r
107 return _regex.Match(Input, Index+Length+_skip, _total);
\r
110 protected internal bool Success {
\r
117 internal class OleDbParamsRegex : SimpleRegex {
\r
118 protected internal override SimpleMatch Match(string input, int beginning, int length) {
\r
120 for (int i = beginning; i < length; i++) {
\r
121 char ch = input[i];
\r
124 int end = input.IndexOf('\'', i+1);
\r
132 int end = input.IndexOf('"', i+1);
\r
140 int end = input.IndexOf(']', i+1);
\r
148 return new SimpleMatch(this, length, true, i, 1, input);
\r
153 return new SimpleMatch(this, length, false, length, 0, input);
\r
157 internal class SqlParamsRegex : SimpleRegex {
\r
158 //static readonly char[] charsEnd = new char[] {' ', '\f', '\t', '\v', '\r', '\n', ',', ';', '(', ')', '[', ']','='};
\r
159 protected internal override SimpleMatch Match(string input, int beginning, int length) {
\r
161 int actualLen = length-1; //there must be something after @
\r
162 for (int i = beginning; i < actualLen; i++) {
\r
163 char ch = input[i];
\r
166 int end = input.IndexOf('\'', i+1);
\r
174 int end = input.IndexOf('"', i+1);
\r
182 int end = input.IndexOf(']', i+1);
\r
194 }while (i < length && input[i] == '@');
\r
199 while (i < length && IsWordChar(input[i]))
\r
202 return new SimpleMatch(this, length, true, start, i-start, input);
\r
207 return new SimpleMatch(this, length, false, length, 0, input);
\r
211 internal class CharacterSplitterRegex : SimpleRegex {
\r
213 internal CharacterSplitterRegex(char delimiter) {
\r
214 _delimiter = delimiter;
\r
216 protected internal override SimpleMatch Match(string input, int beginning, int length) {
\r
218 for (int i = beginning; i < length; i++) {
\r
219 char ch = input[i];
\r
222 int end = input.IndexOf('\'', i+1);
\r
230 int end = input.IndexOf('"', i+1);
\r
238 int end = input.IndexOf(']', i+1);
\r
246 if (ch != _delimiter)
\r
249 return new SimpleMatch(this, length, true, beginning, i-beginning, 1, input);
\r
254 int matchLength = length-beginning;
\r
255 return new SimpleMatch(this, length, matchLength > 0, beginning, matchLength, input);
\r