Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (66 sloc) 2.36 KB
using System;
using System.Text.RegularExpressions;
namespace MongoDB.Driver{
public class Oid
{
private byte[] value;
public byte[] Value {
get { return this.value; }
set {
this.value = value;
}
}
public Oid(){}
public Oid(string value){
ValidateHex(value);
this.Value = DecodeHex(value);
}
public Oid(byte[] value){
this.Value = value;
}
public override string ToString(){
return BitConverter.ToString(value).Replace("-","").ToLower();
}
public override bool Equals(object obj){
if(obj is Oid){
Oid o = obj as Oid;
string hex = (o).ToString();
return this.ToString().Equals(hex);
}
return false;
}
protected void ValidateHex(string val){
if(val == null || val.Length != 24) throw new ArgumentException("Oid strings should be 24 characters");
Regex notHexChars = new Regex(@"[^A-Fa-f0-9]", RegexOptions.None);
if(notHexChars.IsMatch(val)){
throw new ArgumentOutOfRangeException("val","Value contains invalid characters");
}
}
protected static byte[] DecodeHex(string val){
int numberChars = val.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2){
try{
bytes[i / 2] = Convert.ToByte(val.Substring(i, 2), 16);
}
catch{
//failed to convert these 2 chars, they may contain illegal charracters
bytes[i / 2] = 0;
}
}
return bytes;
}
public static bool operator ==(Oid a, Oid b)
{
if (((object)a) == null && ((object)b) == null) return true;
else if ((object)a != null) return a.Equals(b);
else return false;
}
public static bool operator !=(Oid a, Oid b)
{
if (((object)a) == null && ((object)b) == null) return false;
else if ((object)a != null) return !a.Equals(b);
else return true;
}
}
}
Jump to Line
Something went wrong with that request. Please try again.