2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
4
using System . Text ;
5
- using System . Text . RegularExpressions ;
6
5
7
6
namespace Exceptionless . Extensions {
8
7
public static class StringExtensions {
@@ -56,47 +55,14 @@ private static bool CheckForMatch(string pattern, string value, bool ignoreCase
56
55
57
56
return value . Equals ( pattern ) ;
58
57
}
59
-
60
- public static byte [ ] ToByteArray ( this string hex ) {
61
- return Enumerable . Range ( 0 , hex . Length ) .
62
- Where ( x => 0 == x % 2 ) .
63
- Select ( x => Convert . ToByte ( hex . Substring ( x , 2 ) , 16 ) ) .
64
- ToArray ( ) ;
65
- }
66
-
58
+
67
59
public static string ToHex ( this IEnumerable < byte > bytes ) {
68
60
var sb = new StringBuilder ( ) ;
69
61
foreach ( byte b in bytes )
70
62
sb . Append ( b . ToString ( "x2" ) ) ;
71
63
return sb . ToString ( ) ;
72
64
}
73
65
74
- public static bool IsNullOrEmpty ( this string item ) {
75
- return String . IsNullOrEmpty ( item ) ;
76
- }
77
-
78
- public static bool IsNullOrWhiteSpace ( this string item ) {
79
- return String . IsNullOrWhiteSpace ( item ) ;
80
- }
81
-
82
- public static bool IsMixedCase ( this string s ) {
83
- if ( s . IsNullOrEmpty ( ) )
84
- return false ;
85
-
86
- var containsUpper = false ;
87
- var containsLower = false ;
88
-
89
- foreach ( char c in s ) {
90
- if ( Char . IsUpper ( c ) )
91
- containsUpper = true ;
92
-
93
- if ( Char . IsLower ( c ) )
94
- containsLower = true ;
95
- }
96
-
97
- return containsLower && containsUpper ;
98
- }
99
-
100
66
public static bool IsValidIdentifier ( this string value ) {
101
67
if ( value == null )
102
68
return false ;
@@ -108,74 +74,5 @@ public static bool IsValidIdentifier(this string value) {
108
74
109
75
return true ;
110
76
}
111
-
112
- public static string ToPascalCase ( this string value , Regex splitRegex ) {
113
- if ( String . IsNullOrEmpty ( value ) )
114
- return value ;
115
-
116
- var mixedCase = value . IsMixedCase ( ) ;
117
- var names = splitRegex . Split ( value ) ;
118
- var output = new StringBuilder ( ) ;
119
-
120
- if ( names . Length > 1 ) {
121
- foreach ( string name in names ) {
122
- if ( name . Length > 1 ) {
123
- output . Append ( Char . ToUpper ( name [ 0 ] ) ) ;
124
- output . Append ( mixedCase ? name . Substring ( 1 ) : name . Substring ( 1 ) . ToLower ( ) ) ;
125
- } else {
126
- output . Append ( name ) ;
127
- }
128
- }
129
- } else if ( value . Length > 1 ) {
130
- output . Append ( Char . ToUpper ( value [ 0 ] ) ) ;
131
- output . Append ( mixedCase ? value . Substring ( 1 ) : value . Substring ( 1 ) . ToLower ( ) ) ;
132
- } else {
133
- output . Append ( value . ToUpper ( ) ) ;
134
- }
135
-
136
- return output . ToString ( ) ;
137
- }
138
-
139
- public static string ToPascalCase ( this string value ) {
140
- return value . ToPascalCase ( _splitNameRegex ) ;
141
- }
142
-
143
- /// <summary>
144
- /// Takes a NameIdentifier and spaces it out into words "Name Identifier".
145
- /// </summary>
146
- /// <param name="value">The value to convert.</param>
147
- /// <returns>The string</returns>
148
- public static string [ ] ToWords ( this string value ) {
149
- var words = new List < string > ( ) ;
150
- value = ToPascalCase ( value ) ;
151
-
152
- MatchCollection wordMatches = _properWordRegex . Matches ( value ) ;
153
- foreach ( Match word in wordMatches ) {
154
- if ( ! word . Value . IsNullOrWhiteSpace ( ) )
155
- words . Add ( word . Value ) ;
156
- }
157
-
158
- return words . ToArray ( ) ;
159
- }
160
-
161
- /// <summary>
162
- /// Takes a NameIdentifier and spaces it out into words "Name Identifier".
163
- /// </summary>
164
- /// <param name="value">The value to convert.</param>
165
- /// <returns>The string</returns>
166
- public static string ToSpacedWords ( this string value ) {
167
- string [ ] words = ToWords ( value ) ;
168
-
169
- var spacedName = new StringBuilder ( ) ;
170
- foreach ( string word in words ) {
171
- spacedName . Append ( word ) ;
172
- spacedName . Append ( ' ' ) ;
173
- }
174
-
175
- return spacedName . ToString ( ) . Trim ( ) ;
176
- }
177
-
178
- private static readonly Regex _properWordRegex = new Regex ( @"([A-Z][a-z]*)|([0-9]+)" ) ;
179
- private static readonly Regex _splitNameRegex = new Regex ( @"[\W_]+" ) ;
180
77
}
181
78
}
0 commit comments