字符串间隔如何工作?

2024-03-06

我做了一些玩这个答案 https://stackoverflow.com/questions/25308978/what-are-intervals-in-swift-ranges/25631930#25631930,甚至向 Apple 提出了 RADAR 问题,要求提供有关此事的更好文档(蟋蟀鸣叫)。

问题是:字符串间隔如何工作?

如果你看看我的 Playground 中的第 367 行,你会发现我在搞乱字符串间隔。

我将字符串内容提取到一个较小的游乐场中:

// String Intervals
// These are odd. Looks like it is using the ASCII values. I should experiment with Unicode, and see where we go...
let aThroughFClosed:ClosedInterval<String>          = "A"..."F"
let dThroughQClosed:ClosedInterval                  = "D"..."Q"
let mThroughSClosed:ClosedInterval                  = "M"..."S"
let tThroughWClosed:ClosedInterval                  = "T"..."W"
let whiskeyTangoFoxtrot1                            = "QED"..."WTF" /* Not sure what will happen when I start working with this... */
let aThroughHHalfOpen:HalfOpenInterval<String>      = "A"..<"H"
let dThroughRHalfOpen:HalfOpenInterval              = "D"..<"R"
let mThroughTHalfOpen:HalfOpenInterval              = "M"..<"T"
let tThroughXHalfOpen:HalfOpenInterval              = "T"..<"X"
let whiskeyTangoFoxtrot2                            = "QED"..<"WTF"
let clampedString1 = aThroughFClosed.clamp ( dThroughQClosed )  /* This represents "D"..."F" */
let clampedString2 = dThroughQClosed.clamp ( aThroughFClosed )  /* This represents "D"..."F" */
let clampedString3 = dThroughQClosed.clamp ( mThroughSClosed )  /* This represents "M"..."Q" */
let clampedString4 = dThroughQClosed.clamp ( tThroughWClosed )  /* This represents "Q"..."Q" */
let clampedString5 = aThroughFClosed.clamp ( tThroughWClosed )  /* This represents "F"..."F" */
let clampedString6 = aThroughHHalfOpen.clamp ( dThroughRHalfOpen )  /* This represents "D"..<"G" */
let clampedString7 = dThroughRHalfOpen.clamp ( aThroughHHalfOpen )  /* This represents "D"..<"H" */
let clampedString8 = dThroughRHalfOpen.clamp ( mThroughTHalfOpen )  /* This represents "M"..<"R" */
let clampedString9 = dThroughRHalfOpen.clamp ( tThroughXHalfOpen )  /* This represents "R"..<"R" */
let clampedString0 = aThroughHHalfOpen.clamp ( tThroughXHalfOpen )  /* This represents "H"..<"H" (Not exactly sure why) */

// 2.2.3: STRING INTERVALS

// String intervals are weird. Just sayin'...

// 2.2.3.1: STRING INTERVALS AS SWITCH TESTS

var testValue3:String = "B"

switch ( testValue3 )
    {
case aThroughFClosed:   /* This will catch it. */
    println ( "In A...F." )

default:
    println ( "In catchall." )
}

// Looks like the test is only on the first letter.
testValue3 = "Badz-Maru"

switch ( testValue3 )
    {
case aThroughFClosed:   /* This will catch it. */
    println ( "In A...F." )

default:
    println ( "In catchall." )
}

testValue3 = "\tBadz-Maru"   /* If we add a tab character to the start of the string, then the first test will fail. */

switch ( testValue3 )
    {
case aThroughFClosed:
    println ( "In A...F." )

default:    /* This will catch it. */
    println ( "In catchall." )
}

// Now, we'll get really strange. Let's look at our multi-character intervals...

testValue3 = "W"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:  /* This catches it. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "T"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:  /* This catches it. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "F"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:
    println ( "WTF, dude?" )

default: /* However, in this case, it falls through to default. */
    println ( "In catchall." )
}

testValue3 = "WT"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2: /* "WT" is caught. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "WTF"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:
    println ( "WTF, dude?" )

default:    /* "WTF" is not caught. */
    println ( "In catchall." )
}

testValue3 = "QED"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:  /* "QED" is caught. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "QTF"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:  /* "QTF" is caught. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "QSF"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:  /* "QSF" is caught. */
    println ( "WTF, dude?" )

default:
    println ( "In catchall." )
}

testValue3 = "QAF"

switch ( testValue3 )
    {
case whiskeyTangoFoxtrot2:
    println ( "WTF, dude?" )

default: /* QAF falls through. */
    println ( "In catchall." )
}

据我了解,ClosedInterval(lo, hi)代表所有字符串s这样

lo <= s <= hi

and HalfOpenInterval(lo, hi)代表所有字符串s这样

lo <= s < hi

where <= and <是由字典顺序弦乐的, 即逐一比较字符,直到发现差异。

例如"QED" < "T" < "WTF"因为Q < T < W, but "F" < "QED"因为F < Q。所以

HalfOpenInterval("QED" , "WTF").contains("T") == true
HalfOpenInterval("QED" , "WTF").contains("F") == false

And "QED" < "QSF"因为Q == Q and E < S, but "QAF" < "QED"因为Q == Q and A < E。所以

HalfOpenInterval("QED" , "WTF").contains("QSF") == true
HalfOpenInterval("QED" , "WTF").contains("QAF") == false

这应该可以解释你所有的测试结果。

最后,

let clampedString0 = aThroughHHalfOpen.clamp ( tThroughXHalfOpen )

is an empty间隔因为"A"..<"H" and "T"..<"X"没有共同点。 空区间可以表示为HalfOpenInterval(dummy, dummy) for any的价值dummy.

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

字符串间隔如何工作? 的相关文章

