Skip to content

Commit afa26f4

Browse files
committed
update
1 parent dc0d308 commit afa26f4

File tree

7 files changed

+76
-9
lines changed

7 files changed

+76
-9
lines changed

src/CatLib.Core.NetStandard/CatLib.Core.NetStandard.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
<Compile Include="..\CatLib.Core\Support\Util\Enum.cs" Link="Support\Util\Enum.cs" />
122122
<Compile Include="..\CatLib.Core\Support\Util\Extension\IntExtension.cs" Link="Support\Util\Extension\IntExtension.cs" />
123123
<Compile Include="..\CatLib.Core\Support\Util\Extension\StreamExtension.cs" Link="Support\Util\Extension\StreamExtension.cs" />
124+
<Compile Include="..\CatLib.Core\Support\Util\Extension\StringExtension.cs" Link="Support\Util\Extension\StringExtension.cs" />
124125
<Compile Include="..\CatLib.Core\Support\Util\IAwait.cs" Link="Support\Util\IAwait.cs" />
125126
<Compile Include="..\CatLib.Core\Support\Util\Str.cs" Link="Support\Util\Str.cs" />
126127
<Compile Include="..\CatLib.Core\Support\Util\SystemTime.cs" Link="Support\Util\SystemTime.cs" />

src/CatLib.Core.Tests/CatLib.Core.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<Compile Include="Support\Util\DictTests.cs" />
6161
<Compile Include="Support\Util\EnumTests.cs" />
6262
<Compile Include="Support\Util\StreamExtensionTests.cs" />
63+
<Compile Include="Support\Util\StringExtensionTests.cs" />
6364
<Compile Include="Support\Util\StrTests.cs" />
6465
<Compile Include="Support\Util\SystemTimeTests.cs" />
6566
<Compile Include="Support\VersionTests.cs" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* This file is part of the CatLib package.
3+
*
4+
* (c) Yu Bin <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Document: http://catlib.io/
10+
*/
11+
12+
using Microsoft.VisualStudio.TestTools.UnitTesting;
13+
14+
namespace CatLib.Core.Tests.Support.Util
15+
{
16+
[TestClass]
17+
public class StringExtensionTests
18+
{
19+
[TestMethod]
20+
public void TestToStream()
21+
{
22+
Assert.AreNotEqual(null, "hello world".ToStream());
23+
}
24+
}
25+
}

src/CatLib.Core/CatLib.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Support\Util\Arr.cs" />
104104
<Compile Include="Support\Util\Enum.cs" />
105105
<Compile Include="Support\Util\Extension\IntExtension.cs" />
106+
<Compile Include="Support\Util\Extension\StringExtension.cs" />
106107
<Compile Include="Support\Util\IAwait.cs" />
107108
<Compile Include="Support\Util\Str.cs" />
108109
<Compile Include="Support\Util\Extension\StreamExtension.cs" />

src/CatLib.Core/Support/Util/Extension/StreamExtension.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ namespace CatLib
2020
/// </summary>
2121
public static class StreamExtension
2222
{
23-
/// <summary>
24-
/// 编码
25-
/// </summary>
26-
private static Encoding Encoding
27-
{
28-
get { return Encoding.UTF8; }
29-
}
30-
3123
/// <summary>
3224
/// 将当前流追加到目标流中
3325
/// </summary>
@@ -73,7 +65,7 @@ public static string ToText(this Stream source, Encoding encoding = null, bool c
7365
{
7466
try
7567
{
76-
encoding = encoding ?? Encoding;
68+
encoding = encoding ?? Util.Encoding;
7769
if (source is MemoryStream)
7870
{
7971
var memoryStream = (MemoryStream) source;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* This file is part of the CatLib package.
3+
*
4+
* (c) Yu Bin <[email protected]>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* Document: http://catlib.io/
10+
*/
11+
12+
using System.IO;
13+
using System.Text;
14+
15+
namespace CatLib
16+
{
17+
/// <summary>
18+
/// 字符串扩展
19+
/// </summary>
20+
public static class StringExtension
21+
{
22+
/// <summary>
23+
/// 将指定字符串转为Stream流
24+
/// </summary>
25+
/// <param name="str">指定字符串</param>
26+
/// <param name="encoding">使用的编码</param>
27+
/// <returns></returns>
28+
public static Stream ToStream(this string str, Encoding encoding = null)
29+
{
30+
return new MemoryStream((encoding ?? Util.Encoding).GetBytes(str));
31+
}
32+
}
33+
}

src/CatLib.Core/Support/Util/Util.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
using System;
1313
using System.Reflection;
14+
using System.Text;
1415

1516
namespace CatLib
1617
{
@@ -19,6 +20,19 @@ namespace CatLib
1920
/// </summary>
2021
public static class Util
2122
{
23+
/// <summary>
24+
/// 编码
25+
/// </summary>
26+
public static Encoding Encoding { get; set; }
27+
28+
/// <summary>
29+
/// 通用支持初始化时
30+
/// </summary>
31+
static Util()
32+
{
33+
Encoding = Encoding.UTF8;
34+
}
35+
2236
/// <summary>
2337
/// 获取优先级
2438
/// </summary>

0 commit comments

Comments
 (0)