Skip to content

Commit

Permalink
Recode
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoFeiDu committed Dec 6, 2021
1 parent ff56101 commit 982df15
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/Zaabee.Extensions/Zaabee.Extensions.Bool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public static void IfFalse(this bool b, Action action)
if (!b) action();
}

public static TResult? IfTrue<TResult>(this bool b, Func<TResult> func) =>
public static TResult? IfTrue<TResult>(this bool b, Func<TResult?> func) =>
b ? func() : default;

public static TResult? IfFalse<TResult>(this bool b, Func<TResult> func) =>
public static TResult? IfFalse<TResult>(this bool b, Func<TResult?> func) =>
!b ? func() : default;

public static void IfTrueElse(this bool b, Action actionTrue, Action actionElse)
Expand All @@ -42,9 +42,9 @@ public static void IfFalseElse(this bool b, Action actionFalse, Action actionEls
else actionElse();
}

public static TResult? IfTrueElse<TResult>(this bool b, Func<TResult> funcTrue, Func<TResult> funcElse) =>
public static TResult? IfTrueElse<TResult>(this bool b, Func<TResult?> funcTrue, Func<TResult?> funcElse) =>
b ? funcTrue() : funcElse();

public static TResult? IfFalseElse<TResult>(this bool b, Func<TResult> funcFalse, Func<TResult> funcElse) =>
public static TResult? IfFalseElse<TResult>(this bool b, Func<TResult?> funcFalse, Func<TResult?> funcElse) =>
!b ? funcFalse() : funcElse();
}
7 changes: 2 additions & 5 deletions src/Zaabee.Extensions/Zaabee.Extensions.Stream.Read.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ public static async Task<byte[]> ReadToEndAsync(this Stream? stream, Cancellatio
default:
#if NETSTANDARD2_0
using (var memoryStream = new MemoryStream())
#else
await using (var memoryStream = new MemoryStream())
#endif
{

#if NETSTANDARD2_0
await stream.CopyToAsync(memoryStream);
#else
await using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream, cancellationToken);
#endif
return memoryStream.ToArray();
Expand Down
4 changes: 4 additions & 0 deletions src/Zaabee.Extensions/Zaabee.Extensions.Stream.Write.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ public static async Task<bool> TryWriteAsync(this Stream? stream, byte[] buffer,
CancellationToken cancellationToken = default)
{
var canWrite = stream is not null && stream.CanWrite;
#if NETSTANDARD2_0
if (canWrite) await stream!.WriteAsync(buffer, offset, count, cancellationToken);
#else
if (canWrite) await stream!.WriteAsync(buffer.AsMemory(offset, count), cancellationToken);
#endif
return canWrite;
}
}
11 changes: 9 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ public static string TrimStart(this string target, string? trimString)

var result = target;
while (result.StartsWith(trimString))
#if NETSTANDARD2_0
result = result.Substring(trimString!.Length);

#else
result = result[trimString.Length..];
#endif
return result;
}

Expand All @@ -21,7 +24,11 @@ public static string TrimEnd(this string target, string? trimString)

var result = target;
while (result.EndsWith(trimString))
#if NETSTANDARD2_0
result = result.Substring(0, result.Length - trimString!.Length);
#else
result = result[..^trimString.Length];
#endif

return result;
}
Expand All @@ -39,7 +46,7 @@ public static string Format(this string format, params object[] args) =>
string.Format(format, args);

public static string GetLetterOrDigit(this string source) =>
new string(source.Where(char.IsLetterOrDigit).ToArray());
new(source.Where(char.IsLetterOrDigit).ToArray());

public static string? TryReplace(this string? str, string oldValue, string? newValue) =>
string.IsNullOrEmpty(str) ? str : str!.Replace(oldValue, newValue);
Expand Down
4 changes: 2 additions & 2 deletions src/Zaabee.Extensions/Zaabee.Extensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageVersion>2021.14.0</PackageVersion>
<Version>2021.14.0</Version>
<PackageVersion>2022.1.0</PackageVersion>
<Version>2022.1.0</Version>
<Authors>Mutuduxf</Authors>
<Company>Mutuduxf</Company>
<PackageTags>Zaabee;Extensions</PackageTags>
Expand Down

0 comments on commit 982df15

Please sign in to comment.