You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2021/07/24 19:00:39 UTC

[lucenenet] 01/04: Lucene.Net.Support: Factored out Number class in favor of using J2N's parsers and formatters

This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 40ca6f8cd5cd2ac9062f713a5bd208ca3d560120
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Thu Jul 8 15:30:01 2021 +0700

    Lucene.Net.Support: Factored out Number class in favor of using J2N's parsers and formatters
---
 .../VectorHighlight/FieldFragList.cs               |   7 +-
 .../VectorHighlight/FieldPhraseList.cs             |   5 +-
 .../Surround/Query/SrndQuery.cs                    |   4 +-
 src/Lucene.Net.Sandbox/Queries/SlowFuzzyQuery.cs   |   5 +-
 src/Lucene.Net.Tests/Support/TestToStringUtils.cs  |   4 +-
 src/Lucene.Net/Index/SegmentInfos.cs               |   4 +-
 .../Search/Similarities/LMDirichletSimilarity.cs   |   7 +-
 .../Similarities/LMJelinekMercerSimilarity.cs      |   7 +-
 src/Lucene.Net/Support/Number.cs                   | 115 ---------------------
 src/Lucene.Net/Util/ToStringUtils.cs               |   3 +-
 10 files changed, 28 insertions(+), 133 deletions(-)

diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/FieldFragList.cs b/src/Lucene.Net.Highlighter/VectorHighlight/FieldFragList.cs
index 3c44972..6dce798 100644
--- a/src/Lucene.Net.Highlighter/VectorHighlight/FieldFragList.cs
+++ b/src/Lucene.Net.Highlighter/VectorHighlight/FieldFragList.cs
@@ -1,6 +1,6 @@
-using Lucene.Net.Support;
-using System.Collections.Generic;
+using System.Collections.Generic;
 using System.Text;
+using Float = J2N.Numerics.Single;
 using Toffs = Lucene.Net.Search.VectorHighlight.FieldPhraseList.WeightedPhraseInfo.Toffs;
 using WeightedPhraseInfo = Lucene.Net.Search.VectorHighlight.FieldPhraseList.WeightedPhraseInfo;
 
@@ -86,7 +86,8 @@ namespace Lucene.Net.Search.VectorHighlight
                 sb.Append("subInfos=(");
                 foreach (SubInfo si in subInfos)
                     sb.Append(si.ToString());
-                sb.Append(")/").Append(Number.ToString(totalBoost)).Append('(').Append(startOffset).Append(',').Append(endOffset).Append(')');
+                // LUCENENET: intentionally using current culture here
+                sb.Append(")/").Append(Float.ToString(totalBoost)).Append('(').Append(startOffset).Append(',').Append(endOffset).Append(')');
                 return sb.ToString();
             }
 
diff --git a/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs b/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs
index f731181..5ec2f99 100644
--- a/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs
+++ b/src/Lucene.Net.Highlighter/VectorHighlight/FieldPhraseList.cs
@@ -1,9 +1,9 @@
 using J2N.Numerics;
-using Lucene.Net.Support;
 using Lucene.Net.Util;
 using System;
 using System.Collections.Generic;
 using System.Text;
+using Float = J2N.Numerics.Single;
 using QueryPhraseMap = Lucene.Net.Search.VectorHighlight.FieldQuery.QueryPhraseMap;
 using TermInfo = Lucene.Net.Search.VectorHighlight.FieldTermStack.TermInfo;
 
@@ -379,7 +379,8 @@ namespace Lucene.Net.Search.VectorHighlight
             public override string ToString()
             {
                 StringBuilder sb = new StringBuilder();
-                sb.Append(GetText()).Append('(').Append(Lucene.Net.Support.Number.ToString(boost)).Append(")(");
+                // LUCENENET: intentionally using current culture here
+                sb.Append(GetText()).Append('(').Append(Float.ToString(boost)).Append(")(");
                 foreach (Toffs to in termsOffsets)
                 {
                     sb.Append(to);
diff --git a/src/Lucene.Net.QueryParser/Surround/Query/SrndQuery.cs b/src/Lucene.Net.QueryParser/Surround/Query/SrndQuery.cs
index 1f62f4b..aa07cae 100644
--- a/src/Lucene.Net.QueryParser/Surround/Query/SrndQuery.cs
+++ b/src/Lucene.Net.QueryParser/Surround/Query/SrndQuery.cs
@@ -1,7 +1,9 @@
 using Lucene.Net.Search;
 using Lucene.Net.Support;
 using System;
+using System.Globalization;
 using System.Text;
+using Float = J2N.Numerics.Single;
 
 namespace Lucene.Net.QueryParsers.Surround.Query
 {
@@ -44,7 +46,7 @@ namespace Lucene.Net.QueryParsers.Surround.Query
             }
         }
 
-        public virtual string WeightString => Number.ToString(Weight);
+        public virtual string WeightString => Float.ToString(Weight, NumberFormatInfo.InvariantInfo);
 
         public virtual string WeightOperator => "^";
 
diff --git a/src/Lucene.Net.Sandbox/Queries/SlowFuzzyQuery.cs b/src/Lucene.Net.Sandbox/Queries/SlowFuzzyQuery.cs
index 9733c23..d6f585d 100644
--- a/src/Lucene.Net.Sandbox/Queries/SlowFuzzyQuery.cs
+++ b/src/Lucene.Net.Sandbox/Queries/SlowFuzzyQuery.cs
@@ -1,11 +1,12 @@
 using J2N;
 using Lucene.Net.Index;
 using Lucene.Net.Search;
-using Lucene.Net.Support;
 using Lucene.Net.Util;
 using Lucene.Net.Util.Automaton;
 using System;
+using System.Globalization;
 using System.Text;
+using Float = J2N.Numerics.Single;
 
 namespace Lucene.Net.Sandbox.Queries
 {
@@ -165,7 +166,7 @@ namespace Lucene.Net.Sandbox.Queries
             }
             buffer.Append(m_term.Text);
             buffer.Append('~');
-            buffer.Append(Number.ToString(minimumSimilarity));
+            buffer.Append(Float.ToString(minimumSimilarity, NumberFormatInfo.InvariantInfo));
             buffer.Append(ToStringUtils.Boost(Boost));
             return buffer.ToString();
         }
