gpt4 book ai didi

c# - 有没有像 "user-defined encoding fallback"这样的东西

转载 作者:可可西里 更新时间:2023-11-01 08:45:31 25 4
gpt4 key购买 nike

当使用 ASCII 编码并将字符串编码为字节时,ö 之类的字符将生成 ?

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

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

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

如果无法用多个字符替换一个字符,如果我能用一个字符替换它们,我会接受 (ö -> o)

现在有几种EncoderFallback的实现,但是我不明白它们是如何工作的。
一个快速而肮脏的解决方案是在将字符串提供给 Encoding.GetBytes() 之前替换所有这些字符,但这似乎不是“正确”的方式。
我希望我能提供一个编码对象的替换表。

我怎样才能做到这一点?

最佳答案

实现您想要的“最正确”方法是实现一个自定义回退编码器,该编码器执行最适合的回退。由于各种原因,内置于 .NET 中的一种在尝试最适合哪些字符方面相当保守(存在安全隐患,具体取决于您计划将重新编码的字符串用于什么用途。)您的自定义回退策略可以根据您想要的任何规则进行最佳匹配。

话虽如此 - 在您的后备类中,您最终将编写一个包含所有不可编码的 Unicode 代码点的巨大案例语句,并手动将它们映射到它们的最佳替代方案。您可以通过简单地提前遍历您的字符串并换出不支持的字符来实现相同的目标。回退策略的主要好处是性能:您最终只循环一次字符串,而不是至少两次。不过,除非您的琴弦很大,否则我不会太担心。

如果您确实想实现自定义回退策略,您一定要阅读我评论中的文章:Character Encoding in the .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.
}
}

关于c# - 有没有像 "user-defined encoding fallback"这样的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25118364/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com