Skip to content

Commit

Permalink
SWEEP: Reviewed all catch blocks where Lucene uses NumberFormatExcept…
Browse files Browse the repository at this point in the history
…ion and converted them to use our IsNumberFormatException() extension method (see apache#446).
  • Loading branch information
NightOwl888 committed Apr 14, 2021
1 parent 9ae07ac commit 2219698
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/Lucene.Net.Benchmark/ByTask/Tasks/AnalyzerFactoryTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public override void SetParams(string @params)
{
intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture);
}
catch (Exception e) when (e.IsParseException())
catch (Exception e) when (e.IsNumberFormatException())
{
throw new Exception
("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e);
Expand Down Expand Up @@ -204,7 +204,7 @@ public override void SetParams(string @params)
{
intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture);
}
catch (Exception e) when (e.IsParseException())
catch (Exception e) when (e.IsNumberFormatException())
{
throw new Exception
("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e);
Expand Down Expand Up @@ -257,7 +257,7 @@ public override void SetParams(string @params)
{
intArgValue = int.Parse(stok.StringValue.Trim(), CultureInfo.InvariantCulture);
}
catch (Exception e) when (e.IsParseException())
catch (Exception e) when (e.IsNumberFormatException())
{
throw new Exception
("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + stok.StringValue + "'", e);
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Benchmark/Quality/QualityQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public virtual int CompareTo(QualityQuery other)
int nOther = int.Parse(other.queryID, CultureInfo.InvariantCulture);
return n - nOther;
}
catch (Exception e) when (e.IsParseException())
catch (Exception e) when (e.IsNumberFormatException())
{
// fall back to string comparison
return queryID.CompareToOrdinal(other.queryID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public virtual Filter GetFilter(XmlElement e)
}
return filter;
}
catch (FormatException nfe)
catch (Exception nfe) when (nfe.IsNumberFormatException())
{
if (strictMode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public virtual Query GetQuery(XmlElement e)
}
return filter;
}
catch (FormatException nfe)
catch (Exception nfe) when (nfe.IsNumberFormatException())
{
throw new ParserException("Could not parse lowerTerm or upperTerm into a number", nfe);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Lucene.Net.Suggest/Suggest/FileDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal void ReadWeight(string weight)
// keep reading floats for bw compat
curWeight = (long)double.Parse(weight, NumberStyles.Float, CultureInfo.InvariantCulture);
}
catch (FormatException e)
catch (Exception e) when (e.IsNumberFormatException())
{
// LUCENENET: This is just so we can see what string and what culture was being tested when parsing failed,
// to try to reproduce the conditions of the failure.
Expand Down
16 changes: 9 additions & 7 deletions src/Lucene.Net.TestFramework/Search/CheckHits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,17 +403,18 @@ public static void VerifyExplanation(string q, int doc, float score, bool deep,

// LUCENENET NOTE: Using current culture here is intentional because
// we are parsing from text that was made using the current culture.
if (float.TryParse(descr.Substring(k1, k2 - k1).Trim(), out x))
if (float.TryParse(descr.Substring(k1, k2 - k1).Trim(), out x) &&
descr.Substring(k2).Trim().Equals("times others of:", StringComparison.Ordinal))
{
if (descr.Substring(k2).Trim().Equals("times others of:", StringComparison.Ordinal))
{
maxTimesOthers = true;
}
maxTimesOthers = true;
}
}
}
// TODO: this is a TERRIBLE assertion!!!!
Assert.IsTrue(productOf || sumOf || maxOf || maxTimesOthers, q + ": multi valued explanation description=\"" + descr + "\" must be 'max of plus x times others' or end with 'product of'" + " or 'sum of:' or 'max of:' - " + expl);
Assert.IsTrue(productOf || sumOf || maxOf || maxTimesOthers,
q + ": multi valued explanation description=\"" + descr
+ "\" must be 'max of plus x times others' or end with 'product of'"
+ " or 'sum of:' or 'max of:' - " + expl);
float sum = 0;
float product = 1;
float max = 0;
Expand Down Expand Up @@ -446,7 +447,8 @@ public static void VerifyExplanation(string q, int doc, float score, bool deep,
{
Assert.IsTrue(false, "should never get here!");
}
Assert.AreEqual(combined, value, ExplainToleranceDelta(combined, value), q + ": actual subDetails combined==" + combined + " != value=" + value + " Explanation: " + expl);
Assert.AreEqual(combined, value, ExplainToleranceDelta(combined, value),
q + ": actual subDetails combined==" + combined + " != value=" + value + " Explanation: " + expl);
}
}
}
Expand Down
20 changes: 6 additions & 14 deletions src/Lucene.Net.Tests/Util/TestArrayUtil.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Lucene.Net.Support;
using Lucene.Net.Support;
using NUnit.Framework;
using System;
using Assert = Lucene.Net.TestFramework.Assert;
Expand Down Expand Up @@ -82,9 +82,7 @@ public virtual void TestParseInt()
test = ArrayUtil.ParseInt32("".ToCharArray());
Assert.IsTrue(false);
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
//expected
}
Expand All @@ -93,9 +91,7 @@ public virtual void TestParseInt()
test = ArrayUtil.ParseInt32("foo".ToCharArray());
Assert.IsTrue(false);
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
//expected
}
Expand All @@ -104,9 +100,7 @@ public virtual void TestParseInt()
test = ArrayUtil.ParseInt32(Convert.ToString(long.MaxValue).ToCharArray());
Assert.IsTrue(false);
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
//expected
}
Expand All @@ -115,9 +109,7 @@ public virtual void TestParseInt()
test = ArrayUtil.ParseInt32("0.34".ToCharArray());
Assert.IsTrue(false);
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
//expected
}
Expand All @@ -135,7 +127,7 @@ public virtual void TestParseInt()
test = ArrayUtil.ParseInt32("foo 1923 bar".ToCharArray(), 4, 4);
Assert.IsTrue(test == 1923, test + " does not equal: " + 1923);
}
catch (FormatException e)
catch (Exception e) when (e.IsNumberFormatException())
{
Console.WriteLine(e.ToString());
Console.Write(e.StackTrace);
Expand Down
8 changes: 2 additions & 6 deletions src/Lucene.Net.Tests/Util/TestNumericUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public virtual void TestLongSpecialValues()
NumericUtils.PrefixCodedToInt32(prefixVals[i]);
Assert.Fail("decoding a prefix coded long value as int should fail");
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
// worked
}
Expand Down Expand Up @@ -142,9 +140,7 @@ public virtual void TestIntSpecialValues()
NumericUtils.PrefixCodedToInt64(prefixVals[i]);
Assert.Fail("decoding a prefix coded int value as long should fail");
}
#pragma warning disable 168
catch (FormatException e)
#pragma warning restore 168
catch (Exception e) when (e.IsNumberFormatException())
{
// worked
}
Expand Down
8 changes: 4 additions & 4 deletions src/Lucene.Net/Search/FieldCacheImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ protected override FieldCache.Int32s CreateValue(AtomicReader reader, CacheKey<F
return wrapper.GetInt32s(reader, key.field, FieldCache.DEFAULT_INT32_PARSER, setDocsWithField);
#pragma warning restore 612, 618
}
catch (FormatException)
catch (Exception ne) when (ne.IsNumberFormatException())
{
return wrapper.GetInt32s(reader, key.field, FieldCache.NUMERIC_UTILS_INT32_PARSER, setDocsWithField);
}
Expand Down Expand Up @@ -1307,7 +1307,7 @@ protected override FieldCache.Singles CreateValue(AtomicReader reader, CacheKey<
return wrapper.GetSingles(reader, key.field, FieldCache.DEFAULT_SINGLE_PARSER, setDocsWithField);
#pragma warning restore 612, 618
}
catch (FormatException)
catch (Exception ne) when (ne.IsNumberFormatException())
{
return wrapper.GetSingles(reader, key.field, FieldCache.NUMERIC_UTILS_SINGLE_PARSER, setDocsWithField);
}
Expand Down Expand Up @@ -1475,7 +1475,7 @@ protected override FieldCache.Int64s CreateValue(AtomicReader reader, CacheKey<F
return wrapper.GetInt64s(reader, key.field, FieldCache.DEFAULT_INT64_PARSER, setDocsWithField);
#pragma warning restore 612, 618
}
catch (FormatException)
catch (Exception ne) when (ne.IsNumberFormatException())
{
return wrapper.GetInt64s(reader, key.field, FieldCache.NUMERIC_UTILS_INT64_PARSER, setDocsWithField);
}
Expand Down Expand Up @@ -1645,7 +1645,7 @@ protected override FieldCache.Doubles CreateValue(AtomicReader reader, CacheKey<
return wrapper.GetDoubles(reader, key.field, FieldCache.DEFAULT_DOUBLE_PARSER, setDocsWithField);
#pragma warning restore 612, 618
}
catch (FormatException)
catch (Exception ne) when (ne.IsNumberFormatException())
{
return wrapper.GetDoubles(reader, key.field, FieldCache.NUMERIC_UTILS_DOUBLE_PARSER, setDocsWithField);
}
Expand Down
52 changes: 25 additions & 27 deletions src/Lucene.Net/Util/Automaton/RegExp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,37 +1289,35 @@ internal RegExp ParseSimpleExp()
{
throw new ArgumentException("illegal identifier at position " + (pos - 1));
}
try