diff --git a/src/Lucene.Net.Tests/Support/TestToStringUtils.cs b/src/Lucene.Net.Tests/Support/TestToStringUtils.cs
index 00c8412..d6869a0 100644
--- a/src/Lucene.Net.Tests/Support/TestToStringUtils.cs
+++ b/src/Lucene.Net.Tests/Support/TestToStringUtils.cs
@@ -1,4 +1,4 @@
-using Lucene.Net.Attributes;
+using Lucene.Net.Attributes;
 using Lucene.Net.Util;
 using NUnit.Framework;
 using System.Globalization;
@@ -73,7 +73,7 @@ namespace Lucene.Net.Support
                 assertEquals("", ToStringUtils.Boost(boostNormal));
                 assertEquals("^2.5", ToStringUtils.Boost(boostFractional));
                 assertEquals("^5.0", ToStringUtils.Boost(boostNonFractional));
-                assertEquals("^1.111111", ToStringUtils.Boost(boostLong));
+                assertEquals("^1.1111112", ToStringUtils.Boost(boostLong)); // LUCENENET: Confirmed this is the value returned in Java 7
                 assertEquals("^0.0", ToStringUtils.Boost(boostZeroNonFractional));
                 assertEquals("^0.123", ToStringUtils.Boost(boostZeroFractional));
             }
diff --git a/src/Lucene.Net/Index/SegmentInfos.cs b/src/Lucene.Net/Index/SegmentInfos.cs
index 48fe143..3426bfd 100644
--- a/src/Lucene.Net/Index/SegmentInfos.cs
+++ b/src/Lucene.Net/Index/SegmentInfos.cs
@@ -12,6 +12,7 @@ using System.Runtime.ExceptionServices;
 using System.Text;
 using System.Threading;
 using JCG = J2N.Collections.Generic;
+using Long = J2N.Numerics.Int64;
 
 namespace Lucene.Net.Index
 {
@@ -266,7 +267,8 @@ namespace Lucene.Net.Index
             }
             else if (fileName.StartsWith(IndexFileNames.SEGMENTS, StringComparison.Ordinal))
             {
-                return Number.Parse(fileName.Substring(1 + IndexFileNames.SEGMENTS.Length), Character.MaxRadix);
+                // LUCENENET: Optimized parse so we don't allocate a substring
+                return Long.Parse(fileName, 1 + IndexFileNames.SEGMENTS.Length, fileName.Length - (1 + IndexFileNames.SEGMENTS.Length), Character.MaxRadix);
             }
             else
             {
diff --git a/src/Lucene.Net/Search/Similarities/LMDirichletSimilarity.cs b/src/Lucene.Net/Search/Similarities/LMDirichletSimilarity.cs
index c36b467..669ff2d 100644
--- a/src/Lucene.Net/Search/Similarities/LMDirichletSimilarity.cs
+++ b/src/Lucene.Net/Search/Similarities/LMDirichletSimilarity.cs
@@ -1,6 +1,6 @@
-using Lucene.Net.Support;
-using System;
+using System;
 using System.Globalization;
+using Float = J2N.Numerics.Single;
 
 namespace Lucene.Net.Search.Similarities
 {
@@ -99,7 +99,8 @@ namespace Lucene.Net.Search.Similarities
 
         public override string GetName()
         {
-            return "Dirichlet(" + Number.ToString(Mu) + ")";
+            // LUCENENET: Intentionally using current culture
+            return "Dirichlet(" + Float.ToString(Mu) + ")";
         }
     }
 }
