当前位置: 首页>>代码示例>>C#>>正文


C# Portable.Text.Encoding.GetEncoder方法代码示例

本文整理汇总了C#中Portable.Text.Encoding.GetEncoder方法的典型用法代码示例。如果您正苦于以下问题:C# Portable.Text.Encoding.GetEncoder方法的具体用法?C# Portable.Text.Encoding.GetEncoder怎么用?C# Portable.Text.Encoding.GetEncoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Portable.Text.Encoding的用法示例。


在下文中一共展示了Portable.Text.Encoding.GetEncoder方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: GetRfc822Words

		static IList<Word> GetRfc822Words (FormatOptions options, Encoding charset, string text, bool phrase)
		{
			var encoder = charset.GetEncoder ();
			var words = new List<Word> ();
			var chars = new char[2];
			var saved = new Word ();
			var word = new Word ();
			int nchars, n, i = 0;
			char c;

			while (i < text.Length) {
				c = text[i++];

				if (c < 256 && IsBlank (c)) {
					if (word.ByteCount > 0) {
						words.Add (word);
						word = new Word ();
					}

					word.StartIndex = i;
				} else {
					// save state in case adding this character exceeds the max line length
					word.CopyTo (saved);

					if (c < 127) {
						if (IsCtrl (c)) {
							word.Encoding = options.AllowMixedHeaderCharsets ? Math.Max (word.Encoding, 1) : 2;
							word.Type = WordType.EncodedWord;
							word.EncodeCount++;
						} else if (phrase && !IsAtom (c)) {
							// phrases can have quoted strings
							if (word.Type == WordType.Atom)
								word.Type = WordType.QuotedString;
						}

						if (c == '"' || c == '\\')
							word.QuotedPairs++;

						word.ByteCount++;
						word.CharCount++;
						nchars = 1;
					} else if (c < 256) {
						// iso-8859-1
						word.Encoding = options.AllowMixedHeaderCharsets ? Math.Max (word.Encoding, 1) : 2;
						word.Type = WordType.EncodedWord;
						word.EncodeCount++;
						word.ByteCount++;
						word.CharCount++;
						nchars = 1;
					} else {
						if (char.IsSurrogatePair (text, i - 1)) {
							chars[1] = text[i++];
							nchars = 2;
						} else {
							nchars = 1;
						}

						chars[0] = c;

						try {
							n = encoder.GetByteCount (chars, 0, nchars, true);
						} catch {
							n = 3;
						}

						word.Type = WordType.EncodedWord;
						word.CharCount += nchars;
						word.EncodeCount += n;
						word.ByteCount += n;
						word.Encoding = 2;
					}

					if (ExceedsMaxLineLength (options, charset, word)) {
						// restore our previous state
						saved.CopyTo (word);
						i -= nchars;

						// Note: if the word is longer than what we can fit on
						// one line, then we need to encode it.
						if (word.Type == WordType.Atom) {
							word.Type = WordType.EncodedWord;

							// in order to fit this long atom under MaxLineLength, we need to
							// account for the added length of =?us-ascii?q?...?=
							n = "us-ascii".Length + 7;
							word.CharCount -= n;
							word.ByteCount -= n;
							i -= n;
						}

						words.Add (word);

						saved.Type = word.Type;
						word = new Word ();

						// Note: the word-type needs to be preserved when breaking long words.
						word.Type = saved.Type;
						word.StartIndex = i;
					}
				}
//.........这里部分代码省略.........
开发者ID:naeemkhedarun,项目名称:MimeKit,代码行数:101,代码来源:Rfc2047.cs

示例2: CharsetConvert

		static byte[] CharsetConvert (Encoding charset, char[] word, int length, out int converted)
		{
			var encoder = charset.GetEncoder ();
			int count = encoder.GetByteCount (word, 0, length, true);
			var encoded = new byte[count];

			converted = encoder.GetBytes (word, 0, length, encoded, 0, true);

			return encoded;
		}
开发者ID:naeemkhedarun,项目名称:MimeKit,代码行数:10,代码来源:Rfc2047.cs

示例3: CharsetFilter

		/// <summary>
		/// Initializes a new instance of the <see cref="MimeKit.IO.Filters.CharsetFilter"/> class.
		/// </summary>
		/// <remarks>
		/// Creates a new <see cref="CharsetFilter"/> to convert text from the specified
		/// source encoding into the target charset encoding.
		/// </remarks>
		/// <param name="sourceEncoding">Source encoding.</param>
		/// <param name="targetEncoding">Target encoding.</param>
		/// <exception cref="System.ArgumentNullException">
		/// <para><paramref name="sourceEncoding"/> is <c>null</c>.</para>
		/// <para>-or-</para>
		/// <para><paramref name="targetEncoding"/> is <c>null</c>.</para>
		/// </exception>
		public CharsetFilter (Encoding sourceEncoding, Encoding targetEncoding)
		{
			if (sourceEncoding == null)
				throw new ArgumentNullException ("sourceEncoding");

			if (targetEncoding == null)
				throw new ArgumentNullException ("targetEncoding");

			SourceEncoding = sourceEncoding;
			TargetEncoding = targetEncoding;

			decoder = (Decoder) SourceEncoding.GetDecoder ();
			encoder = (Encoder) TargetEncoding.GetEncoder ();
		}
开发者ID:dcga,项目名称:MimeKit,代码行数:28,代码来源:CharsetFilter.cs


注:本文中的Portable.Text.Encoding.GetEncoder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。