-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathUtils.cs
137 lines (119 loc) · 4.09 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
132
133
134
135
136
137
using System;
using System.Text;
namespace Milki.OsuPlayer.Media.Lyric
{
public static class Utils
{
private static void OutputToLocalConsole(string message, ConsoleColor color, bool newLine = true, bool time = true)
{
Console.ForegroundColor = color;
Console.Write((time ? "[" + DateTime.Now.ToLongTimeString() + "] " : string.Empty)
+ message
+ (newLine ? Environment.NewLine : string.Empty));
Console.ResetColor();
}
public static void Output(string message, ConsoleColor color, bool newLine = true, bool time = true)
{
OutputToLocalConsole(message, color, newLine, time);
}
public static void Debug(string message, bool newLine = true, bool time = true)
{
if (Settings.DebugMode)
Output(message, ConsoleColor.Cyan, newLine, time);
}
//https://www.programcreek.com/2013/12/edit-distance-in-java/
public static int EditDistance(string a, string b)
{
int len_a = a.Length;
int len_b = b.Length;
int[,] dp = new int[len_a + 1, len_b + 1];
for (int i = 0; i <= len_a; i++)
{
dp[i, 0] = i;
}
for (int j = 0; j <= len_b; j++)
{
dp[0, j] = j;
}
for (int i = 0; i < len_a; i++)
{
char c_a = a[i];
for (int j = 0; j < len_b; j++)
{
char c_b = b[j];
if (c_a == c_b)
{
dp[i + 1, j + 1] = dp[i, j];
}
else
{
int replace = dp[i, j] + 1;
int insert = dp[i, j + 1] + 1;
int delete = dp[i + 1, j] + 1;
int min = Math.Min(Math.Min(insert, replace), delete);
dp[i + 1, j + 1] = min;
}
}
}
return dp[len_a, len_b];
}
/// <summary>
/// Base64加密,采用utf8编码方式加密
/// </summary>
/// <param name="source">待加密的明文</param>
/// <returns>加密后的字符串</returns>
public static string Base64Encode(string source)
{
return Base64Encode(Encoding.UTF8, source);
}
/// <summary>
/// Base64加密
/// </summary>
/// <param name="encodeType">加密采用的编码方式</param>
/// <param name="source">待加密的明文</param>
/// <returns></returns>
public static string Base64Encode(Encoding encodeType, string source)
{
string encode;
byte[] bytes = encodeType.GetBytes(source);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = source;
}
return encode;
}
/// <summary>
/// Base64解密,采用utf8编码方式解密
/// </summary>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string Base64Decode(string result)
{
return Base64Decode(Encoding.UTF8, result);
}
/// <summary>
/// Base64解密
/// </summary>
/// <param name="encodeType">解密采用的编码方式,注意和加密时采用的方式一致</param>
/// <param name="result">待解密的密文</param>
/// <returns>解密后的字符串</returns>
public static string Base64Decode(Encoding encodeType, string result)
{
string decode;
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encodeType.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
}
}