forked from linvi/tweetinvi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
linvi#430 First fix but some tests are failing
- Loading branch information
Showing
8 changed files
with
168 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Text; | ||
using Tweetinvi.Core.Extensions; | ||
|
||
namespace Tweetinvi.Core.Core.Helpers | ||
{ | ||
public static class UnicodeHelper | ||
{ | ||
public static IEnumerable<string> GetUnicodeCleanupGraphemeEnumerator(string str) | ||
{ | ||
var enumerator = StringInfo.GetTextElementEnumerator(str); | ||
|
||
while (enumerator.MoveNext()) | ||
{ | ||
var grapheme = enumerator.GetTextElement(); | ||
|
||
|
||
UnicodeCategory characterChategory = CharUnicodeInfo.GetUnicodeCategory(grapheme, 0); | ||
|
||
// Other potential categories to consider | ||
// cat == UnicodeCategory.NonSpacingMark || cat == UnicodeCategory.SpacingCombiningMark || cat == UnicodeCategory.EnclosingMark | ||
|
||
if (characterChategory == UnicodeCategory.ModifierSymbol) | ||
{ | ||
continue; | ||
} | ||
|
||
yield return grapheme; | ||
} | ||
} | ||
|
||
public static string UnicodeCleanup(string str) | ||
{ | ||
var cleanStringBuilder = new StringBuilder(); | ||
|
||
UnicodeHelper.GetUnicodeCleanupGraphemeEnumerator(str).ForEach(c => cleanStringBuilder.Append(c)); | ||
|
||
return cleanStringBuilder.ToString(); | ||
} | ||
|
||
public static string UnicodeSubstring(string str, int startIndex, int length) | ||
{ | ||
if (str == null) | ||
{ | ||
return null; | ||
} | ||
|
||
var graphemes = GetUnicodeCleanupGraphemeEnumerator(str).Skip(startIndex).Take(length); | ||
return string.Join("", graphemes); | ||
} | ||
|
||
private const char HIGH_SURROGATE_START = '\uD800'; | ||
private const char HIGH_SURROGATE_END = '\uDBFF'; | ||
private const char LOW_SURROGATE_START = '\uDC00'; | ||
private const char LOW_SURROGATE_END = '\uDFFF'; | ||
|
||
/// <summary> | ||
/// Get the UTF32 length of a string | ||
/// </summary> | ||
public static int UTF32Length(this string str) | ||
{ | ||
var length = 0; | ||
|
||
for (int i = 0; i < str.Length; ++i) | ||
{ | ||
if (str[i] >= HIGH_SURROGATE_START && str[i] <= HIGH_SURROGATE_END && | ||
str[i + 1] >= LOW_SURROGATE_START && str[i + 1] <= LOW_SURROGATE_END) | ||
{ | ||
i++; | ||
} | ||
|
||
++length; | ||
} | ||
|
||
return length; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Tweetinvi.Logic/JsonConverters/StringToCleanupUnicodeConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Tweetinvi.Core.Core.Helpers; | ||
|
||
namespace Tweetinvi.Logic.JsonConverters | ||
{ | ||
public class StringToCleanupUnicodeConverter : JsonConverter | ||
{ | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
serializer.Serialize(writer, value); | ||
|
||
} | ||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var altText = reader.Value as string ?? ""; | ||
var cleanString = UnicodeHelper.UnicodeCleanup(altText); | ||
|
||
return cleanString; | ||
} | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return true; | ||
} | ||
} | ||
} |