lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [lucenenet] 07/11: Lucene.Net.Util.BytesRef: Added BytesRefFormatter struct used to defer building a string.Format() parameter when using Debugging.Assert<T0>(bool, string, T0) overloads
Date Tue, 03 Nov 2020 21:24:19 GMT
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 fb7fb6bf7e124826ce7c6314037b50fee59a721e
Author: Shad Storhaug <shad@shadstorhaug.com>
AuthorDate: Tue Nov 3 10:15:11 2020 +0700

    Lucene.Net.Util.BytesRef: Added BytesRefFormatter struct used to defer building a string.Format()
parameter when using Debugging.Assert<T0>(bool, string, T0) overloads
---
 src/Lucene.Net/Util/BytesRef.cs | 49 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/src/Lucene.Net/Util/BytesRef.cs b/src/Lucene.Net/Util/BytesRef.cs
index 75cfa6d..b3db4c8 100644
--- a/src/Lucene.Net/Util/BytesRef.cs
+++ b/src/Lucene.Net/Util/BytesRef.cs
@@ -484,4 +484,53 @@ namespace Lucene.Net.Util
             return a.Length - b.Length;
         }
     }
+
+    // LUCENENET specific
+    internal enum BytesRefFormat // For assert/test/logging
+    {
+        UTF8,
+        UTF8AsHex
+    }
+
+    // LUCENENET specific - when this object is a parameter of 
+    // a method that calls string.Format(),
+    // defers execution of building a string until
+    // string.Format() is called.
+    // This struct is meant to wrap a directory parameter when passed as a string.Format()
argument.
+    internal struct BytesRefFormatter // For assert/test/logging
+    {
+#pragma warning disable IDE0044 // Add readonly modifier
+        private BytesRef bytesRef;
+        private BytesRefFormat format;
+#pragma warning restore IDE0044 // Add readonly modifier
+        public BytesRefFormatter(BytesRef bytesRef, BytesRefFormat format)
+        {
+            this.bytesRef = bytesRef; // Allow null
+            this.format = format;
+        }
+
+        public override string ToString()
+        {
+            // Special case: null
+            if (bytesRef is null)
+                return "null";
+
+            switch (format)
+            {
+                case BytesRefFormat.UTF8:
+                    try
+                    {
+                        return bytesRef.Utf8ToString();
+                    }
+                    catch (IndexOutOfRangeException)
+                    {
+                        return bytesRef.ToString();
+                    }
+                case BytesRefFormat.UTF8AsHex:
+                    return UnicodeUtil.ToHexString(bytesRef.Utf8ToString());
+                default:
+                    return bytesRef.ToString();
+            }
+        }
+    }
 }
\ No newline at end of file


Mime
View raw message