带替换的 Blob.decode 似乎不起作用

2024-02-24

这段代码:

my $þor-blob = Blob.new("þor".ords);
$þor-blob.decode( "ascii", :replacement("0"), :strict(False) ).say

失败:

Will not decode invalid ASCII (code point > 127 found)␤

和这个:

my $euro = Blob.new("3€".ords);
$euro.decode( "latin1", :replacement("euro") ).say

将 € 替换为 Ø 似乎根本不起作用。

确实如此这些方法没有经过测试 https://github.com/perl6/roast/issues/524,但是语法正确吗?


TL;DR:

  • 只有 samcv 或其他一些核心开发人员才能提供权威答案。这是我对看到的代码、注释和结果的理解。

  • If my understanding is correct, some doc and/or code needs to be sorted out to render this SO moot.1

  • 指定$replacement参数匹配不同的 P6 核心多方法而不是不这样做。我们将其称为“替换器”代码路径。

  • “替换器”代码路径通过$replacement and $strict参数传递到 nqp 中的代码路径,然后 nqp 将它们传递到后端处理替换的代码路径。

  • On the MoarVM backend, the replacement and strict arguments are passed onto the decoders for the windows1252, windows1251, and shiftjis encodings but not for other encodings.2

遵循相关代码路径

你的代码调用这段代码在Buf.pm6 https://github.com/rakudo/rakudo/blob/b394b63c27c22bf6495f7bb2348fc56a47ead45d/src/core/Buf.pm6#L297:

multi method decode(Blob:D: $encoding,
                    Str    :$replacement!,
                    Bool:D :$strict = False) {
    nqp::p6box_s(
      nqp::decoderepconf(
        self,
        Rakudo::Internals.NORMALIZE_ENCODING($encoding),
        $replacement.defined ?? $replacement !! nqp::null_s(),
        $strict ?? 0 !! 1))
}

The nqp::decoderepconffunction 直接映射到后端的相应函数。

在 MoarVM 后端,它是MVM_string_decode_from_buf_config in ops.c https://github.com/MoarVM/MoarVM/blob/6c7810ce7ca905d772ac2a3e47e73cf7c7c41ed8/src/strings/ops.c#L1781.

这又调用MVM_string_decode_config https://github.com/MoarVM/MoarVM/blob/6c7810ce7ca905d772ac2a3e47e73cf7c7c41ed8/src/strings/ops.c#L1642在同一个文件中。

从后一个函数的注释中,有几个关键句子可能解释了替换和严格性参数的相关性:

Unlike MVM_string_decode,它不会通过没有官方映射的代码点。

目前,只有 windows-1252 和 windows-1251 会产生影响。

对代码库中的代码和提交进行深入研究表明后一条评论稍微过时了,因为它看起来也应该对 shiftjis 产生影响。

Also, to be clear, if one specifies the $replacement argument in P6 then the $strict argument is going to end up being ignored (and $strict = True assumed) if decoding any encoding other than the windows or shiftjis encodings.2

特别是 ascii 和 latin1 会发生什么

当前的代码为MVM_string_decode_config does not将替换/严格性参数传递给MVM_string_ascii_decode and MVM_string_latin1_decode功能。

因此,如果使用编码“ascii”,则 blob 必须仅包含 0 到 127 之间的值,而对于“latin1”,值必须介于 0 到 255 之间。

say "þor".ords; # (254 111 114)
say "3€".ords;  # (51 8364)

第一个字符串(作为Buf) 无法解码,而是生成错误消息,因为 254 大于 127 并且MoarVM 中的 ascii 解码器代码 https://github.com/MoarVM/MoarVM/blob/master/src/strings/ascii.c通过抛出带有“无效 ASCII”消息的异常来对无效值做出反应。

The second replaces with ¬. This is because by default a Buf is an 8 bit array, so a value above 255 gets truncated to its low byte, which for is the same as ¬ (in both latin1 and Unicode).3

但如果你使用一个也好不到哪儿去Buf具有更大的元素尺寸。结果仍然是一个¬,结合tofu https://english.stackexchange.com/questions/296505/where-is-tofu-for-font-fallback-box-glyph-coming-from。即使我看不到 C 我也能看到所以我很清楚the MVM_string_latin1_decodeMoarVM 中的函数 https://github.com/MoarVM/MoarVM/blob/master/src/strings/latin1.c解码 latin1 不会抛出异常。因此,大概当它遇到 0-255 范围之外的字符值时,它会将高位字节变成豆腐。

脚注

1 Of course the very thing JJ is doing that led them to post this SO in the first place is fixing the doc. I added this footnote so that other later readers would understand that context and realize that this SO is leading to changes in the doc, and may lead to changes in the code, that will presumably render this SO moot due to the work done.

2 It would be nice if there were multis that rejected use of the $replacement argument if the decoder for the specified encoding doesn't do anything with it.

3 See timotimo++'s comment below.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带替换的 Blob.decode 似乎不起作用 的相关文章

