regex.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. //
  2. // System.Data.ProviderBase.regex.cs
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. //
  7. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Data;
  30. using System.Data.Common;
  31. using System.Data.OleDb;
  32. namespace System.Data.ProviderBase {
  33. public abstract class SimpleRegex {
  34. abstract protected internal SimpleMatch Match(string input, int beginning, int length);
  35. protected internal SimpleMatch Match(string input) {
  36. return Match(input, 0, input.Length);
  37. }
  38. protected bool IsWordChar(char c) { //regexp w ([a-zA-Z_0-9]) + #@$
  39. if (c < '@') {
  40. return (c >= '0' && c <= '9') ||
  41. (c == '#' || c == '$');
  42. }
  43. else {
  44. return (c <= 'Z') ||
  45. (c <= 'z' && c >= '_' && c != '`');
  46. }
  47. }
  48. }
  49. public class SimpleCapture {
  50. readonly int _index;
  51. readonly int _length;
  52. readonly string _input;
  53. protected SimpleCapture(int index, int length, string input) {
  54. _index = index;
  55. _length = length;
  56. _input = input;
  57. }
  58. protected internal int Index {
  59. get {
  60. return _index;
  61. }
  62. }
  63. protected internal int Length {
  64. get {
  65. return _length;
  66. }
  67. }
  68. protected internal string Value {
  69. get {
  70. return Input.Substring(Index, Length);
  71. }
  72. }
  73. protected string Input {
  74. get {
  75. return _input;
  76. }
  77. }
  78. }
  79. public class SimpleMatch : SimpleCapture {
  80. readonly bool _success;
  81. readonly SimpleRegex _regex;
  82. readonly int _total;
  83. readonly int _skip;
  84. public SimpleMatch(SimpleRegex regex, int total, bool success, int index, int length, string input)
  85. : this(regex, total, success, index, length, 0, input) {}
  86. public SimpleMatch(SimpleRegex regex, int total, bool success, int index, int length, int skip, string input)
  87. : base(index, length, input) {
  88. _success = success;
  89. _regex = regex;
  90. _total = total;
  91. _skip = skip;
  92. }
  93. protected internal SimpleMatch NextMatch() {
  94. return _regex.Match(Input, Index+Length+_skip, _total);
  95. }
  96. protected internal bool Success {
  97. get {
  98. return _success;
  99. }
  100. }
  101. }
  102. internal class OleDbParamsRegex : SimpleRegex {
  103. protected internal override SimpleMatch Match(string input, int beginning, int length) {
  104. for (int i = beginning; i < length; i++) {
  105. char ch = input[i];
  106. switch(ch) {
  107. case '\'': {
  108. int end = input.IndexOf('\'', i+1);
  109. if (end < 0)
  110. break;
  111. i = end;
  112. break;
  113. }
  114. case '"': {
  115. int end = input.IndexOf('"', i+1);
  116. if (end < 0)
  117. break;
  118. i = end;
  119. break;
  120. }
  121. case '[': {
  122. int end = input.IndexOf(']', i+1);
  123. if (end < 0)
  124. break;
  125. i = end;
  126. break;
  127. }
  128. case '?': {
  129. return new SimpleMatch(this, length, true, i, 1, input);
  130. }
  131. }
  132. }
  133. return new SimpleMatch(this, length, false, length, 0, input);
  134. }
  135. }
  136. internal class SqlParamsRegex : SimpleRegex {
  137. //static readonly char[] charsEnd = new char[] {' ', '\f', '\t', '\v', '\r', '\n', ',', ';', '(', ')', '[', ']','='};
  138. protected internal override SimpleMatch Match(string input, int beginning, int length) {
  139. int actualLen = length-1; //there must be something after @
  140. for (int i = beginning; i < actualLen; i++) {
  141. char ch = input[i];
  142. switch(ch) {
  143. case '\'': {
  144. int end = input.IndexOf('\'', i+1);
  145. if (end < 0)
  146. break;
  147. i = end;
  148. break;
  149. }
  150. case '"': {
  151. int end = input.IndexOf('"', i+1);
  152. if (end < 0)
  153. break;
  154. i = end;
  155. break;
  156. }
  157. case '[': {
  158. int end = input.IndexOf(']', i+1);
  159. if (end < 0)
  160. break;
  161. i = end;
  162. break;
  163. }
  164. case '@': {
  165. int start = i;
  166. do {
  167. i++;
  168. }while (i < length && input[i] == '@');
  169. if (i - start > 1)
  170. break;
  171. while (i < length && IsWordChar(input[i]))
  172. i++;
  173. return new SimpleMatch(this, length, true, start, i-start, input);
  174. }
  175. }
  176. }
  177. return new SimpleMatch(this, length, false, length, 0, input);
  178. }
  179. }
  180. internal class CharacterSplitterRegex : SimpleRegex {
  181. char _delimiter;
  182. internal CharacterSplitterRegex(char delimiter) {
  183. _delimiter = delimiter;
  184. }
  185. protected internal override SimpleMatch Match(string input, int beginning, int length) {
  186. for (int i = beginning; i < length; i++) {
  187. char ch = input[i];
  188. switch(ch) {
  189. case '\'': {
  190. int end = input.IndexOf('\'', i+1);
  191. if (end < 0)
  192. break;
  193. i = end;
  194. break;
  195. }
  196. case '"': {
  197. int end = input.IndexOf('"', i+1);
  198. if (end < 0)
  199. break;
  200. i = end;
  201. break;
  202. }
  203. case '[': {
  204. int end = input.IndexOf(']', i+1);
  205. if (end < 0)
  206. break;
  207. i = end;
  208. break;
  209. }
  210. default: {
  211. if (ch != _delimiter)
  212. break;
  213. return new SimpleMatch(this, length, true, beginning, i-beginning, 1, input);
  214. }
  215. }
  216. }
  217. int matchLength = length-beginning;
  218. return new SimpleMatch(this, length, matchLength > 0, beginning, matchLength, input);
  219. }
  220. }
  221. }