BsonRegularExpression.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* Copyright 2010-2016 MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Text.RegularExpressions;
  17. namespace MongoDB.Bson
  18. {
  19. /// <summary>
  20. /// Represents a BSON regular expression value.
  21. /// </summary>
  22. #if NET45
  23. [Serializable]
  24. #endif
  25. public class BsonRegularExpression : BsonValue, IComparable<BsonRegularExpression>, IEquatable<BsonRegularExpression>
  26. {
  27. // private fields
  28. private readonly string _pattern;
  29. private readonly string _options;
  30. // constructors
  31. /// <summary>
  32. /// Initializes a new instance of the BsonRegularExpression class.
  33. /// </summary>
  34. /// <param name="pattern">A regular expression pattern.</param>
  35. public BsonRegularExpression(string pattern)
  36. {
  37. if (pattern == null)
  38. {
  39. throw new ArgumentNullException("pattern");
  40. }
  41. if (pattern.Length > 0 && pattern[0] == '/')
  42. {
  43. var index = pattern.LastIndexOf('/');
  44. var escaped = pattern.Substring(1, index - 1);
  45. var unescaped = (escaped == "(?:)") ? "" : escaped.Replace("\\/", "/");
  46. _pattern = unescaped;
  47. _options = pattern.Substring(index + 1);
  48. }
  49. else
  50. {
  51. _pattern = pattern;
  52. _options = "";
  53. }
  54. }
  55. /// <summary>
  56. /// Initializes a new instance of the BsonRegularExpression class.
  57. /// </summary>
  58. /// <param name="pattern">A regular expression pattern.</param>
  59. /// <param name="options">Regular expression options.</param>
  60. public BsonRegularExpression(string pattern, string options)
  61. {
  62. if (pattern == null)
  63. {
  64. throw new ArgumentNullException("pattern");
  65. }
  66. _pattern = pattern;
  67. _options = options ?? "";
  68. }
  69. /// <summary>
  70. /// Initializes a new instance of the BsonRegularExpression class.
  71. /// </summary>
  72. /// <param name="regex">A Regex.</param>
  73. public BsonRegularExpression(Regex regex)
  74. {
  75. if (regex == null)
  76. {
  77. throw new ArgumentNullException("regex");
  78. }
  79. _pattern = regex.ToString();
  80. _options = "";
  81. if ((regex.Options & RegexOptions.IgnoreCase) != 0)
  82. {
  83. _options += "i";
  84. }
  85. if ((regex.Options & RegexOptions.Multiline) != 0)
  86. {
  87. _options += "m";
  88. }
  89. if ((regex.Options & RegexOptions.IgnorePatternWhitespace) != 0)
  90. {
  91. _options += "x";
  92. }
  93. if ((regex.Options & RegexOptions.Singleline) != 0)
  94. {
  95. _options += "s";
  96. }
  97. }
  98. // public properties
  99. /// <summary>
  100. /// Gets the BsonType of this BsonValue.
  101. /// </summary>
  102. public override BsonType BsonType
  103. {
  104. get { return BsonType.RegularExpression; }
  105. }
  106. /// <summary>
  107. /// Gets the regular expression pattern.
  108. /// </summary>
  109. public string Pattern
  110. {
  111. get { return _pattern; }
  112. }
  113. /// <summary>
  114. /// Gets the regular expression options.
  115. /// </summary>
  116. public string Options
  117. {
  118. get { return _options; }
  119. }
  120. // public operators
  121. /// <summary>
  122. /// Converts a Regex to a BsonRegularExpression.
  123. /// </summary>
  124. /// <param name="value">A Regex.</param>
  125. /// <returns>A BsonRegularExpression.</returns>
  126. public static implicit operator BsonRegularExpression(Regex value)
  127. {
  128. return new BsonRegularExpression(value);
  129. }
  130. /// <summary>
  131. /// Converts a string to a BsonRegularExpression.
  132. /// </summary>
  133. /// <param name="value">A string.</param>
  134. /// <returns>A BsonRegularExpression.</returns>
  135. public static implicit operator BsonRegularExpression(string value)
  136. {
  137. return new BsonRegularExpression(value);
  138. }
  139. /// <summary>
  140. /// Compares two BsonRegularExpression values.
  141. /// </summary>
  142. /// <param name="lhs">The first BsonRegularExpression.</param>
  143. /// <param name="rhs">The other BsonRegularExpression.</param>
  144. /// <returns>True if the two BsonRegularExpression values are not equal according to ==.</returns>
  145. public static bool operator !=(BsonRegularExpression lhs, BsonRegularExpression rhs)
  146. {
  147. return !(lhs == rhs);
  148. }
  149. /// <summary>
  150. /// Compares two BsonRegularExpression values.
  151. /// </summary>
  152. /// <param name="lhs">The first BsonRegularExpression.</param>
  153. /// <param name="rhs">The other BsonRegularExpression.</param>
  154. /// <returns>True if the two BsonRegularExpression values are equal according to ==.</returns>
  155. public static bool operator ==(BsonRegularExpression lhs, BsonRegularExpression rhs)
  156. {
  157. if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
  158. return lhs.Equals(rhs);
  159. }
  160. // public methods
  161. /// <summary>
  162. /// Creates a new BsonRegularExpression.
  163. /// </summary>
  164. /// <param name="value">An object to be mapped to a BsonRegularExpression.</param>
  165. /// <returns>A BsonRegularExpression or null.</returns>
  166. public new static BsonRegularExpression Create(object value)
  167. {
  168. if (value == null)
  169. {
  170. throw new ArgumentNullException("value");
  171. }
  172. return (BsonRegularExpression)BsonTypeMapper.MapToBsonValue(value, BsonType.RegularExpression);
  173. }
  174. // public methods
  175. /// <summary>
  176. /// Compares this BsonRegularExpression to another BsonRegularExpression.
  177. /// </summary>
  178. /// <param name="other">The other BsonRegularExpression.</param>
  179. /// <returns>A 32-bit signed integer that indicates whether this BsonRegularExpression is less than, equal to, or greather than the other.</returns>
  180. public int CompareTo(BsonRegularExpression other)
  181. {
  182. if (other == null) { return 1; }
  183. int r = _pattern.CompareTo(other._pattern);
  184. if (r != 0) { return r; }
  185. return _options.CompareTo(other._options);
  186. }
  187. /// <summary>
  188. /// Compares the BsonRegularExpression to another BsonValue.
  189. /// </summary>
  190. /// <param name="other">The other BsonValue.</param>
  191. /// <returns>A 32-bit signed integer that indicates whether this BsonRegularExpression is less than, equal to, or greather than the other BsonValue.</returns>
  192. public override int CompareTo(BsonValue other)
  193. {
  194. if (other == null) { return 1; }
  195. var otherRegularExpression = other as BsonRegularExpression;
  196. if (otherRegularExpression != null)
  197. {
  198. return CompareTo(otherRegularExpression);
  199. }
  200. return CompareTypeTo(other);
  201. }
  202. /// <summary>
  203. /// Compares this BsonRegularExpression to another BsonRegularExpression.
  204. /// </summary>
  205. /// <param name="rhs">The other BsonRegularExpression.</param>
  206. /// <returns>True if the two BsonRegularExpression values are equal.</returns>
  207. public bool Equals(BsonRegularExpression rhs)
  208. {
  209. if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
  210. return _pattern == rhs._pattern && _options == rhs._options;
  211. }
  212. /// <summary>
  213. /// Compares this BsonRegularExpression to another object.
  214. /// </summary>
  215. /// <param name="obj">The other object.</param>
  216. /// <returns>True if the other object is a BsonRegularExpression and equal to this one.</returns>
  217. public override bool Equals(object obj)
  218. {
  219. return Equals(obj as BsonRegularExpression); // works even if obj is null or of a different type
  220. }
  221. /// <summary>
  222. /// Gets the hash code.
  223. /// </summary>
  224. /// <returns>The hash code.</returns>
  225. public override int GetHashCode()
  226. {
  227. // see Effective Java by Joshua Bloch
  228. int hash = 17;
  229. hash = 37 * hash + BsonType.GetHashCode();
  230. hash = 37 * hash + _pattern.GetHashCode();
  231. hash = 37 * hash + _options.GetHashCode();
  232. return hash;
  233. }
  234. /// <summary>
  235. /// Converts the BsonRegularExpression to a Regex.
  236. /// </summary>
  237. /// <returns>A Regex.</returns>
  238. public Regex ToRegex()
  239. {
  240. var options = RegexOptions.None;
  241. if (_options.IndexOf('i') != -1)
  242. {
  243. options |= RegexOptions.IgnoreCase;
  244. }
  245. if (_options.IndexOf('m') != -1)
  246. {
  247. options |= RegexOptions.Multiline;
  248. }
  249. if (_options.IndexOf('x') != -1)
  250. {
  251. options |= RegexOptions.IgnorePatternWhitespace;
  252. }
  253. if (_options.IndexOf('s') != -1)
  254. {
  255. options |= RegexOptions.Singleline;
  256. }
  257. return new Regex(_pattern, options);
  258. }
  259. /// <summary>
  260. /// Returns a string representation of the value.
  261. /// </summary>
  262. /// <returns>A string representation of the value.</returns>
  263. public override string ToString()
  264. {
  265. var escaped = (_pattern == "") ? "(?:)" :_pattern.Replace("/", @"\/");
  266. return string.Format("/{0}/{1}", escaped, _options);
  267. }
  268. }
  269. }