Skip to content

Commit d6e694b

Browse files
committed
字符串截取健壮性提升,版本号升级
- 版本号升级至 10.0.1-preview.522 - 字符串截取方法未找到时返回空字符串,避免异常 - Enum.Parse/Guid.Parse 增加 null-forgiving 操作符 - 多处 string.Format 优化为字符串插值 - 微调 Unicode 编码及文件结尾格式
1 parent 02929c5 commit d6e694b

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

build/version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<Project>
22
<PropertyGroup>
33
<VersionMain>10.0</VersionMain>
4-
<VersionPrefix>0</VersionPrefix>
4+
<VersionPrefix>1</VersionPrefix>
55
<VersionSuffix>-preview.</VersionSuffix>
6-
<VersionSuffixVersion>117</VersionSuffixVersion>
6+
<VersionSuffixVersion>522</VersionSuffixVersion>
77
<!--<Version>$(VersionMain).$(VersionPrefix)$(VersionSuffix)$(VersionSuffixVersion)</Version>
88
<FileVersion>$(VersionMain).$(VersionPrefix).$(VersionSuffixVersion)</FileVersion>-->
99
<Version>$(VersionMain).$(VersionPrefix)</Version>

src/OSharp.Utils/Extensions/ObjectExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// -----------------------------------------------------------------------
1+
// -----------------------------------------------------------------------
22
// <copyright file="AbstractBuilder.cs" company="OSharp开源团队">
33
// Copyright (c) 2014 OSharp. All rights reserved.
44
// </copyright>
@@ -48,11 +48,11 @@ public static object CastTo(this object value, Type conversionType)
4848
}
4949
if (conversionType.IsEnum)
5050
{
51-
return Enum.Parse(conversionType, value.ToString());
51+
return Enum.Parse(conversionType, value.ToString()!);
5252
}
5353
if (conversionType == typeof(Guid))
5454
{
55-
return Guid.Parse(value.ToString());
55+
return Guid.Parse(value.ToString()!);
5656
}
5757
return Convert.ChangeType(value, conversionType);
5858
}
@@ -210,4 +210,4 @@ public static void Validate(this object obj)
210210

211211
#endregion
212212
}
213-
}
213+
}

src/OSharp.Utils/Extensions/StringExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static string Substring(this string source, string startString, params st
163163
startIndex = source.IndexOf(startString, StringComparison.OrdinalIgnoreCase);
164164
if (startIndex < 0)
165165
{
166-
throw new InvalidOperationException(string.Format("在源字符串中无法找到“{0}”的子串位置", startString));
166+
return string.Empty;
167167
}
168168
startIndex = startIndex + startString.Length;
169169
}
@@ -185,7 +185,7 @@ public static string Substring(this string source, string startString, params st
185185
}
186186
if (endIndex < 0 || endIndex < startIndex)
187187
{
188-
throw new InvalidOperationException(string.Format("在源字符串中无法找到“{0}”的子串位置", endStrings.ExpandAndToString()));
188+
return string.Empty;
189189
}
190190

191191
int length = endIndex - startIndex;
@@ -210,7 +210,7 @@ public static string SubstringRegex(this string source, string startString, stri
210210
return string.Empty;
211211
}
212212
string inner = containsEmpty ? "\\s\\S" : "\\S";
213-
string result = source.Match(string.Format("(?<={0})([{1}]+?)(?={2})", startString, inner, endString));
213+
string result = source.Match($"(?<={startString})([{inner}]+?)(?={endString})");
214214
return result.IsMissing() ? null : result;
215215
}
216216

@@ -293,15 +293,15 @@ public static bool IsIdentityCardId(this string value)
293293
return false;
294294
}
295295
array = regex.Split(value);
296-
return DateTime.TryParse(string.Format("{0}-{1}-{2}", "19" + array[2], array[3], array[4]), out time);
296+
return DateTime.TryParse($"{"19" + array[2]}-{array[3]}-{array[4]}", out time);
297297
}
298298
regex = new Regex(@"^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9Xx])$");
299299
if (!regex.Match(value).Success)
300300
{
301301
return false;
302302
}
303303
array = regex.Split(value);
304-
if (!DateTime.TryParse(string.Format("{0}-{1}-{2}", array[2], array[3], array[4]), out time))
304+
if (!DateTime.TryParse($"{array[2]}-{array[3]}-{array[4]}", out time))
305305
{
306306
return false;
307307
}
@@ -817,7 +817,7 @@ public static byte[] ToHexBytes(this string hexString)
817817
public static string ToUnicodeString(this string source)
818818
{
819819
Regex regex = new Regex(@"[^\u0000-\u00ff]");
820-
return regex.Replace(source, m => string.Format(@"\u{0:x4}", (short)m.Value[0]));
820+
return regex.Replace(source, m => $@"\u{(short)m.Value[0]:x4}");
821821
}
822822

823823
/// <summary>

0 commit comments

Comments
 (0)