Example #1
0
        public static EncodedWord Concat(EncodedWord first, EncodedWord second)
        {
            if (first._charset != second._charset)
            {
                throw new InvalidOperationException("Both words must have the same charset");
            }
            if (first._encoding != second._encoding)
            {
                throw new InvalidOperationException("Both words must have the same encoding");
            }

            return(new EncodedWord(first._rawValue.TrimEnd('=') + second._rawValue, first._charset, first._encoding)
            {
                NextSeparator = second.NextSeparator, NextWord = second.NextWord
            });
        }
Example #2
0
        private static string OutputTokens(TokenBase token)
        {
            if (token == null)
            {
                return(string.Empty);
            }

            var nextSeparatorStringValue = token.NextSeparator != null?token.NextSeparator.GetStringValue() : string.Empty;

            if (token is AsciiWord)
            {
                return(token.GetStringValue() + nextSeparatorStringValue + OutputTokens(token.NextWord));
            }

            if (token is EncodedWord)
            {
                var encodedWordToken = token as EncodedWord;
                if (encodedWordToken.CanBeConcatenatedWith(encodedWordToken.NextWord))
                {
                    return(OutputTokens(EncodedWord.Concat(encodedWordToken, encodedWordToken.NextWord as EncodedWord)));
                }

                if (encodedWordToken.NextWord is EncodedWord)
                {
                    return(token.GetStringValue() + OutputTokens(token.NextWord));
                }

                return(token.GetStringValue() + nextSeparatorStringValue + OutputTokens(token.NextWord));
            }

            if (token is Separator)
            {
                if (token.NextWord is EncodedWord)
                {
                    return(OutputTokens(token.NextWord));
                }
                return(token.GetStringValue() + OutputTokens(token.NextWord));
            }

            throw new InvalidOperationException("Unknown token type");
        }