有没有类似于“用户定义编码回退”的东西?

13

当使用ASCII编码并将字符串编码为字节时,像ö这样的字符将会变成?

Encoding encoding = Encoding.GetEncoding("us-ascii");     // or Encoding encoding = Encoding.ASCI;
data = encoding.GetBytes(s);

我正在寻找一种方法来用不同的字符替换那些字符,而不仅仅是一个问号。
例如:

ä -> ae
ö -> oe
ü -> ue
ß -> ss

如果无法用多个字符来替换一个字符,即使我可以用一个字符来替换它(ö -> o),我也会接受。

现在有几种EncoderFallback的实现,但我不理解它们的工作原理。
一个快速而不完美的解决方案是,在将字符串传递给Encoding.GetBytes()之前替换所有这些字符,但这似乎不是“正确”的方法。
我希望我能够将替换表格提供给编码对象。

我该如何实现这一点?


1
你读过这篇文章了吗?特别是关于“最佳适配回退”和“实现自定义回退”的部分?http://msdn.microsoft.com/en-us/library/ms404377(v=vs.110).aspx#custom - Michael Edenfield
1个回答

13
实现你想要的“最正确”的方法是实现一个自定义后备编码器,它执行最佳匹配回退。 .NET内置的那个由于各种原因在尝试最佳匹配时相当保守(根据您计划对重新编码的字符串进行何种用途,可能存在安全性问题)。您的自定义回退策略可以根据您想要的任何规则执行最佳匹配。
话虽如此-在您的回退类中,您最终将编写一个大型案例语句,其中包含所有不可编码的Unicode代码点,并手动将它们映射到其最佳替代品。您可以通过预先循环遍历字符串并替换不支持的字符来实现相同的目标。回退策略的主要好处是性能:您只需要循环一次字符串,而不是至少两次。但除非您的字符串很大,否则我不会太担心。
如果确实要实现自定义回退策略,您应该确实阅读我评论中的文章:.NET Framework中的字符编码。这不是真的难,但您必须了解编码回退的工作原理。
您为Encoder.GetEncoding方法提供自定义类的实现,该类必须派生自EncoderFallback。不过,该类基本上只是真正工作的包装器,在EncoderFallbackBuffer中完成。您需要缓冲区的原因是回退不一定是一对一的过程;在您的示例中,您可能会将单个Unicode字符映射到两个ASCII字符。
在编码过程首次遇到问题并需要依靠您的策略进行回退的点上,它使用您的EncoderFallback 实现来创建EncoderFallbackBuffer 的实例。然后调用您自定义缓冲区的Fallback方法。
在内部,您的缓冲区构建一组要替换非可编码字符的字符,并返回true 。从那里开始,编码器将反复调用GetNextChar,只要Remaining> 0 和/或直到GetNextChar返回CP 0为止,并将这些字符粘贴到编码结果中。
文章包括一个几乎完全实现了您要尝试做的事情的实现;我已经复制了下面的基本框架,这应该可以让您入门。
public class CustomMapper : EncoderFallback
{
   // Use can override the "replacement character", so track what they
   // give us.
   public string DefaultString;

   public CustomMapper() : this("*")
   {   
   }

   public CustomMapper(string defaultString)
   {
      this.DefaultString = defaultString;
   }

   public override EncoderFallbackBuffer CreateFallbackBuffer()
   {
      return new CustomMapperFallbackBuffer(this);
   }

   // This is the length of the largest possible replacement string we can
   // return for a single Unicode code point.
   public override int MaxCharCount
   {
      get { return 2; }
   } 
}

public class CustomMapperFallbackBuffer : EncoderFallbackBuffer
{
   CustomMapper fb; 

   public CustomMapperFallbackBuffer(CustomMapper fallback)
   {
      // We can use the same custom buffer with different fallbacks, e.g.
      // we might have different sets of replacement characters for different
      // cases. This is just a reference to the parent in case we want it.
      this.fb = fallback;
   }

   public override bool Fallback(char charUnknown, int index)
   {
      // Do the work of figuring out what sequence of characters should replace
      // charUnknown. index is the position in the original string of this character,
      // in case that's relevant.

      // If we end up generating a sequence of replacement characters, return
      // true, and the encoder will start calling GetNextChar. Otherwise return
      // false.

      // Alternatively, instead of returning false, you can simply extract
      // DefaultString from this.fb and return that for failure cases.
   }

   public override bool Fallback(char charUnknownHigh, char charUnknownLow, int index)
   {
      // Same as above, except we have a UTF-16 surrogate pair. Same rules
      // apply: if we can map this pair, return true, otherwise return false.
      // Most likely, you're going to return false here for an ASCII-type
      // encoding.
   }

   public override char GetNextChar()
   {
      // Return the next character in our internal buffer of replacement
      // characters waiting to be put into the encoded byte stream. If
      // we're all out of characters, return '\u0000'.
   }

   public override bool MovePrevious()
   {
      // Back up to the previous character we returned and get ready
      // to return it again. If that's possible, return true; if that's
      // not possible (e.g. we have no previous character) return false;
   }

   public override int Remaining 
   {
      // Return the number of characters that we've got waiting
      // for the encoder to read.
      get { return count < 0 ? 0 : count; }
   }

   public override void Reset()
   {
       // Reset our internal state back to the initial one.
   }
}

很棒的答案,比MSDN上的例子容易理解多了。我并不真正理解他们在例子中为什么要做几件事情(因此使它变得复杂)。我仍然在质疑:为什么我应该选择这种方法而不是简单地替换原始字符串中的字符,然后将其传递给GetBytes() - joe
1
我能想到唯一合理的原因就是性能。我假设你仍然想在最后使用Encoding.GetBytes();在这种情况下,您将不得不循环遍历字符串以交换无效字符为可编码字符,并且由于它们不会是1对1,这将涉及大量的字符串复制。 然后编码器必须最后一次循环遍历您的字符串以进行编码。正如我所说,只有当您的字符串变得非常长时才会出现此问题。(或者,您可以自己编写整个ASCII编码器,它是所有编码器中最简单的 :) ) - Michael Edenfield
谢谢,非常好的答案! - joe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接