// LUCENENET: Refactored so we don't throw exceptions in the normal flow
if (i == 0 || i == s.Length - 1 || i != s.LastIndexOf('-'))
{
if (i == 0 || i == s.Length - 1 || i != s.LastIndexOf('-'))
{
throw new FormatException();
}
string smin = s.Substring(0, i);
string smax = s.Substring(i + 1, s.Length - (i + 1));
int imin = Convert.ToInt32(smin, CultureInfo.InvariantCulture);
int imax = Convert.ToInt32(smax, CultureInfo.InvariantCulture);
int digits;
if (smin.Length == smax.Length)
{
digits = smin.Length;
}
else
{
digits = 0;
}
if (imin > imax)
{
int t = imin;
imin = imax;
imax = t;
}
return MakeInterval(imin, imax, digits);
throw new ArgumentException("interval syntax error at position " + (pos - 1));
}
string smin = s.Substring(0, i);
string smax = s.Substring(i + 1, s.Length - (i + 1));

if (!int.TryParse(smin, NumberStyles.Integer, CultureInfo.InvariantCulture, out int imin) ||
!int.TryParse(smax, NumberStyles.Integer, CultureInfo.InvariantCulture, out int imax))
throw new ArgumentException("interval syntax error at position " + (pos - 1));

int digits;
if (smin.Length == smax.Length)
{
digits = smin.Length;
}
else
{
digits = 0;
}
catch (FormatException e)
if (imin > imax)
{
throw new ArgumentException("interval syntax error at position " + (pos - 1), e);
int t = imin;
imin = imax;
imax = t;
}
return MakeInterval(imin, imax, digits);
}
}
else
Expand Down

0 comments on commit 2219698

Please sign in to comment.