lucenenet-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nightowl...@apache.org
Subject [35/58] [abbrv] lucenenet git commit: Added StringBuilder.AppendCodePoint() extension method to Core.Support.StringBuilderExtensions + added unit tests. Fixed buggy StringBuilder.Reverse() method to account for Unicode.
Date Thu, 10 Nov 2016 11:47:49 GMT
Added StringBuilder.AppendCodePoint() extension method to Core.Support.StringBuilderExtensions
+ added unit tests. Fixed buggy StringBuilder.Reverse() method to account for Unicode.


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/081ce8c3
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/081ce8c3
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/081ce8c3

Branch: refs/heads/master
Commit: 081ce8c35473e96025d5631ffa929f94847ecf18
Parents: 8af1037
Author: Shad Storhaug <shad@shadstorhaug.com>
Authored: Thu Nov 3 18:18:25 2016 +0700
Committer: Shad Storhaug <shad@shadstorhaug.com>
Committed: Tue Nov 8 02:24:53 2016 +0700

----------------------------------------------------------------------
 .../Support/StringBuilderExtensions.cs          | 57 +++++++++++++---
 src/Lucene.Net.Tests/Lucene.Net.Tests.csproj    |  1 +
 .../core/Support/TestStringBuilderExtensions.cs | 72 ++++++++++++++++++++
 3 files changed, 121 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/081ce8c3/src/Lucene.Net.Core/Support/StringBuilderExtensions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Support/StringBuilderExtensions.cs b/src/Lucene.Net.Core/Support/StringBuilderExtensions.cs
