This repository was archived by the owner on Feb 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFormatting.cs
201 lines (187 loc) · 7.12 KB
/
Formatting.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using System;
namespace Shoko.Commons.Utils
{
public class Formatting
{
public static string FormatAniDBRating(double rat) => $"{rat:0.00}";
public static string FormatPercentage(double val) => $"{val:0.0}%";
public static string FormatSecondsToDisplayTime(int secs)
{
TimeSpan t = TimeSpan.FromSeconds(secs);
return t.Hours > 0 ? $"{t.Hours}:{t.Minutes.ToString().PadLeft(2, '0')}:{t.Seconds.ToString().PadLeft(2, '0')}" : $"{t.Minutes}:{t.Seconds.ToString().PadLeft(2, '0')}";
}
public static string FormatFileSize(long bytes) => ((bytes / 1024f) / 1024f).ToString("##.# MB");
public static string FormatFileSize(double bytes)
{
string[] suffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
for (int i = 0; i < suffixes.Length; i++)
{
if (bytes <= (Math.Pow(1024, i + 1)))
{
return ThreeNonZeroDigits(bytes /
Math.Pow(1024, i)) +
" " + suffixes[i];
}
}
return ThreeNonZeroDigits(bytes /
Math.Pow(1024, suffixes.Length - 1)) +
" " + suffixes[suffixes.Length - 1];
}
public static string FormatBitRate(double bytes)
{
string[] suffixes = { "bytes", "kbps" };
for (int i = 0; i < suffixes.Length; i++)
{
if (bytes <= (Math.Pow(1024, i + 1)))
{
return ThreeNonZeroDigits(bytes /
Math.Pow(1024, i)) +
" " + suffixes[i];
}
}
return ThreeNonZeroDigits(bytes /
Math.Pow(1024, suffixes.Length - 1)) +
" " + suffixes[suffixes.Length - 1];
}
private static string ThreeNonZeroDigits(double value)
{
if (value >= 100)
{
// No digits after the decimal.
return value.ToString("0,0");
}
else if (value >= 10)
{
// One digit after the decimal.
return value.ToString("0.0");
}
else
{
// Two digits after the decimal.
return value.ToString("0.00");
}
}
private static string[] escapes = { "SOURCE", "TAKEN", "FROM", "HTTP", "ANN", "ANIMENFO", "ANIDB", "ANIMESUKI" };
public static string ReparseDescription(string description)
{
if (description == null || description.Length == 0) return "";
string val = description;
val = val.Replace("<br />", Environment.NewLine).Replace("<br/>", Environment.NewLine).Replace("<i>", "").
Replace("</i>", "").Replace("<b>", "").Replace("</b>", "").Replace("[i]", "").Replace("[/i]", "").
Replace("[b]", "").Replace("[/b]", "");
val = val.Replace("<BR />", Environment.NewLine).Replace("<BR/>", Environment.NewLine).Replace("<I>", "").Replace("</I>", "").Replace("<B>", "").Replace("</B>", "").Replace("[I]", "").Replace("[/I]", "").
Replace("[B]", "").Replace("[/B]", "");
string vup = val.ToUpper();
while ((vup.Contains("[URL")) || (vup.Contains("[/URL]")))
{
int a = vup.IndexOf("[URL");
if (a >= 0)
{
int b = vup.IndexOf("]", a + 1);
if (b >= 0)
{
val = val.Substring(0, a) + val.Substring(b + 1);
vup = val.ToUpper();
}
}
a = vup.IndexOf("[/URL]");
if (a >= 0)
{
val = val.Substring(0, a) + val.Substring(a + 6);
vup = val.ToUpper();
}
}
while (vup.Contains("HTTP:"))
{
int a = vup.IndexOf("HTTP:");
if (a >= 0)
{
int b = vup.IndexOf(" ", a + 1);
if (b >= 0)
{
if (vup[b + 1] == '[')
{
int c = vup.IndexOf("]", b + 1);
val = val.Substring(0, a) + " " + val.Substring(b + 2, c - b - 2) + val.Substring(c + 1);
}
else
{
val = val.Substring(0, a) + val.Substring(b);
}
vup = val.ToUpper();
}
else
{
break;
}
}
}
int d = -1;
do
{
if (d + 1 >= vup.Length)
break;
d = vup.IndexOf("[", d + 1);
if (d != -1)
{
int b = vup.IndexOf("]", d + 1);
if (b != -1)
{
string cont = vup.Substring(d, b - d);
bool dome = false;
foreach (string s in escapes)
{
if (cont.Contains(s))
{
dome = true;
break;
}
}
if (dome)
{
val = val.Substring(0, d) + val.Substring(b + 1);
vup = val.ToUpper();
}
}
}
} while (d != -1);
d = -1;
do
{
if (d + 1 >= vup.Length)
break;
d = vup.IndexOf("(", d + 1);
if (d != -1)
{
int b = vup.IndexOf(")", d + 1);
if (b != -1)
{
string cont = vup.Substring(d, b - d);
bool dome = false;
foreach (string s in escapes)
{
if (cont.Contains(s))
{
dome = true;
break;
}
}
if (dome)
{
val = val.Substring(0, d) + val.Substring(b + 1);
vup = val.ToUpper();
}
}
}
} while (d != -1);
d = vup.IndexOf("SOURCE:");
if (d == -1)
d = vup.IndexOf("SOURCE :");
if (d > 0)
{
val = val.Substring(0, d);
}
return val.Trim();
}
}
}