-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve ByteExtension (string and byte[])
- Loading branch information
Showing
5 changed files
with
79 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
using System.Text; | ||
using ByEx = Byter.ByterExtension; | ||
|
||
namespace Byter | ||
{ | ||
public static class ByteExtension | ||
{ | ||
public static string GetString(this byte[] content) | ||
{ | ||
if (content == null || content.Length <= 0) return string.Empty; | ||
return StringExtension.Default.GetString(content); | ||
return ByEx.IsNull(content) ? ByEx.DEFAULT_STRING_VALUE : ByEx.DEFAULT_ENCODING.GetString(content); | ||
} | ||
|
||
public static string GetString(this byte[] content, Encoding encoding) | ||
{ | ||
if (content == null || content.Length <= 0) return string.Empty; | ||
return encoding.GetString(content); | ||
return ByEx.IsNull(content) ? ByEx.DEFAULT_STRING_VALUE : encoding.GetString(content); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Text; | ||
|
||
namespace Byter | ||
{ | ||
internal static class ByterExtension | ||
{ | ||
public static readonly Encoding DEFAULT_ENCODING = Encoding.UTF8; | ||
public static readonly byte[] DEFAULT_BYTES_VALUE = Array.Empty<byte>(); | ||
public static readonly string DEFAULT_STRING_VALUE = string.Empty; | ||
|
||
public static bool IsNull(string value) | ||
{ | ||
return string.IsNullOrEmpty(value); | ||
} | ||
|
||
public static bool IsNull(byte[] value) | ||
{ | ||
return !(value != null && value.Length > 0); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
using System.Globalization; | ||
using System.Text; | ||
using ByEx = Byter.ByterExtension; | ||
|
||
namespace Byter | ||
{ | ||
public static class StringExtension | ||
{ | ||
public static Encoding Default { get; set; } = Encoding.UTF8; | ||
|
||
public static byte[] GetBytes(this string content) | ||
public static byte[] GetBytes(this string value) | ||
{ | ||
return Default.GetBytes(content); | ||
return ByEx.IsNull(value) ? ByEx.DEFAULT_BYTES_VALUE : ByEx.DEFAULT_ENCODING.GetBytes(value); | ||
} | ||
|
||
public static byte[] GetBytes(this string content, Encoding encoding) | ||
public static byte[] GetBytes(this string value, Encoding encoding) | ||
{ | ||
return encoding.GetBytes(content); | ||
return ByEx.IsNull(value) ? ByEx.DEFAULT_BYTES_VALUE : encoding.GetBytes(value); | ||
} | ||
|
||
public static string ToCapitalize(this string content) | ||
public static string ToCapitalize(this string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(content)) return content; | ||
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(content); | ||
return ByEx.IsNull(value) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value); | ||
} | ||
|
||
public static string ToUpperCase(this string content) | ||
public static string ToUpperCase(this string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(content)) return content; | ||
return CultureInfo.CurrentCulture.TextInfo.ToUpper(content); | ||
return ByEx.IsNull(value) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToUpper(value); | ||
} | ||
|
||
public static string ToLowerCase(this string content) | ||
public static string ToLowerCase(this string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(content)) return content; | ||
return CultureInfo.CurrentCulture.TextInfo.ToLower(content); | ||
return ByEx.IsNull(value) ? string.Empty : CultureInfo.CurrentCulture.TextInfo.ToLower(value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters