NanoXLSX.Core 3.0.0-rc.3
Loading...
Searching...
No Matches
Comparators.cs
1using System;
2using System.Runtime.InteropServices;
3using System.Security;
4
6{
10 public static class Comparators
11 {
12
16 private const float FLOAT_THRESHOLD = 0.0001f;
17
24 public static bool CompareSecureStrings(SecureString value1, SecureString value2)
25 {
26 bool v1Empty = false;
27 bool v2Empty = false;
28 if (value1 == null || value1.Length == 0)
29 {
30 v1Empty = true;
31 }
32 if (value2 == null || value2.Length == 0)
33 {
34 v2Empty = true;
35 }
36 if (v1Empty && !v2Empty || !v1Empty && v2Empty)
37 {
38 return false;
39 }
40 if (v1Empty && v2Empty)
41 {
42 return true;
43 }
44 IntPtr unmanagedString1 = IntPtr.Zero;
45 IntPtr unmanagedString2 = IntPtr.Zero;
46 try
47 {
48 unmanagedString1 = Marshal.SecureStringToBSTR(value1);
49 unmanagedString2 = Marshal.SecureStringToBSTR(value2);
50 int length1 = Marshal.ReadInt32(unmanagedString1, -4);
51 int length2 = Marshal.ReadInt32(unmanagedString2, -4);
52 if (length1 == length2)
53 {
54 for (int i = 0; i < length1; ++i)
55 {
56 byte byte1 = Marshal.ReadByte(unmanagedString1, i);
57 byte byte2 = Marshal.ReadByte(unmanagedString2, i);
58 if (byte1 != byte2) return false;
59 }
60 }
61 else
62 {
63 return false;
64 }
65 return true;
66 }
67 finally
68 {
69 // Cleanup
70 if (unmanagedString2 != IntPtr.Zero) Marshal.ZeroFreeBSTR(unmanagedString2);
71 if (unmanagedString1 != IntPtr.Zero) Marshal.ZeroFreeBSTR(unmanagedString1);
72 }
73 }
74
82 public static int CompareDimensions(float? dimension1, float? dimension2)
83 {
84 if (dimension1 == null)
85 {
86 dimension1 = float.MinValue;
87 }
88 if (dimension2 == null)
89 {
90 dimension2 = float.MinValue;
91 }
92 if (Math.Abs(dimension1.Value - dimension2.Value) < FLOAT_THRESHOLD)
93 {
94 return 0;
95 }
96 else if (dimension1 > dimension2)
97 {
98 return 1;
99 }
100 else
101 {
102 return -1;
103 }
104 }
105 }
106}
Class providing general comparator methods.
static bool CompareSecureStrings(SecureString value1, SecureString value2)
Compares whether the content of two SecureString instances are equal. The comparison method tries to ...
static int CompareDimensions(float? dimension1, float? dimension2)
Compares two dimensions (e.g. column width, column height).