Skip to content

Comments

feat!: 扩展 ByteBuffer#28

Merged
AnemoFlower merged 4 commits intomainfrom
feature/byte-buffer
Feb 1, 2026
Merged

feat!: 扩展 ByteBuffer#28
AnemoFlower merged 4 commits intomainfrom
feature/byte-buffer

Conversation

@AnemoFlower
Copy link
Member

@AnemoFlower AnemoFlower commented Feb 1, 2026

本 PR 为 ByteBuffer 添加了 readString(_:)writeString(_:) 方法,并为其它读取函数添加了 throws 关键字以便在越界时抛出错误。

closes #23

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

此 PR 为 ByteBuffer 类添加了字符串读写方法,并改进了错误处理机制,使读取操作在越界时能够抛出错误而不是崩溃。

Changes:

  • ByteBuffer 添加了 readString(_:)writeString(_:) 方法以支持 UTF-8 字符串操作
  • 为所有读取方法(readDatareadUInt8readUInt16readUInt32)添加了 throws 关键字
  • 新增了两个错误类型:noEnoughBytesfailedToDecodeString,并提供了中英文本地化支持
  • 更新了现有代码以使用新的字符串读写方法和错误处理机制
  • 移除了 Response.text 属性,改为使用新的 parse(using:) 方法

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
Sources/SwiftScaffolding/Util/ByteBuffer.swift 核心变更:添加字符串读写方法,为读取方法添加错误处理,参数类型从 Int 改为 any BinaryInteger
Sources/SwiftScaffolding/Errors.swift 新增 noEnoughBytes 和 failedToDecodeString 两个错误枚举值及其本地化描述
Sources/SwiftScaffolding/zh-Hans.lproj/Localizable.strings 添加新错误的中文本地化字符串
Sources/SwiftScaffolding/en.lproj/Localizable.strings 添加新错误的英文本地化字符串
Sources/SwiftScaffolding/Server/ScaffoldingServer.swift 更新以使用新的错误处理和 readString 方法
Sources/SwiftScaffolding/Server/RequestHandler.swift 使用 writeString 替代手动编码
Sources/SwiftScaffolding/Scaffolding.swift 添加 Response.parse 方法,移除 text 属性,更新错误处理
Sources/SwiftScaffolding/Client/ScaffoldingClient.swift 使用新的 parse 和 readString 方法简化代码
Comments suppressed due to low confidence (8)

Sources/SwiftScaffolding/Util/ByteBuffer.swift:38

  • readData 方法中缺少对负数长度的检查。当传入负数的 length 时,Int.init(exactly:) 可能会成功(对于符合范围的负数),但随后的 intLength == 0 检查会跳过,导致在第 39 行的边界检查中产生意外行为。建议在第 38 行之后添加对 intLength < 0 的检查,并在这种情况下抛出 ConnectionError.noEnoughBytes 错误。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:73
  • 文档注释不完整。readString 方法的 length 参数描述为"字符串长度",但没有说明这是以字节为单位还是以字符为单位。由于实际实现是以字节为单位读取 UTF-8 编码的数据,建议将文档改为"字符串的字节长度(UTF-8 编码)"以避免歧义。此外,应该添加 - Returns: 文档说明返回值,以及 - Throws: 文档说明可能抛出的错误。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:115
  • writeString 方法缺少文档注释中的参数说明和功能描述。建议添加 - Parameter str: 来描述输入参数的含义,并且如果该方法改为可抛出错误的话,还需要添加 - Throws: 说明。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:36
  • 错误枚举名称存在拼写错误。noEnoughBytes 应该改为 notEnoughBytes,因为正确的英语表达是 "not enough" 而不是 "no enough"。这个命名问题也需要在对应的本地化字符串键中同步修改。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:40
  • 错误枚举名称存在拼写错误。noEnoughBytes 应该改为 notEnoughBytes,因为正确的英语表达是 "not enough" 而不是 "no enough"。这个命名问题也需要在对应的本地化字符串键中同步修改。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:116
  • writeString 方法中使用了强制解包 (!)。虽然将 Swift 字符串转换为 UTF-8 数据在正常情况下不会失败,但为了与 API 的其他部分保持一致(例如 readString 会抛出错误),建议使该方法也能够抛出错误。建议将签名改为 public func writeString(_ str: String) throws,并在转换失败时抛出适当的错误。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:74
  • readString 方法中,当没有提供 length 参数时,默认值使用 data.count,这可能会导致意外行为。由于 ByteBuffer 维护了一个内部的 index,如果之前已经读取了部分数据,data.count 仍然表示整个缓冲区的长度,而不是剩余未读取的数据长度。建议将默认值改为 data.count - index,以读取从当前位置到缓冲区末尾的所有剩余数据。
    Sources/SwiftScaffolding/Util/ByteBuffer.swift:68
  • 由于 readDatareadUInt8readUInt16readUInt32 方法现在都抛出错误,应该在它们的文档注释中添加 - Throws: 说明来描述可能抛出的错误类型和条件。这对于 API 的使用者理解错误处理至关重要。

@AnemoFlower AnemoFlower merged commit 96763c2 into main Feb 1, 2026
1 check passed
@AnemoFlower AnemoFlower deleted the feature/byte-buffer branch February 1, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] 扩展 ByteBuffer

1 participant