随机推荐

  • 生成 BKS 密钥库并存储应用程序密钥

    我应该创建一个 BKS 密钥库并存储一个私有应用程序密钥 该密钥是一个 48 个字符长的字符串 我还有一个 JCEKS 密钥库 其中已包含此密钥值 如果我尝试使用 portecle 工具转换为 BKS 类型密钥库 则密钥条目会丢失 我是 J
  • 为什么我的程序集需要按特定顺序加载?

    我正在编写一个简单的插件并偶然发现contractType IsAssignableFrom pluginType https msdn microsoft com en us library system type isassignabl
  • 为 nltk 解析树生成语法规则

    如果我有这句话 Mary saw a dog 以及以下内容 pos tags NNP VBD DT NN 是否可以生成这句话的语法规则 以便可以生成解析树 下面的语法是使用的语法规则nltk parse cfg sent Mary saw
  • R 中的 strptime 错误:输入字符串太长

    我似乎无法将数据从 csv 转换为正确的日期类 我使用的是包含 1033 个日期的 csv 我已将 CSV 保存为 YYYYMMDD 格式 这是我导入 csv 的代码 似乎有效 bd lt read csv birthdaysExample
  • 用其内容替换组件 - 角度 2 [重复]

    这个问题在这里已经有答案了 我正在寻找一种方法来拥有仅以其内容呈现的组件 例如 给定组件 Component selector my cmp template div my cmp div class MyComponent 使用 angu
  • Google Analytics 缺少analytics.js 脚本

    我被困在复制谷歌分析跟踪代码问题上 看起来 Google Tag Assistant 没有考虑我从为网站生成的 GA 跟踪 JS 中复制的代码 该网站位于http www orchid co nz http www orchid co nz
  • 如何让用户保持登录状态?迅速

    我正在使用 firebase 进行登录 我需要知道如何保持登录状态直到注销 我听说过 UserDefaults 但我不确定如何使用它 这是我的登录视图控制器 import FirebaseAuth import FirebaseFirest
  • strip_tags 和 html_entity_decode 组合无法按预期工作

    从昨天起我一直在与这个问题作斗争 不幸的是没有任何效果 不完全是 我找到了某种解决方法 经过一些研究和重读文档后我仍然有点目瞪口呆和困惑 假设有一个丑陋的字符串 它已经有正确的 html 编码的特殊字符 像这样 exampleString
  • 是否可以在没有 Axon Server Enterprise 的情况下扩展 Axon Framework

    是否可以在没有 Axon Server 的情况下扩展 Axon Framework企业 我有兴趣使用 Axon 创建原型 CQRS 应用程序 但最终的可部署系统必须免收许可费用 如果 Axon Framework 无法使用免费软件扩展到六个
  • Java Android 激光条码扫描器

    我正在尝试开发一个能够读取条形码 1d 而不是 qr 的应用程序 真正的问题是 常见的条形码阅读器软件非常有用 但它们似乎太慢 无法作为非常频繁和常见的功能使用 这些条形码相当长 有 20 30 位数字 对于 zxing 等应用程序来说似乎
  • 从java中的plsql函数获取返回的记录类型

    我有一个plsql返回记录类型的函数challan rec create or replace package xx bal api as type challan rec is record challan number varchar2
  • THREE.js JSONLoader 回调

    在 THREE js 中 如果我多次调用 JSONLoader 来加载多个对象 如下所示 简化示例 function init var loader new THREE JSONLoader loader load mesh1 js cre
  • React form onChange->setState 落后一步

    我在构建网络应用程序时遇到了这个问题 我在这个中复制了它jsfiddle http jsfiddle net terda12 270Lf0x9 本质上 我想要一个输入来调用this setState message input val 每次
  • Git/SVN 用于 asp.net 开发而不是 VSS?

    在工作中 我们使用ASP net 2 0和VSS VSS 是一个野兽 我们不断地遇到人们检查文件的问题 而且没有分支 让它变得疯狂 我知道 SVN GIT 主要由开源开发人员使用 那么 ASP NET 开发人员使用它有什么缺点吗 我一直在内
  • 如何使用MomentJS将自定义日期设置为react-datepicker的DatePicker?

    我已将日期存储在数据库中 我可以成功获取日期 但是当涉及到将任何特定日期设置为 DatePicker 时反应日期选择器 https www npmjs com package react datepicker 我无法设定那个日期 这是我用来
  • 模拟会话在 MVC 5 中不起作用

    我将值存储在正在测试的控制器操作的会话中 我读过几篇关于如何模拟会话的文章 我正在尝试实现 Milox 的答案在单元测试中设置 httpcontext 当前会话 https stackoverflow com questions 96242
  • 如何在 Apple Watch 上绘制自定义图形?

    如何在 Apple Watch 上绘制自定义图形 如果我理解正确的话 我们只能在 Apple Watch 上使用图像和标准控件 如果是这样 是否可以在内存中的图像上绘制自定义图形 然后将这些图像放到屏幕上 In watchOS2你可以借鉴W
  • std::vector::resize() 与 gcc 4.7.0 的奇怪行为

    我仍然对 的行为感到困惑std vector resize 考虑以下代码 另请参阅std vector 的类型要求 https stackoverflow com questions 12251368 type requirements f
  • 如何检测包含(水平)合并单元格的 Word 表格?

    当 Word 表格包含水平合并的单元格时 访问 Table Columns First 或对 Table Columns 执行 Foreach 将导致错误 有没有办法确定表格是否包含水平合并的单元格而不导致错误 我读了确定Word单元格是否
  • 带替换的 Blob.decode 似乎不起作用

    这段代码 my or blob Blob new or ords or blob decode ascii replacement 0 strict False say 失败 Will not decode invalid ASCII co