Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,6 @@
<NoWarn>$(NoWarn);CA1862</NoWarn> <!-- Prefer the string comparison method overload -->
<NoWarn>$(NoWarn);CA1864</NoWarn> <!-- To avoid double lookup, call 'TryAdd' instead of calling 'Add' with a 'ContainsKey' -->
<NoWarn>$(NoWarn);CA1853</NoWarn> <!-- Do not guard 'Dictionary.Remove(key)' with 'Dictionary.ContainsKey(key)' -->
<NoWarn>$(NoWarn);CA1865</NoWarn> <!-- Use 'string.EndsWith(char)' instead of 'string.EndsWith(string)' when you have a string with a single char -->
<NoWarn>$(NoWarn);CA1866</NoWarn> <!-- Use 'string.IndexOf(char)' instead of 'string.IndexOf(string)' when you have a string with a single char -->
<NoWarn>$(NoWarn);CA1872</NoWarn> <!-- Prefer 'System.Convert.ToHexString(byte[])' over call chains based on 'System.BitConverter.ToString(byte[])' -->
<NoWarn>$(NoWarn);CA2019</NoWarn> <!-- 'ThreadStatic' fields should not use inline initialization -->
<NoWarn>$(NoWarn);CA2022</NoWarn> <!-- Avoid inexact read -->
Expand All @@ -109,8 +107,6 @@
<NoWarn>$(NoWarn);CA2211</NoWarn> <!-- Non-constant fields should not be visible -->
<NoWarn>$(NoWarn);CA2219</NoWarn> <!-- Do not raise an exception from within a finally clause -->
<NoWarn>$(NoWarn);CA2241</NoWarn> <!-- Provide correct arguments to formatting methods -->
<NoWarn>$(NoWarn);CA2249</NoWarn> <!-- Use 'string.Contains' instead of 'string.IndexOf' -->
<NoWarn>$(NoWarn);CA2251</NoWarn> <!-- Use 'string.Equals' instead of comparing the result of 'string.Compare' to 0 -->
<NoWarn>$(NoWarn);CA2263</NoWarn> <!-- Prefer the generic overload 'System.Enum.GetValues<TEnum>()' instead of 'System.Enum.GetValues(System.Type)' -->
<NoWarn>$(NoWarn);CA3075</NoWarn> <!-- Unsafe overload of 'LoadXml' method -->
<NoWarn>$(NoWarn);CA5351</NoWarn> <!-- broken cryptographic algorithm -->
Expand Down
5 changes: 5 additions & 0 deletions OpenXmlFormats/NPOI.OpenXmlFormats.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@
<ProjectReference Include="..\main\NPOI.Core.csproj" />
<ProjectReference Include="..\openxml4Net\NPOI.OpenXml4Net.Core.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\main\Polyfills.cs" Link="Polyfills.cs" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions OpenXmlFormats/Spreadsheet/SharedString/CT_Rst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public string XmlText
if (r.t != null)
{
sw.Write("<t");
if(r.t.IndexOf(' ')>=0)
if(r.t.Contains(' '))
sw.Write(" xml:space=\"preserve\"");
sw.Write(">");
sw.Write(XmlHelper.EncodeXml(r.t));
Expand All @@ -180,7 +180,7 @@ public string XmlText
if (this.t != null)
{
sw.Write("<t");
if (this.t.IndexOf(' ') >= 0)
if (t.Contains(' '))
sw.Write(" xml:space=\"preserve\"");
sw.Write(">");
sw.Write(XmlHelper.EncodeXml(this.t));
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/Extractor/EventBasedExcelExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public override String Text
TextListener tl = TriggerExtraction();

text = tl.text.ToString();
if (!text.EndsWith("\n", StringComparison.Ordinal))
if (!text.EndsWith('\n'))
{
text = text + "\n";
}
Expand Down
4 changes: 2 additions & 2 deletions main/HSSF/UserModel/DVConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,11 +527,11 @@ internal static DVConstraint CreateDVConstraint(DVRecord dvRecord, IFormulaRende
if (dvRecord.ListExplicitFormula)
{
String values = toFormulaString(dvRecord.Formula1, book).AsString();
if (values.StartsWith("\""))
if (values.StartsWith('"'))
{
values = values.Substring(1);
}
if (values.EndsWith("\""))
if (values.EndsWith('"'))
{
values = values.Substring(0, values.Length - 1);
}
Expand Down
4 changes: 2 additions & 2 deletions main/HSSF/UserModel/HSSFName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ thus we are stuck with Character.isLetter (for now).
// is first character valid?
char c = name[0];
String allowedSymbols = "_\\";
bool characterIsValid = (char.IsLetter(c) || allowedSymbols.IndexOf(c) != -1);
bool characterIsValid = (char.IsLetter(c) || allowedSymbols.Contains(c));
if (!characterIsValid)
{
throw new ArgumentException("Invalid name: '" + name + "': first character must be underscore or a letter");
Expand All @@ -185,7 +185,7 @@ thus we are stuck with Character.isLetter (for now).
allowedSymbols = "_.\\"; //backslashes needed for unicode escape
foreach (char ch in name.ToCharArray())
{
characterIsValid = (char.IsLetterOrDigit(ch) || allowedSymbols.IndexOf(ch) != -1);
characterIsValid = (char.IsLetterOrDigit(ch) || allowedSymbols.Contains(ch));
if (!characterIsValid)
{
throw new ArgumentException("Invalid name: '" + name + "': name must be letter, digit, period, or underscore");
Expand Down
2 changes: 1 addition & 1 deletion main/HSSF/UserModel/HSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ private String GetUniqueSheetName(String srcName)
int uniqueIndex = 2;
String baseName = srcName;
int bracketPos = srcName.LastIndexOf('(');
if (bracketPos > 0 && srcName.EndsWith(")", StringComparison.Ordinal))
if (bracketPos > 0 && srcName.EndsWith(')'))
{
String suffix = srcName.Substring(bracketPos + 1, srcName.Length - bracketPos - 2);
try
Expand Down
12 changes: 6 additions & 6 deletions main/HSSF/UserModel/HeaderFooter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,35 @@ private String[] SplitParts()
switch (text[1])
{
case 'L':
if (text.IndexOf("&C", StringComparison.Ordinal) >= 0)
if (text.Contains("&C"))
{
pos = Math.Min(pos, text.IndexOf("&C", StringComparison.Ordinal));
}
if (text.IndexOf("&R", StringComparison.Ordinal) >= 0)
if (text.Contains("&R"))
{
pos = Math.Min(pos, text.IndexOf("&R", StringComparison.Ordinal));
}
_left = text.Substring(2, pos - 2);
text = text.Substring(pos);
break;
case 'C':
if (text.IndexOf("&L", StringComparison.Ordinal) >= 0)
if (text.Contains("&L"))
{
pos = Math.Min(pos, text.IndexOf("&L", StringComparison.Ordinal));
}
if (text.IndexOf("&R", StringComparison.Ordinal) >= 0)
if (text.Contains("&R"))
{
pos = Math.Min(pos, text.IndexOf("&R", StringComparison.Ordinal));
}
_center = text.Substring(2, pos - 2);
text = text.Substring(pos);
break;
case 'R':
if (text.IndexOf("&C", StringComparison.Ordinal) >= 0)
if (text.Contains("&C"))
{
pos = Math.Min(pos, text.IndexOf("&C", StringComparison.Ordinal));
}
if (text.IndexOf("&L", StringComparison.Ordinal) >= 0)
if (text.Contains("&L"))
{
pos = Math.Min(pos, text.IndexOf("&L", StringComparison.Ordinal));
}
Expand Down
1 change: 0 additions & 1 deletion main/NPOI.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.0;netstandard2.1;net472;net8.0</TargetFrameworks>
<TargetFrameworks>net472;netstandard2.0;netstandard2.1;net8.0</TargetFrameworks>
<RootNamespace>NPOI</RootNamespace>
<SignAssembly>true</SignAssembly>
Expand Down
23 changes: 23 additions & 0 deletions main/Polyfills.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace NPOI
{
internal static class Polyfills
{
#if NETFRAMEWORK || NETSTANDARD2_0
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
internal static bool Contains(this string source, char c) => source.IndexOf(c) != -1;

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
internal static bool StartsWith(this string source, char c) => source.Length > 0 && source[0] == c;

[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
internal static bool EndsWith(this string source, char c) => source.Length > 0 && source[source.Length - 1] == c;
#endif

#if NETFRAMEWORK
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
internal static bool Contains(this ReadOnlySpan<string> source, string c) => source.IndexOf(c) != -1;
#endif
}
}
2 changes: 1 addition & 1 deletion main/SS/Format/CellFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private CellFormat(String format)
String valueDesc = m.Groups[0].Value;

// Strip out the semicolon if it's there
if (valueDesc.EndsWith(";"))
if (valueDesc.EndsWith(';'))
valueDesc = valueDesc.Substring(0, valueDesc.Length - 1);

parts.Add(new CellFormatPart(valueDesc));
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Format/CellFormatPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public static StringBuilder ParseFormat(String fdesc, CellFormatType type,
}
public static String QuoteReplacement(String s)
{
if ((s.IndexOf('\\') == -1) && (s.IndexOf('$') == -1))
if (!s.Contains('\\') && !s.Contains('$'))
return s;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.Length; i++)
Expand Down
6 changes: 3 additions & 3 deletions main/SS/Format/CellGeneralFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public override void FormatValue(StringBuilder toAppendTo, Object value)
{
// strip off trailing zeros
int RemoveFrom;
if (fmt.StartsWith("E"))
RemoveFrom = toAppendTo.ToString().LastIndexOf("E") - 1;
if (fmt.StartsWith('E'))
RemoveFrom = toAppendTo.ToString().LastIndexOf('E') - 1;
else
RemoveFrom = toAppendTo.Length - 1;
while (toAppendTo[RemoveFrom] == '0')
Expand All @@ -89,7 +89,7 @@ public override void FormatValue(StringBuilder toAppendTo, Object value)
}
// remove zeros after E by antony.liu
string text = toAppendTo.ToString();
RemoveFrom = toAppendTo.ToString().LastIndexOf("E");
RemoveFrom = toAppendTo.ToString().LastIndexOf('E');
if (RemoveFrom > 0)
{
RemoveFrom++;
Expand Down
11 changes: 6 additions & 5 deletions main/SS/Format/CellNumberFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ private void WriteScientific(double value, StringBuilder output, SortedList<Cell

// (2) Determine the result's sign.
string tmp = result.ToString();
int ePos = tmp.IndexOf("E");// fractionPos.EndIndex;
int ePos = tmp.IndexOf('E');// fractionPos.EndIndex;
int signPos = ePos + 1;
char expSignRes = result[signPos];

Expand Down Expand Up @@ -941,11 +941,11 @@ private void Writeint(StringBuilder result, StringBuilder output,
bool ShowCommas)
{

int pos = result.ToString().IndexOf(".") - 1;
int pos = result.ToString().IndexOf('.') - 1;
if (pos < 0)
{
if (exponent != null && numSpecials == integerSpecials)
pos = result.ToString().IndexOf("E") - 1;
pos = result.ToString().IndexOf('E') - 1;
else
pos = result.Length - 1;
}
Expand Down Expand Up @@ -1017,9 +1017,10 @@ private void WriteFractional(StringBuilder result, StringBuilder output)
int strip;
if (fractionalSpecials.Count > 0)
{
digit = result.ToString().IndexOf(".") + 1;
string resultString = result.ToString();
digit = resultString.IndexOf('.') + 1;
if (exponent != null)
strip = result.ToString().IndexOf("E") - 1;
strip = resultString.IndexOf('E') - 1;
else
strip = result.Length - 1;
while (strip > digit && result[strip] == '0')
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Format/CellTextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public CellTextFormatter(String format)
int pos = desc.Length - 1;
for (int i = 0; i < textPos.Length; i++)
{
textPos[i] = desc.LastIndexOf("\u0000", pos);
textPos[i] = desc.LastIndexOf('\u0000', pos);
pos = textPos[i] - 1;
}
}
Expand Down
4 changes: 2 additions & 2 deletions main/SS/Formula/EvaluationConditionalFormatRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static bool IsValid(OperatorEnum @operator, object cellValue, object v1,
}
else if (cellValue is String equalString)
{
return string.Compare(equalString, (String)v1, true) == 0;
return string.Equals(equalString, (String)v1, StringComparison.Ordinal);
}
else if (cellValue is Boolean b)
{
Expand All @@ -70,7 +70,7 @@ public static bool IsValid(OperatorEnum @operator, object cellValue, object v1,
if (cellValue is String s)
{
String n1 = (String)v1;
return string.Compare(s, n1, true) != 0;
return !string.Equals(s, n1, StringComparison.Ordinal);
}
else if (cellValue is Boolean b)
{
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Formula/Functions/Bin2Dec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override ValueEval Evaluate(int srcRowIndex, int srcColumnIndex, ValueEva
else
{
unsigned = number.Substring(1);
isPositive = number.StartsWith("0");
isPositive = number.StartsWith('0');
}

String value;
Expand Down
12 changes: 6 additions & 6 deletions main/SS/Formula/Functions/DStarRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ private static bool testNormalCondition(ValueEval value, ValueEval condition)
if (condition is StringEval eval) {
String conditionString = eval.StringValue;

if (conditionString.StartsWith("<"))
if (conditionString.StartsWith('<'))
{ // It's a </<= condition.
String number = conditionString.Substring(1);
if(number.StartsWith("="))
if(number.StartsWith('='))
{
number = number.Substring(1);
return testNumericCondition(value, Operator.smallerEqualThan, number);
}
else if(number.StartsWith(">"))
else if(number.StartsWith('>'))
{
number = number.Substring(1);
bool itsANumber = IsNumber(number);
Expand All @@ -355,10 +355,10 @@ private static bool testNormalCondition(ValueEval value, ValueEval condition)
return testNumericCondition(value, Operator.smallerThan, number);
}
}
else if (conditionString.StartsWith(">"))
else if (conditionString.StartsWith('>'))
{ // It's a >/>= condition.
String number = conditionString.Substring(1);
if (number.StartsWith("="))
if (number.StartsWith('='))
{
number = number.Substring(1);
return testNumericCondition(value, Operator.largerEqualThan, number);
Expand All @@ -368,7 +368,7 @@ private static bool testNormalCondition(ValueEval value, ValueEval condition)
return testNumericCondition(value, Operator.largerThan, number);
}
}
else if (conditionString.StartsWith("="))
else if (conditionString.StartsWith('='))
{ // It's a = condition.
String stringOrNumber = conditionString.Substring(1);

Expand Down
9 changes: 5 additions & 4 deletions main/SS/Formula/Functions/DateValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ private sealed class Format
public Format(string patternString, String groupOrder)
{
this.pattern = new Regex(patternString, RegexOptions.Compiled);
this.hasYear = groupOrder.Contains("y");
var idx = groupOrder.IndexOf('y');
this.hasYear = idx != -1;
if (hasYear)
{
yearIndex = groupOrder.IndexOf("y");
yearIndex = idx;
}
monthIndex = groupOrder.IndexOf("m");
dayIndex = groupOrder.IndexOf("d");
monthIndex = groupOrder.IndexOf('m');
dayIndex = groupOrder.IndexOf('d');
}
private static List<Format> formats = new List<Format>();
static Format()
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Formula/Functions/NumberValueFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public ValueEval Evaluate(ValueEval[] args, OperationEvaluationContext ec)
//Multiple percent signs are additive if they are used in the Text argument just as they are if they are used in a formula.
//For example, =NUMBERVALUE("9%%") returns the same result (0.0009) as the formula =9%%.
int countPercent = 0;
while (text.EndsWith("%"))
while (text.EndsWith('%'))
{
countPercent++;
text = text.Substring(0, text.Length - 1);
Expand Down
7 changes: 3 additions & 4 deletions main/SS/UserModel/DataFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,7 @@ private FormatBase GetFormat(double cellValue, int formatIndex, string formatStr
// handle these ourselves in a special way.
// For now, if we detect 3+ parts, we call out to CellFormat to handle it
// TODO Going forward, we should really merge the logic between the two classes
if (formatStr.IndexOf(';') != -1 &&
formatStr.IndexOf(';') != formatStr.LastIndexOf(';'))
if (formatStr.Contains(';') && formatStr.IndexOf(';') != formatStr.LastIndexOf(';'))
{
try
{
Expand Down Expand Up @@ -466,7 +465,7 @@ private FormatBase CreateFormat(double cellValue, int formatIndex, string sForma
}

// Excel supports fractions in format strings, which Java doesn't
if (formatStr.IndexOf("#/") >= 0 || formatStr.IndexOf("?/") >= 0)
if (formatStr.Contains("#/") || formatStr.Contains("?/"))
{
String[] chunks = formatStr.Split(";".ToCharArray());
for (int i = 0; i < chunks.Length; i++)
Expand Down Expand Up @@ -908,7 +907,7 @@ private string GetFormattedNumberString(ICell cell)
}
//return numberFormat.Format(d, currentCulture);
string formatted = numberFormat.Format(d);
if (formatted.StartsWith("."))
if (formatted.StartsWith('.'))
formatted = "0" + formatted;
if (formatted.StartsWith("-."))
formatted = "-0" + formatted.Substring(1);
Expand Down
Loading