\ No newline at end of file
diff --git a/src/Lucene.Net/Search/Similarities/LMJelinekMercerSimilarity.cs b/src/Lucene.Net/Search/Similarities/LMJelinekMercerSimilarity.cs
index 0f6f99a..2b66b52 100644
--- a/src/Lucene.Net/Search/Similarities/LMJelinekMercerSimilarity.cs
+++ b/src/Lucene.Net/Search/Similarities/LMJelinekMercerSimilarity.cs
@@ -1,6 +1,6 @@
-using Lucene.Net.Support;
-using System;
+using System;
 using System.Globalization;
+using Float = J2N.Numerics.Single;
 
 namespace Lucene.Net.Search.Similarities
 {
@@ -75,7 +75,8 @@ namespace Lucene.Net.Search.Similarities
 
         public override string GetName()
         {
-            return "Jelinek-Mercer(" + Number.ToString(Lambda) + ")";
+            // LUCENENET: Intentionally using current culture
+            return "Jelinek-Mercer(" + Float.ToString(Lambda) + ")";
         }
     }
 }
\ No newline at end of file
diff --git a/src/Lucene.Net/Support/Number.cs b/src/Lucene.Net/Support/Number.cs
deleted file mode 100644
index 9aae78d..0000000
--- a/src/Lucene.Net/Support/Number.cs
+++ /dev/null
@@ -1,115 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-using System;
-using System.Globalization;
-
-namespace Lucene.Net.Support
-{
-    /// <summary>
-    /// A simple class for number conversions.
-    /// </summary>
-    internal static class Number
-    {
-        /// <summary>
-        /// Min radix value.
-        /// </summary>
-        public const int MIN_RADIX = 2;
-
-        /// <summary>
-        /// Max radix value.
-        /// </summary>
-        public const int MAX_RADIX = 36;
-
-        /*public const int CHAR_MIN_CODE_POINT =
-        public const int CHAR_MAX_CODE_POINT = */
-
-        private const string digits = "0123456789abcdefghijklmnopqrstuvwxyz";
-
-        /// <summary>
-        /// Converts a number to System.String.
-        /// </summary>
-        /// <param name="f"></param>
-        /// <returns></returns>
-        public static System.String ToString(float f)
-        {
-            if (((float)(int)f) == f)
-            {
-                // Special case: When we have an integer value,
-                // the standard .NET formatting removes the decimal point
-                // and everything to the right. But we need to always
-                // have at least decimal place to match Lucene.
-                return f.ToString("0.0", CultureInfo.InvariantCulture);
-            }
-            else
-            {
-                // LUCENENET NOTE: Although the MSDN documentation says that 
-                // round-trip on float will be limited to 7 decimals, it appears
-                // not to be the case. Also, when specifying "0.0######", we only
-                // get a result to 6 decimal places maximum. So, we must round before
-                // doing a round-trip format to guarantee 7 decimal places.
-                return Math.Round(f, 7).ToString("R", CultureInfo.InvariantCulture);
-            }
-        }
-
-        /// <summary>
-        /// Parses a number in the specified radix.
-        /// </summary>
-        /// <param name="s">An input System.String.</param>
-        /// <param name="radix">A radix.</param>
-        /// <returns>The parsed number in the specified radix.</returns>
-        public static long Parse(System.String s, int radix)
-        {
-            if (s is null)
-            {
-                throw new ArgumentNullException(nameof(s));
-            }
-
-            if (radix < MIN_RADIX)
-            {
-                throw new NotSupportedException("radix " + radix +
-                                                " less than Number.MIN_RADIX");
-            }
-            if (radix > MAX_RADIX)
-            {
-                throw new NotSupportedException("radix " + radix +
-                                                " greater than Number.MAX_RADIX");
-            }
-
-            long result = 0;
-            long mult = 1;
-
-            s = s.ToLowerInvariant();
-
-            for (int i = s.Length - 1; i >= 0; i--)
-            {
-                int weight = digits.IndexOf(s[i]);
-                if (weight == -1)
-                    throw new FormatException("Invalid number for the specified radix");
-
-                result += (weight * mult);
-                mult *= radix;
-            }
-
-            return result;
-        }
-    }
-}
\ No newline at end of file
diff --git a/src/Lucene.Net/Util/ToStringUtils.cs b/src/Lucene.Net/Util/ToStringUtils.cs
index a53c093..e56c690 100644
--- a/src/Lucene.Net/Util/ToStringUtils.cs
+++ b/src/Lucene.Net/Util/ToStringUtils.cs
@@ -1,6 +1,7 @@
 using J2N.Numerics;
 using System.Globalization;
 using System.Text;
+using Float = J2N.Numerics.Single;
 
 namespace Lucene.Net.Util
 {
@@ -34,7 +35,7 @@ namespace Lucene.Net.Util
             if (boost != 1.0f)
             {
                 // .NET compatibility fix
-                return "^" + boost.ToString("0.0######", CultureInfo.InvariantCulture);
+                return "^" + Float.ToString(boost, CultureInfo.InvariantCulture);
             }
             else
                 return "";