index 728ff60..8164939 100644
--- a/src/Lucene.Net.Core/Support/StringBuilderExtensions.cs
+++ b/src/Lucene.Net.Core/Support/StringBuilderExtensions.cs
@@ -1,4 +1,5 @@
-using System.Text;
+using System.Globalization;
+using System.Text;
 
 namespace Lucene.Net.Support
 {
@@ -6,20 +7,58 @@ namespace Lucene.Net.Support
     {
         public static StringBuilder Reverse(this StringBuilder text)
         {
-            if (text.Length > 1)
+            int textLength = text.Length;
+            if (textLength > 1)
             {
-                int pivotPos = text.Length / 2;
-                for (int i = 0; i < pivotPos; i++)
+                // Pull the string out of the StringBuilder so we
+                // can work with the various text elements (chars, glyphs, graphemes, etc)
+                // and reverse the order of the string without reversing chars that need
to be
+                // in a specific order to represent the same text as the forward string.
+                // Reference: http://stackoverflow.com/a/36310993/181087
+                int offset = textLength;
+                var enumerator = StringInfo.GetTextElementEnumerator(text.ToString());
+                while (enumerator.MoveNext())
                 {
-                    int iRight = text.Length - (i + 1);
-                    char rightChar = text[i];
-                    char leftChar = text[iRight];
-                    text[i] = leftChar;
-                    text[iRight] = rightChar;
+                    string element = enumerator.GetTextElement();
+
+                    // Back up the current offset by the length of the element
+                    offset -= element.Length;
+
+                    for (int i = 0; i < element.Length; i++)
+                    {
+                        // Write the chars in forward order from the element
+                        // to the StringBuilder based on the offset.
+                        text[i + offset] = element[i];
+                    }
                 }
             }
 
             return text;
         }
+
+        /// <summary>
+        /// Appends the string representation of the <paramref name="codePoint"/>
+        /// argument to this sequence.
+        /// 
+        /// <para>
+        /// The argument is appended to the contents of this sequence.
+        /// The length of this sequence increases by <see cref="Character.CharCount(int)"/>.
+        /// </para>
+        /// <para>
+        /// The overall effect is exactly as if the argument were
+        /// converted to a <see cref="char"/> array by the method
+        /// <see cref="Character.ToChars(int)"/> and the character in that array
+        /// were then <see cref="StringBuilder.Append(char[])">appended</see>
to this 
+        /// <see cref="StringBuilder"/>.
+        /// </para>
+        /// </summary>
+        /// <param name="text">This <see cref="StringBuilder"/>.</param>
+        /// <param name="codePoint">a Unicode code point</param>
+        /// <returns>a reference to this object.</returns>
+        public static StringBuilder AppendCodePoint(this StringBuilder text, int codePoint)
+        {
+            text.Append(Character.ToChars(codePoint));
+            return text;
+        }
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/081ce8c3/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
index 9e6377e..00a14dc 100644
--- a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
+++ b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
@@ -491,6 +491,7 @@
     <Compile Include="core\Support\TestLurchTableThreading.cs" />
     <Compile Include="core\Support\TestDataInputStream.cs" />
     <Compile Include="core\Support\TestDataOutputStream.cs" />
+    <Compile Include="core\Support\TestStringBuilderExtensions.cs" />
     <Compile Include="core\Support\TestTreeDictionary.cs" />
     <Compile Include="core\Support\TestTreeSet.cs" />
     <Compile Include="core\TestAssertions.cs">

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/081ce8c3/src/Lucene.Net.Tests/core/Support/TestStringBuilderExtensions.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Support/TestStringBuilderExtensions.cs b/src/Lucene.Net.Tests/core/Support/TestStringBuilderExtensions.cs
new file mode 100644
index 0000000..f7c23d0
--- /dev/null
+++ b/src/Lucene.Net.Tests/core/Support/TestStringBuilderExtensions.cs
@@ -0,0 +1,72 @@
+using Lucene.Net.Attributes;
+using NUnit.Framework;
+using System;
+using System.Text;
+
+namespace Lucene.Net.Support
+{
+    [TestFixture]
+    public class TestStringBuilderExtensions
+    {
+        [Test, LuceneNetSpecific]
+        public virtual void TestReverse()
+        {
+            var sb = new StringBuilder("foo 𝌆 bar𫀁mañana");
+
+            sb.Reverse();
+
+            Assert.AreEqual("anañam𫀁rab 𝌆 oof", sb.ToString());
+        }
+
+        [Test, LuceneNetSpecific]
+        public virtual void TestAppendCodePointBmp()
+        {
+            var sb = new StringBuilder("foo bar");
+            int codePoint = 97; // a
+
+            sb.AppendCodePoint(codePoint);
+
+            Assert.AreEqual("foo bara", sb.ToString());
+        }
+
+        [Test, LuceneNetSpecific]
+        public virtual void TestAppendCodePointUnicode()
+        {
+            var sb = new StringBuilder("foo bar");
+            int codePoint = 3594; // ช
+
+            sb.AppendCodePoint(codePoint);
+
+            Assert.AreEqual("foo barช", sb.ToString());
+        }
+
+        [Test, LuceneNetSpecific]
+        public virtual void TestAppendCodePointUTF16Surrogates()
+        {
+            var sb = new StringBuilder("foo bar");
+            int codePoint = 176129; // '\uD86C', '\uDC01' (𫀁)
+
+            sb.AppendCodePoint(codePoint);
+
+            Assert.AreEqual("foo bar𫀁", sb.ToString());
+        }
+
+        [Test, LuceneNetSpecific]
+        public virtual void TestAppendCodePointTooHigh()
+        {
+            var sb = new StringBuilder("foo bar");
+            int codePoint = Character.MAX_CODE_POINT + 1;
+
+            Assert.Throws<ArgumentException>(() => sb.AppendCodePoint(codePoint));
+        }
+
+        [Test, LuceneNetSpecific]
+        public virtual void TestAppendCodePointTooLow()
+        {
+            var sb = new StringBuilder("foo bar");
+            int codePoint = Character.MIN_CODE_POINT - 1;
+
+            Assert.Throws<ArgumentException>(() => sb.AppendCodePoint(codePoint));
+        }
+    }
+}


Mime
View raw message