Skip to content

Commit

Permalink
Test for empty strings using string length
Browse files Browse the repository at this point in the history
Signed-off-by: nMatveev <nikolayMatveev.job@gmail.com>
  • Loading branch information
makeProjectGreatAgain authored and igitur committed Jun 18, 2020
1 parent b83be99 commit 8c1b7ad
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion .editorconfig
Expand Up @@ -8,4 +8,7 @@ trim_trailing_whitespace = true
# 4 space indentation
[*.cs]
indent_style = space
indent_size = 4
indent_size = 4

# CA1820: Test for empty strings using string length
dotnet_diagnostic.CA1820.severity = error
6 changes: 3 additions & 3 deletions ClosedXML/Excel/CalcEngine/CalcEngineHelpers.cs
Expand Up @@ -44,10 +44,10 @@ internal static bool ValueSatisfiesCriteria(object value, object criteria, CalcE
else if (criteria is TimeSpan ts) cdbl = ts.TotalDays;
else if (criteria is String cs)
{
if (value is string && (value as string).Trim() == "")
return cs == "";
if (value is string && (value as string).Trim().Length == 0)
return cs.Length == 0;

if (cs == "")
if (cs.Length == 0)
return cs.Equals(value);

// if criteria is an expression (e.g. ">20"), use calc engine
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML/Excel/CalcEngine/Functions/MathTrig.cs
Expand Up @@ -576,7 +576,7 @@ private static object Arabic(List<Expression> p)

try
{
if (input == "")
if (input.Length == 0)
return 0;
if (input == "-")
throw new NumberException();
Expand Down
2 changes: 1 addition & 1 deletion ClosedXML/Excel/CalcEngine/Functions/XLMath.cs
Expand Up @@ -120,7 +120,7 @@ public static string ToRoman(int number)

public static int RomanToArabic(string text)
{
if (text == "")
if (text.Length == 0)
return 0;
if (text.StartsWith("M", StringComparison.InvariantCultureIgnoreCase))
return 1000 + RomanToArabic(text.Substring(1));
Expand Down
8 changes: 4 additions & 4 deletions ClosedXML/Excel/Cells/XLCell.cs
Expand Up @@ -1138,13 +1138,13 @@ public XLDataType DataType
case DateTime d:
_cellValue = d.ToOADate().ToInvariantString();

if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0)
if (style.NumberFormat.Format.Length == 0 && style.NumberFormat.NumberFormatId == 0)
Style.NumberFormat.NumberFormatId = _cellValue.Contains('.') ? 22 : 14;

break;

case TimeSpan ts:
if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0)
if (style.NumberFormat.Format.Length == 0 && style.NumberFormat.NumberFormatId == 0)
Style.NumberFormat.NumberFormatId = 46;

break;
Expand Down Expand Up @@ -2311,15 +2311,15 @@ private void SetDateTimeFormat(XLStyleValue style, Boolean onlyDatePart)
{
_dataType = XLDataType.DateTime;

if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0)
if (style.NumberFormat.Format.Length == 0 && style.NumberFormat.NumberFormatId == 0)
Style.NumberFormat.NumberFormatId = onlyDatePart ? 14 : 22;
}

private void SetTimeSpanFormat(XLStyleValue style)
{
_dataType = XLDataType.TimeSpan;

if (style.NumberFormat.Format == String.Empty && style.NumberFormat.NumberFormatId == 0)
if (style.NumberFormat.Format.Length == 0 && style.NumberFormat.NumberFormatId == 0)
Style.NumberFormat.NumberFormatId = 46;
}

Expand Down
2 changes: 1 addition & 1 deletion ClosedXML/XLHelper.cs
Expand Up @@ -277,7 +277,7 @@ private static string Evaluator(Match match, Int32 row, String column)
if (split.Length == 1) return column + row; // A1
if (split.Length == 3) return match.Groups["one"].Value; // $A$1
var a = XLAddress.Create(match.Groups["one"].Value);
if (split[0] == String.Empty) return "$" + a.ColumnLetter + row; // $A1
if (split[0].Length == 0) return "$" + a.ColumnLetter + row; // $A1
return column + "$" + a.RowNumber;
}

Expand Down
2 changes: 1 addition & 1 deletion ClosedXML_Tests/Excel/Ranges/MergedRangesTests.cs
Expand Up @@ -306,7 +306,7 @@ public void UnmergedCellsPreserveStyle()
range.Unmerge();

Assert.IsTrue(range.Cells().All(c => c.Style.Fill.BackgroundColor == XLColor.Red));
Assert.IsTrue(range.Cells().Where(c => c != firstCell).All(c => c.Value == ""));
Assert.IsTrue(range.Cells().Where(c => c != firstCell).All(c => c.GetString().Length == 0));
Assert.AreEqual("B2", firstCell.Value);

Assert.AreEqual(XLBorderStyleValues.Thick, ws.Cell("B2").Style.Border.TopBorder);
Expand Down

0 comments on commit 8c1b7ad

Please sign in to comment.