public static IEnumerable <TextEntityDescription> ParseText(
            string text, IEnumerable <TwitterEntity> entities)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }
            var escaped  = ParsingExtension.EscapeEntity(text);
            var endIndex = 0;

            // distinct by startindex ignores extended_entities.
            foreach (var entity in entities.Distinct(e => e.StartIndex).OrderBy(e => e.StartIndex))
            {
                if (endIndex < entity.StartIndex)
                {
                    // return raw string
                    yield return(new TextEntityDescription(ParsingExtension.ResolveEntity(
                                                               escaped.SurrogatedSubstring(endIndex, entity.StartIndex - endIndex))));
                }
                if (escaped.Length <= entity.StartIndex || escaped.Length < entity.EndIndex)
                {
                    // Twitter rarely gives entities of extended_tweet for text of compatibility mode.
                    // We need paranoiac test here.
                    continue;
                }
                // get entitied text
                var body = ParsingExtension.ResolveEntity(escaped.SurrogatedSubstring(
                                                              entity.StartIndex, entity.EndIndex - entity.StartIndex));
                yield return(new TextEntityDescription(body, entity));

                endIndex = entity.EndIndex;
            }
            if (endIndex == 0)
            {
                // entity is empty.
                yield return(new TextEntityDescription(text));
            }
            else if (endIndex < escaped.Length)
            {
                // return remain text
                yield return(new TextEntityDescription(ParsingExtension.ResolveEntity(
                                                           escaped.SurrogatedSubstring(endIndex))));
            }
        }
Esempio n. 2
0
        public static IEnumerable <TextEntityDescription> ParseText(
            string text, IEnumerable <TwitterEntity> entities)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }
            var escaped  = ParsingExtension.EscapeEntity(text);
            var endIndex = 0;

            foreach (var entity in entities.OrderBy(e => e.StartIndex))
            {
                if (endIndex < entity.StartIndex)
                {
                    // return raw string
                    yield return(new TextEntityDescription(ParsingExtension.ResolveEntity(
                                                               escaped.SubstringForSurrogatePaire(endIndex, entity.StartIndex - endIndex))));
                }
                // get entitied text
                var body = ParsingExtension.ResolveEntity(escaped.SubstringForSurrogatePaire(
                                                              entity.StartIndex, entity.EndIndex - entity.StartIndex));
                yield return(new TextEntityDescription(body, entity));

                endIndex = entity.EndIndex;
            }
            if (endIndex == 0)
            {
                // entity is empty.
                yield return(new TextEntityDescription(text));
            }
            else if (endIndex < escaped.Length)
            {
                // return remain text
                yield return(new TextEntityDescription(ParsingExtension.ResolveEntity(
                                                           escaped.SubstringForSurrogatePaire(endIndex))));
            }
        }