1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . Linq ;
5+ using System . Text ;
6+ using System . Text . RegularExpressions ;
7+
8+ namespace Gommon
9+ {
10+ public partial class Extensions
11+ {
12+ private const int MemoryTierSize = 1024 ;
13+
14+ /// <summary>
15+ /// Formats a type to a pretty .NET-styled type name.
16+ /// </summary>
17+ /// <param name="type">The current Type.</param>
18+ /// <returns>A pretty string that shows what this type is.</returns>
19+ public static string AsPrettyString ( this Type type )
20+ {
21+ string FormatTypeName ( Type t )
22+ {
23+ switch ( t . Name )
24+ {
25+ case "Boolean" : return "bool" ;
26+ case "Byte" : return "byte" ;
27+ case "SByte" : return "sbyte" ;
28+ case "Int16" : return "short" ;
29+ case "UInt16" : return "ushort" ;
30+ case "Int32" : return "int" ;
31+ case "UInt32" : return "uint" ;
32+ case "Int64" : return "long" ;
33+ case "UInt64" : return "ulong" ;
34+ case "Char" : return "char" ;
35+ case "String" : return "string" ;
36+ default : return t . Name ;
37+ }
38+ }
39+
40+ var types = type . GenericTypeArguments ;
41+
42+ //thanks .NET for putting an annoying ass backtick and number at the end of type names.
43+ var vs = FormatTypeName ( type ) . Replace ( $ "`{ types . Length } ", "" ) ;
44+
45+ if ( ! types . IsEmpty ( ) ) vs += $ "<{ types . Select ( FormatTypeName ) . Join ( ", " ) } >";
46+
47+ return vs ;
48+ }
49+
50+ /// <summary>
51+ /// Gets all flags in the current enum.
52+ /// </summary>
53+ /// <param name="input">The current enum.</param>
54+ /// <typeparam name="T">The enum's type.</typeparam>
55+ /// <returns>A collection of all the current Enum's members.</returns>
56+ public static IEnumerable < T > GetFlags < T > ( this T input ) where T : Enum
57+ {
58+ return Enumerable . Cast < T > ( Enum . GetValues ( input . GetType ( ) ) ) . Where ( e => input . HasFlag ( e ) ) ;
59+ }
60+
61+ /// <summary>
62+ /// Appends all elements in the specified string array as a line to the current StringBuilder.
63+ /// </summary>
64+ /// <param name="sb">The current StringBuilder.</param>
65+ /// <param name="lines">The lines to append.</param>
66+ /// <returns>The current StringBuilder for chaining convenience.</returns>
67+ public static StringBuilder AppendAllLines ( this StringBuilder sb , params string [ ] lines )
68+ {
69+ foreach ( var line in lines )
70+ {
71+ sb . AppendLine ( line ) ;
72+ }
73+
74+ return sb ;
75+ }
76+
77+ /// <summary>
78+ /// Attempts to match a string against regex and the resulting match is the out variable.
79+ /// </summary>
80+ /// <param name="regex">The current regex.</param>
81+ /// <param name="str">The string to attempt to match to.</param>
82+ /// <param name="match">The resulting match.</param>
83+ /// <returns>True if it was a match and <paramref name="match"/> has a value; false otherwise.</returns>
84+ public static bool IsMatch ( this Regex regex , string str , out Match match )
85+ {
86+ match = regex . Match ( str ) ;
87+ return match . Success ;
88+ }
89+
90+ /// <summary>
91+ /// Gets the current process's memory usage as a pretty string. Can be shown in Bytes or all the way up to Terabytes via the <paramref name="memType"/> parameter.
92+ /// </summary>
93+ /// <param name="process">The current Process.</param>
94+ /// <param name="memType">The MemoryType to format the string to.</param>
95+ /// <returns>The formatted string.</returns>
96+ public static string GetMemoryUsage ( this Process process , MemoryType memType = MemoryType . Megabytes )
97+ {
98+ var res = process . PrivateMemorySize64 ;
99+ switch ( memType )
100+ {
101+ case MemoryType . Terabytes :
102+ return $ "{ res / MemoryTierSize / MemoryTierSize / MemoryTierSize / MemoryTierSize } TB";
103+ case MemoryType . Gigabytes : return $ "{ res / MemoryTierSize / MemoryTierSize / MemoryTierSize } GB";
104+ case MemoryType . Megabytes : return $ "{ res / MemoryTierSize / MemoryTierSize } MB";
105+ case MemoryType . Kilobytes : return $ "{ res / MemoryTierSize } KB";
106+ case MemoryType . Bytes : return $ "{ res } B";
107+ default : return "null" ;
108+ }
109+ }
110+ }
111+
112+ public enum MemoryType
113+ {
114+ Terabytes ,
115+ Gigabytes ,
116+ Megabytes ,
117+ Kilobytes ,
118+ Bytes
119+ }
120+ }
0 commit comments