当使用 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/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)
我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我有一个奇怪的问题:我在rvm上安装了rubyonrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file