Skip to content

Instantly share code, notes, and snippets.

@halllo
Created October 30, 2016 13:05
Show Gist options
  • Save halllo/a080ee8b76cb5dae7a41a7dd735ba66e to your computer and use it in GitHub Desktop.
Save halllo/a080ee8b76cb5dae7a41a7dd735ba66e to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SurrogatePairDetection
{
[TestClass]
public class SurrogatePairDetection
{
[TestMethod]
public void LengthTest()
{
var s = "𠮷hal𠮷lo野";
var symbols = Symbols(s).ToList();
Assert.AreEqual(8, symbols.Count());
}
[TestMethod]
public void SymbolsTest()
{
var s = "𠮷hal𠮷lo野";
var symbols = Symbols(s).ToList();
CollectionAssert.AreEqual(new List<string> { "𠮷", "h", "a", "l", "𠮷", "l", "o", "野" }, symbols);
}
private IEnumerable<string> Symbols(string s)
{
for (int i = 0; i < s.Length; i++)
{
if (i + 1 < s.Length && char.IsSurrogatePair(s[i], s[i + 1]))
{
yield return new string(new[] { s[i], s[i + 1] });
i++;
}
else
{
yield return s[i].ToString();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment