Skip to content

Commit

Permalink
Fix bug
Browse files Browse the repository at this point in the history
Signed-off-by: virtual <1185513330@qq.com>
  • Loading branch information
cocosip committed Oct 16, 2023
1 parent c04b182 commit 14b9d8c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
2 changes: 1 addition & 1 deletion common.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>
<PropertyGroup>
<LangVersion>latest</LangVersion>
<Version>5.1.1</Version>
<Version>5.1.2</Version>
<Authors>virtual</Authors>
<Product>DotCommon</Product>
<PackageProjectUrl>https://github.com/cocosip/DotCommon</PackageProjectUrl>
Expand Down
12 changes: 4 additions & 8 deletions src/DotCommon/Utility/StringUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,11 @@ public static string Anonymous(string source, int length, char replace = '*')
}

var valueChars = source.ToCharArray();
if (valueChars.Length == 1)
if (valueChars.Length == 2)
{
valueChars[0] = replace;
chars[chars.Length - 1] = valueChars[valueChars.Length - 1];
}
else if (valueChars.Length == 2)
{
chars[chars.Length - 1] = replace;
}
else
else if (valueChars.Length > 2)
{
if (valueChars.Length < length)
{
Expand All @@ -393,7 +389,7 @@ public static string Anonymous(string source, int length, char replace = '*')
}
}

return chars.ToString();
return new string(chars);
}

}
Expand Down
8 changes: 2 additions & 6 deletions test/DotCommon.Test/Extensions/TaskExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
using Moq;
using DotCommon.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using DotCommon.Extensions;
using System.Threading;
using System.Linq;

namespace DotCommon.Test.Extensions
{
Expand Down
22 changes: 22 additions & 0 deletions test/DotCommon.Test/Utility/StringUtilTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ public void IsSafeSqlString_Test()
Assert.False(StringUtil.IsSafeSqlString(s1));
}

[Fact]
public void Anonymous_Test()
{
var n1 = "周";
var n2 = "张三";
var n3 = "曾国藩";
var n4 = "ZHANGJIAGANG";

var v1 = StringUtil.Anonymous(n1, 4);
Assert.Equal("****", v1);

var v2 = StringUtil.Anonymous(n2, 4);
Assert.Equal("***三", v2);

var v3 = StringUtil.Anonymous(n3, 4);
var v3_3 = StringUtil.Anonymous(n3, 3);
Assert.Equal("***藩", v3);
Assert.Equal("曾*藩", v3_3);

var v4 = StringUtil.Anonymous(n4, 4);
Assert.Equal("Z**G", v4);
}

}
}

0 comments on commit 14b9d8c

Please sign in to comment.