Skip to content

Instantly share code, notes, and snippets.

@tayl0r
Created September 11, 2014 22:02
Show Gist options
  • Save tayl0r/48d3ed2f916a91d1cc2c to your computer and use it in GitHub Desktop.
Save tayl0r/48d3ed2f916a91d1cc2c to your computer and use it in GitHub Desktop.
jsonfx reader for emoji
case 'u':
{
// Unicode escape sequence
// e.g. Copyright: "\u00A9"
// unicode ordinal
int utf16;
if (this.index+4 < this.SourceLength &&
Int32.TryParse(
this.Source.Substring(this.index+1, 4),
NumberStyles.AllowHexSpecifier,
NumberFormatInfo.InvariantInfo,
out utf16))
{
this.index += 4;
int utf16_pair;
// tsteil - added
// see if we have another \u char (surrogate pair)
if (this.index+6 < this.SourceLength &&
Int32.TryParse(
this.Source.Substring(this.index+3, 4),
NumberStyles.AllowHexSpecifier,
NumberFormatInfo.InvariantInfo,
out utf16_pair))
{
this.index += 5;
var utf32 = Char.ConvertToUtf32((char)utf16, (char)utf16_pair);
builder.Append(Char.ConvertFromUtf32(utf32));
} else {
builder.Append(Char.ConvertFromUtf32(utf16));
}
}
else
{
// using FireFox style recovery, if not a valid hex
// escape sequence then treat as single escaped 'u'
// followed by rest of string
builder.Append(this.Source[this.index]);
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment