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 2020/11/03 21:24:20 UTC

[lucenenet] 08/11: Lucene.Net.Codecs.BlockTreeTermsWriter: Added PendingBlocksFormatter struct used to defer building a string.Format() parameter when using Debugging.Assert(bool, string, T0) overloads

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 ff2b18d572d1008213ebc51eab8ad039d2d0f96f
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Tue Nov 3 10:21:38 2020 +0700

    Lucene.Net.Codecs.BlockTreeTermsWriter: Added PendingBlocksFormatter struct used to defer building a string.Format() parameter when using Debugging.Assert<T0>(bool, string, T0) overloads
---
 src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs | 70 ++++++++++++++++-----------
 1 file changed, 41 insertions(+), 29 deletions(-)

diff --git a/src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs b/src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs
index 29cb14b..a9da552 100644
--- a/src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs
+++ b/src/Lucene.Net/Codecs/BlockTreeTermsWriter.cs
@@ -437,41 +437,53 @@ namespace Lucene.Net.Codecs
             }
 
             // LUCENENET specific - to keep the Debug.Assert statement from throwing exceptions
-            // because of invalid UTF8 code in Prefix, we have a wrapper method that falls back
-            // to using PendingBlock.Prefix.ToString() if PendingBlock.ToString()
-            private string ToString(IList<PendingBlock> blocks) // For assert
+            // because of invalid UTF8 code in Prefix, we have a wrapper class that falls back
+            // to using PendingBlock.Prefix.ToString() if PendingBlock.ToString() errors.
+            // This struct defers formatting the string until it is actually used as a parameter
+            // in string.Format().
+            private struct PendingBlocksFormatter // For assert
             {
-                if (blocks == null)
-                    return "null";
+#pragma warning disable IDE0044 // Add readonly modifier
+                private IList<PendingBlock> blocks;
+#pragma warning restore IDE0044 // Add readonly modifier
+                public PendingBlocksFormatter(IList<PendingBlock> blocks)
+                {
+                    this.blocks = blocks; // May be null
+                }
 
+                public override string ToString() // For assert
+                {
+                    if (blocks == null)
+                        return "null";
 
-                if (blocks.Count == 0)
-                    return "[]";
+                    if (blocks.Count == 0)
+                        return "[]";
 
-                using (var it = blocks.GetEnumerator())
-                {
-                    StringBuilder sb = new StringBuilder();
-                    sb.Append('[');
-                    it.MoveNext();
-                    while (true)
+                    using (var it = blocks.GetEnumerator())
                     {
-                        var e = it.Current;
-                        // There is a chance that the Prefix will contain invalid UTF8,
-                        // so we catch that and use the alternative way of displaying it
-                        try
-                        {
-                            sb.Append(e.ToString());
-                        }
-                        catch (IndexOutOfRangeException)
-                        {
-                            sb.Append("BLOCK: ");
-                            sb.Append(e.Prefix.ToString());
-                        }
-                        if (!it.MoveNext())
+                        StringBuilder sb = new StringBuilder();
+                        sb.Append('[');
+                        it.MoveNext();
+                        while (true)
                         {
-                            return sb.Append(']').ToString();
+                            var e = it.Current;
+                            // There is a chance that the Prefix will contain invalid UTF8,
+                            // so we catch that and use the alternative way of displaying it
+                            try
+                            {
+                                sb.Append(e.ToString());
+                            }
+                            catch (IndexOutOfRangeException)
+                            {
+                                sb.Append("BLOCK: ");
+                                sb.Append(e.Prefix.ToString());
+                            }
+                            if (!it.MoveNext())
+                            {
+                                return sb.Append(']').ToString();
+                            }
+                            sb.Append(',').Append(' ');
                         }
-                        sb.Append(',').Append(' ');
                     }
                 }
             }
@@ -480,7 +492,7 @@ namespace Lucene.Net.Codecs
             {
                 // LUCENENET specific - we use a custom wrapper function to display floorBlocks, since
                 // it might contain garbage that cannot be converted into text.
-                if (Debugging.AssertsEnabled) Debugging.Assert((IsFloor && floorBlocks != null && floorBlocks.Count != 0) || (!IsFloor && floorBlocks == null), "isFloor={0} floorBlocks={1}", IsFloor , ToString(floorBlocks));
+                if (Debugging.AssertsEnabled) Debugging.Assert((IsFloor && floorBlocks != null && floorBlocks.Count != 0) || (!IsFloor && floorBlocks == null), "isFloor={0} floorBlocks={1}", IsFloor , new PendingBlocksFormatter(floorBlocks));
 
                 if (Debugging.AssertsEnabled) Debugging.Assert(scratchBytes.GetFilePointer() == 0);