添加项目文件。

main
soul liu 2 years ago
parent a10ec831fa
commit a293252159
  1. 10
      General/General.csproj
  2. 30
      General/utils/CollectionsUtils.cs
  3. 158
      General/utils/StringUtils.cs
  4. 191
      General/utils/TimerUtils.cs
  5. 25
      Soul2-Library.sln

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>Soul2.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
</PropertyGroup>
</Project>

@ -0,0 +1,30 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Soul2.General.utils
{
public class CollectionsUtils
{
public static bool isNotEmpty(IEnumerable collection)
{
return !isEmpty(collection);
}
public static bool isEmpty(IEnumerable collection)
{
if (collection == null)
{
return true;
}
foreach (var item in collection)
{
return false;
}
return true;
}
}
}

@ -0,0 +1,158 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Soul2.General.utils
{
/// <summary>
/// 字符串工具类
/// By Soul2
/// </summary>
public static class StringUtils
{
/// <summary>
/// 判断空值(null或"")
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool isEmpty(this string str)
{
return string.IsNullOrEmpty(str);
}
public static bool isNotEmpty(this string str)
{
return !string.IsNullOrEmpty(str);
}
/// <summary>
/// 判断空值(null、""或只有空格)
/// </summary>
/// <param name="str">被判断的字符串</param>
/// <returns></returns>
public static bool isNotBlank(this string str) { return !string.IsNullOrWhiteSpace(str); }
public static bool IsBlank(this string str) { return string.IsNullOrWhiteSpace(str); }
/// <summary>
/// 任何一个为空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isAnyBlank(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrWhiteSpace(str))
{
return true;
}
}
return false;
}
/// <summary>
/// 任何一个为空
/// 空格属于非空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isAnyEmpty(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrEmpty(str))
{
return true;
}
}
return false;
}
/// <summary>
/// 任何一个不为空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isNoneBlank(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrWhiteSpace(str))
{
return false;
}
}
return true;
}
/// <summary>
/// 任何一个不为空
/// 空格属于非空
/// </summary>
/// <param name="strs"></param>
/// <returns></returns>
public static bool isNoneEmpty(params string[] strs)
{
foreach (var str in strs)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
}
return true;
}
/// <summary>
/// 如果不以val结尾,则添加到结尾
/// </summary>
/// <param name="original"></param>
/// <param name="str"></param>
public static void appendIfMissing(this string original, string str)
{
if (!original.EndsWith(str))
{
original += str;
}
}
/// <summary>
/// 如果不以val开头,则添加到开头
/// </summary>
/// <param name="original"></param>
/// <param name="str"></param>
public static void prependIfMissing(this string original, string str)
{
if (!original.StartsWith(str))
{
original = str + original;
}
}
/// <summary>
/// 翻转
/// 例:"123"->"321"
/// </summary>
/// <param name="original"></param>
/// <returns></returns>
public static string flip(this string original)
{
char[] chars = original.ToCharArray();
Array.Reverse(chars);
return new string(chars);
}
public static string subStrWith(this string original, int begin, int end)
{
if (begin < 0 || end > original.Length || begin > end)
{
throw new ArgumentException("Invalid input parameters");
}
return original.Substring(begin, end - begin);
}
}
}

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Timer = System.Timers.Timer;
namespace Soul2.General.utils
{
/// <summary>
/// 扩展System.Timers.Timer
/// By Soul2
/// </summary>
public static class TimerUtils
{
private const int default_loop_times = 512;
/// <summary>
/// 一次性计时器
/// 运行结束后自动销毁
/// </summary>
/// <param name="timer"></param>
/// <param name="time"></param>
/// <param name="callback"></param>
/// <returns></returns>
public static Timer startOnce(this Timer timer, double time, Action<Timer> callback)
{
void onElapsed(object source, ElapsedEventArgs e)
{
callback(timer);
timer.destroy();
}
return run(timer, (int)(time * 1000), false, onElapsed);
}
public static Timer startOnce(this Timer timer, double time, Action callback)
{
return timer.startOnce(time, (t) => callback?.Invoke());
}
/// <summary>
/// 循环计时器
/// 可以指定运行次数
/// 运行结束后自动销毁
/// </summary>
/// <param name="timer">计时器</param>
/// <param name="time">时长</param>
/// <param name="times">次数</param>
/// <param name="callback"></param>
/// <returns></returns>
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer, int> callback)
{
int run_times = 0;
void onElapsed(object source, ElapsedEventArgs e)
{
run_times += 1;
if (run_times <= times)
{
callback(timer, run_times);
}
else
{
timer.destroy();
}
}
return run(timer, (int)(time * 1000), true, onElapsed);
}
public static Timer startLoop(this Timer timer, double time, int times, Action<Timer> callback)
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke(t));
}
public static Timer startLoop(this Timer timer, double time, int times, Action<int> callback)
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke(i));
}
public static Timer startLoop(this Timer timer, double time, int times, Action callback)
{
return timer.startLoop(time, times, (t, i) => callback?.Invoke());
}
/// <summary>
/// 循环计时器
/// 默认运行次数512
/// </summary>
/// <param name="timer">计时器</param>
/// <param name="time">时长</param>
/// <param name="times">次数</param>
/// <param name="callback"></param>
/// <returns></returns>
public static Timer startKeep(this Timer timer, double time, Action<Timer, int> callback, int times = default_loop_times)
{
int run_times = 0;
void onElapsed(object source, ElapsedEventArgs e)
{
run_times += 1;
if (run_times <= times)
{
callback(timer, run_times);
}
else
{
timer.destroy();
}
}
return run(timer, (int)(time * 1000), true, onElapsed);
}
public static Timer startKeep(this Timer timer, double time, Action<int> callback, int times = default_loop_times)
{
return timer.startKeep(time, (t, i) => callback?.Invoke(i));
}
public static Timer startKeep(this Timer timer, double time, Action<Timer> callback, int times = default_loop_times)
{
return timer.startKeep(time, (t, i) => callback?.Invoke(t));
}
public static Timer startKeep(this Timer timer, double time, Action callback, int times = default_loop_times)
{
return timer.startKeep(time, (t, i) => callback?.Invoke());
}
public static void destroy(this Timer timer)
{
if (timer.Enabled)
{
timer.Stop();
}
timer.Dispose();
}
// -----------------------------------------
private static Timer run(Timer timer, int interval, bool loop, ElapsedEventHandler elapsed)
{
timer.Elapsed += elapsed;
timer.Interval = interval;
timer.AutoReset = loop;
timer.Start();
return timer;
}
public static Timer startOnce(double time, Action<Timer> callback)
{
return new Timer().startOnce(time, callback);
}
public static Timer startOnce(double time, Action callback)
{
return new Timer().startOnce(time, callback);
}
// -----------------------------------------
public static Timer startLoop(double time, int times, Action<Timer, int> callback)
{
return new Timer().startLoop(time, times, callback);
}
public static Timer startLoop(double time, int times, Action<Timer> callback)
{
return new Timer().startLoop(time, times, callback);
}
public static Timer startLoop(double time, int times, Action<int> callback)
{
return new Timer().startLoop(time, times, callback);
}
public static Timer startLoop(double time, int times, Action callback)
{
return new Timer().startLoop(time, times, callback);
}
// -----------------------------------------
public static Timer startKeep(double time, Action<Timer, int> callback, int times = default_loop_times)
{
return new Timer().startKeep(time, callback, times);
}
public static Timer startKeep(double time, Action<int> callback, int times = default_loop_times)
{
return new Timer().startKeep(time, callback, times);
}
public static Timer startKeep(double time, Action<Timer> callback, int times = default_loop_times)
{
return new Timer().startKeep(time, callback, times);
}
public static Timer startKeep(double time, Action callback, int times = default_loop_times)
{
return new Timer().startKeep(time, callback, times);
}
}
}

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33530.505
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "General", "General\General.csproj", "{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8B24AB73-0D6F-426D-92D3-25CBE6ED718E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9A5A4059-DD4A-465F-9AF2-E036B3C730C6}
EndGlobalSection
EndGlobal
Loading…
Cancel
Save