- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
当使用 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/
我试图找出 asp-fallback-test-property 和 asp-fallback-test 之间的区别,两者都使用该属性来确定是否回退。它们是相同还是有区别 下面的链接没有回答差异,而且
我有以下路由器对象 const appRoutes: Routes = [ { path: '', redirectTo: '/', pathMatch:'full'}, { path: ''
我是微服务的新手,我一直遇到挑战,我认为这些挑战很小但无法解决。来 self 的 Eureka 注册服务 @RestController @RequestMapping("/users") publi
所以如果你有这样的代码: background: url('image.png'); background: -webkit-gradient(linear, left top, left botto
我怎么知道在使用 FALLBACK 时我是否真的需要返回一个 l 值? ? 我正在使用 return-rw但我只想使用 return在可能的情况。我想跟踪我是否真的修改过 %!attrs或仅在 FAL
我有以下使用流类型的函数 push = (pathname: string, data?: Object) => { const history = [...this.state.hist
正如 Google 对页面性能的建议,我将 webp 图像添加到我的项目中。但是我知道 Safari 不支持这种图像类型,所以我试图为 Safari 用户添加后备 png 图像。 我使用了下面的设置。
我正在尝试将SQL2008支持添加到.NET 2.0应用程序。但是,我的独特之处是某些用户仍将使用SQL2005,并且我不想要求他们安装SQL2008客户端组件。 我为SQL2008所需的实际DLL集
正如 Google 对页面性能的建议,我将 webp 图像添加到我的项目中。但是我知道 Safari 不支持这种图像类型,所以我试图为 Safari 用户添加后备 png 图像。 我使用了下面的设置。
我目前正在微调一个相对较大的项目的 log4j 配置。目前我还没有为所有可以创建日志条目的位置配置日志级别。 我希望 log4j 有某种后备附加程序来记录未配置其他附加程序的所有消息。因此,如果我有一
我在我的 javascript 中使用选项,如果它不存在,它会回退到默认值: var tabActive = typeof data.tabActive !== 'undefined' ? data.
我最近将 xcode 更新到最新的 5.1.1,随后将 ios 支持升级到 7.1(从 7.0)。 我有一些中文字符的自定义字体,在我更新之前可以正常使用,但现在根本无法使用! 这是它之前工作的图片:
我正在编写一个必须显示图像并可能加载图像的应用程序。所以我想知道是否有让 QGraphicsScene 使用 OpenGL 的正确方法,如果失败,请使用软件渲染器。 我已经阅读了文档,但是如果设置视口
为什么在使用 hystrix 库时在后备方法中进行数据库调用是一种不好的做法? @HystrixCommand(fallbackMethod ="fallBackMethod") public Dou
我想使用 fallback.io 中的 fallback.js,所以我使用了 github 中的文档。问题是它只适用于 css 和字体文件,但不适用于我的所有 js 脚本。 fallbac
我有一组类,每个类都需要在某个时候决定它们应该在内部使用两种或三种方法中的哪一种来在外部实现相同的功能。理想情况下,这应该包括回退功能,如果 ApproachA 失败,它会失败以尝试 Approach
我想在失败的情况下使用回退,因此“打开”状态下的行为应该是调用回退而不是抛出异常。 问题是回退在“关闭”状态下被调用,而对于“打开”状态我仍然得到异常。这是预期的行为吗?无法获得我正在寻找的东西? 我
我正在尝试将 xsl:fallback 合并到我的样式表中。我正在使用 Saxon9.5.1.23-HE。 我在调用的命名模板中尝试了这段代码: blabla
我玩 asp core 并希望回退到我的本地 bootswatchSlate.css,它位于 wwwroot 文件夹中,只有当我无法从 cdn 访问 bootstrap 时: 布局.cshtml 虽
我想通过使用两个后备将字符串输出到呈现的 HTML。 我的错字是在顶级页面(我网站的根页面)定义的,并传递到较低级别的所有页面。 结构: 主页 1 级页面 2 级页面 文件的常规输出: 该字符串来自
我是一名优秀的程序员,十分优秀!