From 3389a1ca35e60c4e5169fa54147172fe77803b4b Mon Sep 17 00:00:00 2001 From: linvi Date: Wed, 4 Oct 2017 23:51:33 +0100 Subject: [PATCH] #430 Fixed a small issue and now the tests are now correctly running --- Testinvi/Tweetinvi.Core/UnicodeHelperTests.cs | 2 +- .../Tweetinvi.Logic/ExtendedTweetTests.cs | 2 +- Tweetinvi.Core/Core/Helpers/UnicodeHelper.cs | 30 ++++++++++++++++++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/Testinvi/Tweetinvi.Core/UnicodeHelperTests.cs b/Testinvi/Tweetinvi.Core/UnicodeHelperTests.cs index d5d8b0b34..e713cc3b5 100644 --- a/Testinvi/Tweetinvi.Core/UnicodeHelperTests.cs +++ b/Testinvi/Tweetinvi.Core/UnicodeHelperTests.cs @@ -25,7 +25,7 @@ public void UnicodeSubstring() var substr = UnicodeHelper.UnicodeSubstring(str, 141); // Assert - Assert.AreEqual(substr, " https://t.co/oUeMIkyb5G"); + Assert.AreEqual(substr, "https://t.co/oUeMIkyb5G"); } } } diff --git a/Testinvi/Tweetinvi.Logic/ExtendedTweetTests.cs b/Testinvi/Tweetinvi.Logic/ExtendedTweetTests.cs index ab21e9f18..cc833672f 100644 --- a/Testinvi/Tweetinvi.Logic/ExtendedTweetTests.cs +++ b/Testinvi/Tweetinvi.Logic/ExtendedTweetTests.cs @@ -203,7 +203,7 @@ public void UnicodeExtendedTweetSuffix() { ITweet tweet = InitTweet(EXTENDED_TWEET_WITH_UNICODE, TweetMode.Extended); - Assert.AreEqual(tweet.Suffix, " https://t.co/oUeMIkyb5G"); + Assert.AreEqual(tweet.Suffix, "https://t.co/oUeMIkyb5G"); } diff --git a/Tweetinvi.Core/Core/Helpers/UnicodeHelper.cs b/Tweetinvi.Core/Core/Helpers/UnicodeHelper.cs index ebefe606f..af6bf739a 100644 --- a/Tweetinvi.Core/Core/Helpers/UnicodeHelper.cs +++ b/Tweetinvi.Core/Core/Helpers/UnicodeHelper.cs @@ -1,4 +1,6 @@ -using System.Globalization; +using System; +using System.Diagnostics; +using System.Globalization; using System.Text; namespace Tweetinvi.Core.Core.Helpers @@ -14,12 +16,38 @@ public static string UnicodeSubstring(string str, int startIndex) var sbuilder = new StringBuilder(); + Func shouldCountTwice = (string str2, int i2) => + { + if (char.IsSurrogatePair(str2, i2)) + { + var grapheme = $"{str2[i2]}{str2[i2 + 1]}"; + + Console.WriteLine($"{grapheme} = {(int)str[i2]}/{(int)str[i2 + 1]}"); + + UnicodeCategory characterChategory = CharUnicodeInfo.GetUnicodeCategory(grapheme, 0); + + return characterChategory == UnicodeCategory.ModifierSymbol; + } + + return false; + }; + var i = 0; for (; i < startIndex; ++i) { if (char.IsSurrogatePair(str, i)) { ++i; + ++startIndex; + + var grapheme = $"{str[i]}{str[i + 1]}"; + + UnicodeCategory characterChategory = CharUnicodeInfo.GetUnicodeCategory(grapheme, 0); + + if (characterChategory == UnicodeCategory.ModifierSymbol) + { + ++startIndex; + } } }