From a015f38a4bca26837a11cd7fdf79159544def0f8 Mon Sep 17 00:00:00 2001 From: Gan Keyu Date: Sun, 30 Jul 2023 16:49:49 +0800 Subject: [PATCH] fix warnings on comparing value types to null --- main/SS/Formula/Functions/EDate.cs | 9 +++++++-- main/Util/StringUtil.cs | 4 ++-- .../OPC/Internal/PackagePropertiesPart.cs | 18 +++++++++++------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/main/SS/Formula/Functions/EDate.cs b/main/SS/Formula/Functions/EDate.cs index 517df60e6..96cfbc764 100644 --- a/main/SS/Formula/Functions/EDate.cs +++ b/main/SS/Formula/Functions/EDate.cs @@ -59,11 +59,16 @@ public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec) int offsetInMonthAsNumber = (int)GetValue(args[1]); // resolve the arguments - DateTime startDate = DateUtil.GetJavaDate(startDateAsNumber); - if (startDate == null) + DateTime startDate; + try + { + startDate = DateUtil.GetJavaDate(startDateAsNumber); + } + catch (ArgumentException) { return ErrorEval.VALUE_INVALID; } + DateTime resultDate = startDate.AddMonths(offsetInMonthAsNumber); result = DateUtil.GetExcelDate(resultDate); diff --git a/main/Util/StringUtil.cs b/main/Util/StringUtil.cs index 01b984464..4bb30c4a8 100644 --- a/main/Util/StringUtil.cs +++ b/main/Util/StringUtil.cs @@ -510,8 +510,8 @@ public static String MapMsCodepointString(String string1) { int msCodepoint = char.ConvertToUtf32(string1, offset);//codePointAt(stringChars, offset, string1.Length); - int uniCodepoint = msCodepointToUnicode[(msCodepoint)]; - sb.Append(Char.ConvertFromUtf32(uniCodepoint == null ? msCodepoint : uniCodepoint)); + + sb.Append(Char.ConvertFromUtf32(msCodepointToUnicode.TryGetValue(msCodepoint, out var uniCodepoint) ? uniCodepoint : msCodepoint)); offset += CharCount(msCodepoint); } diff --git a/openxml4Net/OPC/Internal/PackagePropertiesPart.cs b/openxml4Net/OPC/Internal/PackagePropertiesPart.cs index 4bd59e853..d5ed913b1 100644 --- a/openxml4Net/OPC/Internal/PackagePropertiesPart.cs +++ b/openxml4Net/OPC/Internal/PackagePropertiesPart.cs @@ -309,7 +309,7 @@ public Nullable GetModifiedProperty() { * @return A string representation of the modified date. */ public String GetModifiedPropertyString() { - if (modified.Value == null) + if (modified == null) return GetDateValue(new Nullable(new DateTime())); else return GetDateValue(modified); @@ -570,10 +570,12 @@ private String SetStringValue(String s) { { SimpleDateFormat df = new SimpleDateFormat(fStr); df.TimeZone = TimeZoneInfo.Utc; - DateTime d = df.Parse(dateTzStr); - if (d != null) + try + { + return df.Parse(dateTzStr); + } + catch (FormatException) { - return new DateTime?(d); } } } @@ -582,10 +584,12 @@ private String SetStringValue(String s) { { SimpleDateFormat df = new SimpleDateFormat(fStr); df.TimeZone = TimeZoneInfo.Utc; - DateTime d = df.Parse(dateTzStr).ToUniversalTime(); - if (d != null) + try + { + return df.Parse(dateTzStr).ToUniversalTime(); + } + catch (FormatException) { - return new DateTime?(d); } } //if you're here, no pattern matched, throw exception