-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.cs
131 lines (117 loc) · 4.38 KB
/
Utils.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Jil;
namespace TinderAutomator
{
internal static class Utils
{
public static readonly DateTime UNIX_START_DATE = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
private static readonly CompareInfo COMPINF = CultureInfo.CurrentCulture.CompareInfo;
private static readonly CompareOptions COMPOPTS = CompareOptions.IgnoreCase;
/// <summary>
/// Converts timestamp to DateTime.
/// </summary>
/// <param name="timestamp"></param>
/// <returns></returns>
public static DateTime ConvertUnixTimestamp(long timestamp)
{
return timestamp > 1000000000000 ?
UNIX_START_DATE.AddMilliseconds(timestamp).ToLocalTime() :
UNIX_START_DATE.AddSeconds(timestamp).ToLocalTime();
}
/// <summary>
/// Case-insensitive String.IndexOf
/// </summary>
/// <param name="str"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int IIndexOf(this string str, string value) =>
COMPINF.IndexOf(str, value, COMPOPTS);
/// <summary>
/// Case-insensitive String.IndexOf
/// </summary>
/// <param name="str"></param>
/// <param name="value"></param>
/// <param name="startIndex"></param>
/// <returns></returns>
public static int IIndexOf(this string str, string value, int startIndex) =>
COMPINF.IndexOf(str, value, startIndex, COMPOPTS);
/// <summary>
/// Case-insensitive String.Contains
/// </summary>
/// <param name="str"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool IContains(this string str, string value) =>
str.IIndexOf(value) >= 0;
/// <summary>
/// Case-insensitive number of occurrences of a string within another string.
/// </summary>
/// <param name="str"></param>
/// <param name="value"></param>
/// <returns></returns>
public static int ICount(this string str, string value)
{
int count = 0;
int index = str.IIndexOf(value);
while (index >= 0)
{
++count;
index = str.IIndexOf(value, index + value.Length);
}
return count;
}
public static void SaveDictAs<K, V>(this IEnumerable<KeyValuePair<K, V>> dict, string path, string separator = " :=: ")
{
if (dict != null)
File.WriteAllLines(
path, dict.Select(
kv => kv.Key.ToString() + separator + kv.Value.ToString()
)
);
}
#region SaveAs
public static void SaveAs(this Object obj, string path)
{
obj.SaveAs(path, Options.ExcludeNullsIncludeInherited);
}
public static void SaveAs(this Object obj, string path, Options opts)
{
if (obj != null)
File.WriteAllText(path, JSON.Serialize(obj, opts));
}
public static void SaveAs(this Object obj, string path, Encoding encoding)
{
obj.SaveAs(path, Options.ExcludeNullsIncludeInherited, encoding);
}
public static void SaveAs(this Object obj, string path, Options opts, Encoding encoding)
{
if (obj != null)
File.WriteAllText(path, JSON.Serialize(obj, opts), encoding);
}
#endregion
#region LoadJSON
public static T LoadJSON<T>(string path, Options opts)
{
return JSON.Deserialize<T>(File.ReadAllText(path), opts);
}
public static T LoadJSON<T>(string path)
{
return LoadJSON<T>(path, Options.ExcludeNullsIncludeInherited);
}
public static T LoadJSON<T>(string path, Options opts, Encoding encoding)
{
return JSON.Deserialize<T>(File.ReadAllText(path, encoding), opts);
}
public static T LoadJSON<T>(string path, Encoding encoding)
{
return LoadJSON<T>(path, Options.ExcludeNullsIncludeInherited, encoding);
}
#endregion
}
}