forked from trimble-oss/dba-dash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensionMethods.cs
105 lines (93 loc) · 3.85 KB
/
ExtensionMethods.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Collections.Specialized;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO.Compression;
using System.IO;
namespace DBADash
{
public static class ExtensionMethods
{
public static void AppendCollection(this StringCollection sc1, StringCollection sc2)
{
var array = new string[sc2.Count];
sc2.CopyTo(array, 0);
sc1.AddRange(array);
}
//https://stackoverflow.com/questions/5417070/c-sharp-version-of-sql-like
public static bool Like(this string toSearch, string toFind)
{
return new Regex(@"\A" + new Regex(@"\.|\$|\^|\{|\[|\(|\||\)|\*|\+|\?|\\").Replace(toFind, ch => @"\" + ch).Replace('_', '.').Replace("%", ".*") + @"\z", RegexOptions.Singleline).IsMatch(toSearch);
}
public static T DeepCopy<T>(this T self)
{
var settings = new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto // Preserve type information
};
var serialized = JsonConvert.SerializeObject(self, settings);
return JsonConvert.DeserializeObject<T>(serialized, settings);
}
public static string Truncate(this string value, int maxLength, bool ellipsis = false)
{
if (string.IsNullOrEmpty(value)) return value;
if (ellipsis)
{
return value.Length <= maxLength ? value : value[..(maxLength - 3)] + "...";
}
else
{
return value.Length <= maxLength ? value : value[..maxLength];
}
}
/// <summary>
/// Add items from dict2 to dict1 if they don't already exist in dict1. If they do exist, use the value from dict1.
/// </summary>
/// <param name="dict1"></param>
/// <param name="dict2"></param>
/// <returns></returns>
public static Dictionary<string, CustomCollection> CombineCollections(
this Dictionary<string, CustomCollection> dict1, Dictionary<string, CustomCollection> dict2)
{
if (dict1 == null && dict2 == null) return new Dictionary<string, CustomCollection>();
if (dict2 == null) return dict1;
if (dict1 == null) return dict2;
var result = new Dictionary<string, CustomCollection>(dict1);
foreach (var item in dict2.Where(item => !result.ContainsKey(item.Key)))
{
result.Add(item.Key, item.Value);
}
return result;
}
public static byte[] Compress(this byte[] data)
{
using var sourceStream = new MemoryStream(data);
using var destinationStream = new MemoryStream();
sourceStream.CompressTo(destinationStream);
return destinationStream.ToArray();
}
public static byte[] Decompress(this byte[] data)
{
using var sourceStream = new MemoryStream(data);
using var destinationStream = new MemoryStream();
sourceStream.DecompressTo(destinationStream);
return destinationStream.ToArray();
}
public static void CompressTo(this Stream stream, Stream outputStream)
{
using var gZipStream = new GZipStream(outputStream, CompressionMode.Compress);
stream.CopyTo(gZipStream);
gZipStream.Flush();
}
public static void DecompressTo(this Stream stream, Stream outputStream)
{
using var gZipStream = new GZipStream(stream, CompressionMode.Decompress);
gZipStream.CopyTo(outputStream);
}
public static string AppendToUrl(this string url, string appendString)
{
return (url.EndsWith('/') ? url : url + '/') + appendString;
}
}
}