Skip to content

Commit

Permalink
improve ByteExtension (string and byte[])
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed Dec 11, 2024
1 parent b0599d3 commit 6d23a32
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 45 deletions.
7 changes: 3 additions & 4 deletions src/src/extension/ByteExtension.cs
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);
}
}
}
22 changes: 22 additions & 0 deletions src/src/extension/ByterExtension.cs
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);
}
}
}
26 changes: 11 additions & 15 deletions src/src/extension/StringExtension.cs
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);
}
}
}
21 changes: 14 additions & 7 deletions test/extension/ByteExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Text;
using Byter;
using Xunit;
Expand All @@ -11,12 +12,10 @@ public class ByteExtensionTest
[Fact]
public void GetString()
{
byte[] defBytes = BaseText.GetBytes(StringExtension.Default);
byte[] utf8Bytes = BaseText.GetBytes(Encoding.UTF8);
byte[] utf16Bytes = BaseText.GetBytes(Encoding.Unicode);
byte[] utf32Bytes = BaseText.GetBytes(Encoding.UTF32);

Assert.Equal(BaseText, defBytes.GetString());
var utf8Bytes = BaseText.GetBytes(Encoding.UTF8);
var utf16Bytes = BaseText.GetBytes(Encoding.Unicode);
var utf32Bytes = BaseText.GetBytes(Encoding.UTF32);

Assert.Equal(BaseText, utf8Bytes.GetString(Encoding.UTF8));
Assert.Equal(BaseText, utf16Bytes.GetString(Encoding.Unicode));
Assert.Equal(BaseText, utf32Bytes.GetString(Encoding.UTF32));
Expand All @@ -25,4 +24,12 @@ public void GetString()
Assert.NotEqual(BaseText, utf32Bytes.GetString(Encoding.Unicode));
Assert.NotEqual(BaseText, utf8Bytes.GetString(Encoding.UTF32));
}
}

[Fact]
public void Nullable()
{
var result = string.Empty;
Assert.Equal(result, Array.Empty<byte>().GetString());
Assert.Equal(result, ByteExtension.GetString(null!));
}
}
48 changes: 29 additions & 19 deletions test/extension/StringExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Text;
using Byter;
using Xunit;
Expand Down Expand Up @@ -26,33 +27,34 @@ public void GetBytes()
[Fact]
public void GetBytesDefault()
{
Encoding defaultEncoding = Encoding.UTF8;
byte[] defaultEncodingResult = defaultEncoding.GetBytes(BaseText);
byte[] internalDefaultEncodingResult = BaseText.GetBytes();
var utf8Encoded = Encoding.UTF8.GetBytes(BaseText);
var utf32Encoded = Encoding.UTF32.GetBytes(BaseText);
var defaultEncoded = BaseText.GetBytes();

Assert.Equal(defaultEncoding, StringExtension.Default);
Assert.Equal(defaultEncodingResult, internalDefaultEncodingResult);

Encoding newEncoding = Encoding.UTF32;
StringExtension.Default = newEncoding;
byte[] newEncodingResult = newEncoding.GetBytes(BaseText);
byte[] internalNewEncodingResult = BaseText.GetBytes();

Assert.NotEqual(defaultEncodingResult, internalNewEncodingResult);
Assert.Equal(newEncodingResult, internalNewEncodingResult);
{
Assert.Equal(utf8Encoded, defaultEncoded);
Assert.NotEqual(utf8Encoded, utf32Encoded);
Assert.NotEqual(utf32Encoded, defaultEncoded);
Assert.Equal(utf8Encoded.GetString(Encoding.UTF8), defaultEncoded.GetString());
Assert.Equal(utf8Encoded.GetString(Encoding.UTF8), utf32Encoded.GetString(Encoding.UTF32));
Assert.NotEqual(defaultEncoded.GetString(), utf32Encoded.GetString()); // need add Encoding.UTF32
Assert.NotEqual(utf8Encoded.GetString(), utf32Encoded.GetString()); // need add Encoding.UTF32
Assert.Equal(defaultEncoded.GetString(), utf32Encoded.GetString(Encoding.UTF32)); // Now is OKAY!
Assert.Equal(utf8Encoded.GetString(), utf32Encoded.GetString(Encoding.UTF32)); // Now is OKAY!
}
}

[Fact]
public void TestText()
{
Assert.Equal(BaseText.ToLower(), BaseTextLower);
Assert.Equal(BaseText.ToUpper(), BaseTextUpper);
Assert.Equal(BaseTextLower, BaseText.ToLower());
Assert.Equal(BaseTextUpper, BaseText.ToUpper());

Assert.NotEqual(BaseText.ToLower(), BaseText);
Assert.NotEqual(BaseText.ToUpper(), BaseText);
Assert.NotEqual(BaseText, BaseText.ToLower());
Assert.NotEqual(BaseText, BaseText.ToUpper());

Assert.NotEqual(BaseText.ToLower(), BaseTextCaptz);
Assert.NotEqual(BaseText.ToUpper(), BaseTextCaptz);
Assert.NotEqual(BaseTextCaptz, BaseText.ToLower());
Assert.NotEqual(BaseTextCaptz, BaseText.ToUpper());
}

[Fact]
Expand All @@ -75,4 +77,12 @@ public void ToCapitalizeCase()
Assert.NotEqual(BaseTextCaptz, BaseText);
Assert.Equal(BaseTextCaptz, BaseText.ToCapitalize());
}

[Fact]
public void Nullable()
{
var result = Array.Empty<byte>();
Assert.Equal(result, string.Empty.GetBytes());
Assert.Equal(result, StringExtension.GetBytes(null!));
}
}

0 comments on commit 6d23a32

Please sign in to comment.