BytesParser

一个简单的面向字节的解析器/写入器。可以轻松地从二进制数据块/文件中读取和写入格式化的值!

tag Platform support License MIT Build

支持:-

在线文档

读取

BytesParser 类类型提供了从二进制数据中读取和提取有用信息的基本机制。

使用 BytesParser 对象

let reader = BytesReader(data: <some data object>)

// The magic identifier for the file is an 8-byte ascii string (not null terminated)
let magic = try reader.readString(length: 8, encoding: .ascii)
assert("8BCBAFFE", magic)

// Read 12-character little-endian encoded WCS2 (UTF16) string 
let title = try reader.readUTF16String(.little, length: 12)
let length = try reader.readInt16(.little)
let offset = try reader.readInt32(.little)

基于闭包

基于闭包的 API 隐藏了打开/关闭字节文件/数据块的一些复杂性。

示例

// Parsing a (local) crossword file
try BytesReader.read(fileURL: url) { reader in
   let checksum = try reader.readUInt16(.little)
   let magic = try reader.readStringASCII(length: 12, lengthIncludesTerminator: true)
   assert(magic, "ACROSS&DOWN")

   let width = try reader.readInt8()
   let height = try reader.readInt8()

   let clueCount = try reader.readUInt16(.little)
   ...
   let title = try parser.readStringNullTerminated(encoding: .ascii)
}

写入

BytesWriter 类允许您将格式化的二进制数据写入文件/数据。

基本流程如下:-

  1. 使用适当的初始化程序创建一个新的 BytesWriter 写入器对象
  2. 使用 .write(...) 方法写入写入器对象
  3. 在写入器上调用 complete() 以关闭任何打开的文件并使数据准备就绪。未能调用 .complete() 可能会使您的数据无法读取。

使用 BytesWriter 对象

let data: [UInt8] = [0x20, 0x40, 0x60, 0x80]

let writer = try BytesWriter()
try writer.writeBytes(data)

// Mark the content as complete
writer.complete()

let myData = writer.data()

基于闭包

基于闭包的 API 隐藏了打开/关闭写入目标的一些复杂性。 您无需在构建块的末尾调用 complete()

// Write to a Data object 
static func build(_ block: (BytesWriter) throws -> Void) throws -> Data
// Write to a file
static func build(fileURL: URL, _ block: (BytesWriter) throws -> Void) throws

示例

let message = "10. 好き 【す・き】 (na-adj) – likable; desirable"
try BytesWriter.build(fileURL: fileURL) { writer in
   // Write out a 'message' block
   // > Write out the block type identifier
   try writer.writeByte(0x78)
	// > Write message length (big-endian UInt32)
   try writer.writeUInt32(...message length in UTF32 chars..., .bigEndian)
   // > Write out the message in a big-endian, UTF32 format
   try writer.writeStringUTF32BE(message)

   // Write a 'data' block
	// > Write out the block type identifier
   try writer.writeByte(0x33)
   // > Write data length (big-endian UInt32)
   try writer.writeUInt32(...length of data..., .bigEndian)
   // > Write raw data as bytes
   try writer.writeData(...data...)
}

注意

许可证

MIT License

Copyright (c) 2024 Darren Ford

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.