随机推荐

  • numpy任意精度线性代数

    我有一个 numpy 2d 数组 中 大尺寸 比如说 500x500 我想找到它的元素指数的特征值 问题是某些值非常负 800 1000 等 并且它们的指数下溢 意味着它们非常接近零 因此 numpy 将它们视为零 无论如何可以在 nump
  • PHP_SELF 和 XSS

    我发现一篇文章声称 SERVER PHP SELF 容易受到 XSS 攻击 我不确定我是否理解正确 但我几乎可以肯定这是错误的 这怎么可能容易受到 XSS 攻击
  • 画布不绘制图像

    我只是想弄清楚如何在画布上绘制图像 我按照 W3 学校的教程进行操作 但是当我自己尝试时 它似乎不起作用 我将下面的代码复制并粘贴到 HTML 文件中 但图像从未加载到画布中 我将图片下载到同一目录中 我四处询问 上网查了一下 但似乎没有人
  • 使用 php 透视 html 表

    我有这个 php 代码 query production SELECT uploadedby as name sum points as points date format uploaddate Y m d as date FROM im
  • 保护非空单元格VBA

    我添加了 VBA 代码 双击时将时间或日期插入单元格 我设法让事情进展顺利 我正在努力解决的问题是在输入时间 日期后保护和锁定单元格 我已经到了这样的地步 当我双击 尝试编辑非空单元格时 我收到运行时错误 调试后 让我失望的行是 Targe
  • Android BLE 特性 getValue 在 API 级别 33 中已弃用,我获取该值的正确方法是什么?

    Android BLE 特性 getValue 在 API 级别 33 和developer android 中已弃用蓝牙Gatt特性 https developer android com reference android blueto
  • 在 Rmarkdown 中的参考书目后添加图形和表格

    我想在 R Markdown 文档中的参考书目后面添加表格和图形 但是 R Markdown 文档默认情况下始终将参考书目添加到报告的末尾 有没有一种简单的方法可以在参考文献之后添加内容到文档中 尝试的解决方案 A 之前的回答 https
  • 如何向 Exuberant Ctags 配置文件添加注释?

    我可以使用什么字符在 Exuberant Ctags 中发表评论 ctags file 我想添加注释和解释 也许还可以禁用一些正则表达式 但我找不到 ctags exuberant 接受的任何评论字符 我不断收到警告 ctags Warni
  • 我可以使用实体框架(模型优先)来生成组合键吗?

    我正在使用实体框架的 模型优先 方法设计一个数据库 基于我收到的反馈非常好 https stackoverflow com questions 4969133 database design problem在这里 我对数据库的一部分采用超类
  • Rmarkdown nocite 不显示 pdf 中的引用

    找到不起作用的方法是很困难的 我想使用nocite命令来显示我在文档中未引用的参考书目的条目 但我不明白为什么它不起作用 这是一个 MWE title Test date r format Sys time d B Y output pdf
  • Nodejs 使用 npm + package.json 解决依赖关系

    我的项目结构如下 index js package json node modules Service A main js package json Service B main js package json 当我做npm install
  • Android 5.1.1 及更高版本 - getRunningAppProcesses() 仅返回我的应用程序包

    看来谷歌终于关闭了获取当前前台应用程序包的所有大门 棒棒糖更新后 杀死了getRunningTasks int maxNum http developer android com intl zh cn reference android a
  • 如何检测网站是否无法通过 iframe 嵌入?

    我正在尝试在 iframe 中嵌入一些随机站点 我收到此错误 Refused to display document because display forbidden by X Frame Options 我愿意尊重他们的意愿而不嵌入它
  • 如何将JsDoc变成“混合”类型?

    简单的问题 如何记录 混合类型 我知道我可以列出所有可能的类型 例如 null undefined String Number Object 最终发现自己错过了一个并使其变得过于复杂 我尝试使用 Mixed 关键字 但它在许多 IDE 例如
  • JavaScript 性能? - 将事件放在html标签中,或者绑定它们?

    我想知道哪个性能更好 我有一个 网络应用程序 之类的东西 它有很多 javascript 单击按钮时 隐藏的 div 变得可见 这个新 div 有 5 个按钮 哪个对性能更好 1 将按钮点击事件放在每个按钮的 html 标签中 例如 onC
  • 多线程Socket通信客户端/服务器

    我写完了一个客户端 服务器套接字通信程序 运行良好 现在我正在尝试弄清楚如何制作它 以便我可以同时与服务器建立多个客户端连接 我环顾四周 似乎有不止几种不同的方法可以做到这一点 所以我来这里向你们寻求帮助 建议 我的服务器 public c
  • GC 根和局部变量

    在看着定时器文档 http msdn microsoft com en us library system timers timer interval aspx我遇到了以下带有此评论的示例 Normally the timer is dec
  • 根据 Sidekiq 或 Rails 的调用记录到不同的记录器

    我有一个基于 Rails 3 2 的应用程序 它使用 Sidekiq 2 12 来运行后台作业 Sidekiq 作业可以调用与交互式 Rails 应用程序相同的方法 我希望这些方法在从 Sidekiq 调用时记录到 Sidekiq 日志 并
  • 如何使用单独的 WpfControlLibrary 中的 ResourceDictionary 将样式设置为 Prism 6 模块中的 RadioButton?

    在我的 Prism 6 WPF 模块化应用程序中 我使用名为 CommonControlLibrary 的 WPF ControlLibrary 项目 该项目具有包含 ResourceDictionary 的 SwitchButtonSty
  • 字符串间隔如何工作?

    我做了一些玩这个答案 https stackoverflow com questions 25308978 what are intervals in swift ranges 25631930 25631930 甚至向 Apple 提出了