gpt4 book ai didi

regex - 使用 NSRegularExpression 匹配的奇怪字符串范围行为

转载 作者:行者123 更新时间:2023-11-28 16:06:08 25 4
gpt4 key购买 nike

我正在尝试解析原始 HTTP 响应,但在尝试将 NSRange 转换为范围时得到的范围不正确。这是来自 playground 的相关代码:

public extension NSRange {
public func toStringRange(_ str: String) -> Range<String.Index>? {
guard str.characters.count >= length - location && location < str.characters.count else { return nil }
let fromIdx = str.characters.index(str.startIndex, offsetBy: self.location)
print("from: \(self.location) = \(fromIdx)")
let toIdx = str.characters.index(fromIdx, offsetBy: self.length)
return fromIdx..<toIdx
}
}

let responseString = "HTTP/1.0 200 OK\r\nContent-Length: 193\r\nContent-Type: application/json\r\n"
let responseRange = NSRange(location: 0, length: responseString.characters.count)
let responseRegex = try! NSRegularExpression(pattern: "^(HTTP/1.\\d) (\\d+) (.*?\r\n)(.*)", options: [.anchorsMatchLines])
guard let matchResult = responseRegex.firstMatch(in: responseString, options: [], range: responseRange),
matchResult.numberOfRanges == 5,
let versionRange = matchResult.rangeAt(1).toStringRange(responseString),
let statusRange = matchResult.rangeAt(2).toStringRange(responseString),
let headersRange = matchResult.rangeAt(4).toStringRange(responseString)
else { fatalError() }

toStringRange() 中打印的输出是

from: 0 = Index(_base: Swift.String.UnicodeScalarView.Index(_position: 0), _countUTF16: 1)
from: 9 = Index(_base: Swift.String.UnicodeScalarView.Index(_position: 9), _countUTF16: 1)
from: 17 = Index(_base: Swift.String.UnicodeScalarView.Index(_position: 18), _countUTF16: 1)

为什么第三个 toStringRange() 调用返回的字符串范围从 18 而不是 17 开始?

最佳答案

您的转换方法来自 NSRangeRange<String.Index>才不是对于外部的扩展字素簇和字符可以正常工作“基本的多语言平面”(表情符号、旗帜等)。

NSRange计算 UTF-16 代码点(对应于 unicharNSString 中表示). Range<String.Index>计数 swift Characters表示扩展的字素簇。

在您的具体案例中,"\r\n"算作两个 UTF-16 代码点,但是作为一个Character ,这会导致不需要的“转变”。

这是一个简化的例子:

let responseString = "OK\r\nContent-Length"

let nsRange = (responseString as NSString).range(of: "Content")
print(nsRange.location, nsRange.length) // 4 7

if let sRange1 = nsRange.toStringRange(responseString) {
print(responseString.substring(with: sRange1)) // "ontent-"
}

使用方法

extension String {
func range(from nsRange: NSRange) -> Range<String.Index>? {
guard
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
let from = String.Index(from16, within: self),
let to = String.Index(to16, within: self)
else { return nil }
return from ..< to
}
}

来自 NSRange to Range<String.Index>你会得到预期的结果:

if let sRange2 = responseString.range(from: nsRange) {
print(responseString.substring(with: sRange2)) // "Content"
}

关于regex - 使用 NSRegularExpression 匹配的奇怪字符串范围行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40249780/

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