From 01d1387a739e054fc2bf8d727cce57b75abfbc00 Mon Sep 17 00:00:00 2001 From: Yasuhiro Hatta Date: Sat, 23 May 2026 13:48:52 +0900 Subject: [PATCH 1/2] Add swift-format.sh --- .swift-format | 9 +++++++++ swift-format.sh | 7 +++++++ 2 files changed, 16 insertions(+) create mode 100644 .swift-format create mode 100755 swift-format.sh diff --git a/.swift-format b/.swift-format new file mode 100644 index 0000000..255f796 --- /dev/null +++ b/.swift-format @@ -0,0 +1,9 @@ +{ + "indentation": { + "spaces": 4 + }, + "indentConditionalCompilationBlocks": false, + "lineLength": 200, + "spacesAroundRangeFormationOperators": true, + "version": 1 +} \ No newline at end of file diff --git a/swift-format.sh b/swift-format.sh new file mode 100755 index 0000000..646e7c1 --- /dev/null +++ b/swift-format.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env zsh + +set -eu + +SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd) + +swift format --in-place --parallel --recursive "$SCRIPT_DIR" From f878fcd0d4fbf853614efe716b9f2dc2e9ad8aaf Mon Sep 17 00:00:00 2001 From: Yasuhiro Hatta Date: Sat, 23 May 2026 13:52:11 +0900 Subject: [PATCH 2/2] Run swift-format --- Package.swift | 6 ++-- Sources/ULID/Data+Base32.swift | 50 +++++++++++++------------- Sources/ULID/ULID.swift | 14 ++++---- Tests/ULIDTests/Data+Base32Tests.swift | 28 +++++++-------- Tests/ULIDTests/ULIDTests.swift | 22 ++++++------ 5 files changed, 60 insertions(+), 60 deletions(-) diff --git a/Package.swift b/Package.swift index 677ea7d..6497a40 100644 --- a/Package.swift +++ b/Package.swift @@ -3,12 +3,12 @@ import PackageDescription let package = Package( - name: "ULID.swift", + name: "ULID.swift", platforms: [ - .iOS(.v12), .tvOS(.v12), .watchOS(.v4), .macOS(.v10_13) + .iOS(.v12), .tvOS(.v12), .watchOS(.v4), .macOS(.v10_13), ], products: [ - .library(name: "ULID", targets: ["ULID"]), + .library(name: "ULID", targets: ["ULID"]) ], targets: [ .target(name: "ULID"), diff --git a/Sources/ULID/Data+Base32.swift b/Sources/ULID/Data+Base32.swift index 10bf820..b8a3143 100644 --- a/Sources/ULID/Data+Base32.swift +++ b/Sources/ULID/Data+Base32.swift @@ -14,22 +14,22 @@ enum Base32 { static let crockfordsDecodingTable: [UInt8] = [ // 0 1 2 3 4 5 6 7 8 9 a b c d e f - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 1 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 2 - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 3 - 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x01, 0x12, 0x13, 0x01, 0x14, 0x15, 0x00, // 4 - 0x16, 0x17, 0x18, 0x19, 0x1a, 0xff, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, // 5 - 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x01, 0x12, 0x13, 0x01, 0x14, 0x15, 0x00, // 6 - 0x16, 0x17, 0x18, 0x19, 0x1a, 0xff, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, // 7 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 8 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 9 - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // a - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // b - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // c - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // d - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // e - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff // f + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 0 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 1 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 2 + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 3 + 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x01, 0x12, 0x13, 0x01, 0x14, 0x15, 0x00, // 4 + 0x16, 0x17, 0x18, 0x19, 0x1a, 0xff, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, // 5 + 0xff, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x01, 0x12, 0x13, 0x01, 0x14, 0x15, 0x00, // 6 + 0x16, 0x17, 0x18, 0x19, 0x1a, 0xff, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, // 7 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 8 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // 9 + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // a + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // b + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // c + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // d + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // e + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // f ] } @@ -69,7 +69,7 @@ extension Data { switch worklen { case 8: - dst[dsti + 4] = (work[6] << 5) | (work[7] ) + dst[dsti + 4] = (work[6] << 5) | (work[7]) fallthrough case 7: dst[dsti + 3] = (work[4] << 7) | (work[5] << 2) | (work[6] >> 3) @@ -118,26 +118,26 @@ extension Data { while srcleft > 0 { switch srcleft { case _ where 5 <= srcleft: - work[7] = src[srci + 4] - work[6] = src[srci + 4] >> 5 + work[7] = src[srci + 4] + work[6] = src[srci + 4] >> 5 fallthrough case 4: work[6] |= src[srci + 3] << 3 - work[5] = src[srci + 3] >> 2 - work[4] = src[srci + 3] >> 7 + work[5] = src[srci + 3] >> 2 + work[4] = src[srci + 3] >> 7 fallthrough case 3: work[4] |= src[srci + 2] << 1 - work[3] = src[srci + 2] >> 4 + work[3] = src[srci + 2] >> 4 fallthrough case 2: work[3] |= src[srci + 1] << 4 - work[2] = src[srci + 1] >> 1 - work[1] = src[srci + 1] >> 6 + work[2] = src[srci + 1] >> 1 + work[1] = src[srci + 1] >> 6 fallthrough case 1: work[1] |= src[srci + 0] << 2 - work[0] = src[srci + 0] >> 3 + work[0] = src[srci + 0] >> 3 default: break } diff --git a/Sources/ULID/ULID.swift b/Sources/ULID/ULID.swift index 83b459d..d78e9b5 100644 --- a/Sources/ULID/ULID.swift +++ b/Sources/ULID/ULID.swift @@ -39,17 +39,17 @@ public struct ULID: Hashable, Equatable, Comparable, CustomStringConvertible, Se $0.copyBytes(from: data.dropFirst(4)) } } - + /// Create a ULID from a timestamp and a random part. /// /// - Parameters: /// - timestamp: Specify the timestamp as `Date`. /// - data: Data representation of the random part of the ULID. /// - Returns: **nil** if the `data` is less than 80 bits or 10 bytes in size. - public init?(timestamp: Date = Date(), randomPartData data: Data){ + public init?(timestamp: Date = Date(), randomPartData data: Data) { let randomDataInBytes = 10 guard data.count >= randomDataInBytes else { return nil } - + withUnsafeMutableBytes(of: &ulid) { (buffer) in var i = 0 var millisec = UInt64(timestamp.timeIntervalSince1970 * 1000.0).bigEndian @@ -59,13 +59,13 @@ public struct ULID: Hashable, Equatable, Comparable, CustomStringConvertible, Se i += 1 } } - var randomPart:Data = Data() - if data.count > randomDataInBytes{ + var randomPart: Data = Data() + if data.count > randomDataInBytes { randomPart = data.prefix(randomDataInBytes) - }else{ + } else { randomPart = data } - + withUnsafeBytes(of: &randomPart) { for j in 0 ..< 10 { buffer[i] = $0[j] diff --git a/Tests/ULIDTests/Data+Base32Tests.swift b/Tests/ULIDTests/Data+Base32Tests.swift index f436055..3927032 100644 --- a/Tests/ULIDTests/Data+Base32Tests.swift +++ b/Tests/ULIDTests/Data+Base32Tests.swift @@ -22,7 +22,7 @@ struct Base32Tests { let bytes: [UInt8] = [ 0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0x3D, 0xD4, 0x19, 0x86, - 0xFE, 0xB3, 0xF9, 0x57, 0xAF, 0x8B, 0x6F, 0x46, 0x79, 0x77 + 0xFE, 0xB3, 0xF9, 0x57, 0xAF, 0x8B, 0x6F, 0x46, 0x79, 0x77, ] let data = Data(bytes) @@ -31,7 +31,7 @@ struct Base32Tests { @Test func testEncode1() { let bytes: [UInt8] = [ - 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 + 0b11111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, ] let data = Data(bytes) @@ -40,7 +40,7 @@ struct Base32Tests { @Test func testEncode2() { let bytes: [UInt8] = [ - 0b00000111, 0b11000000, 0b00000000, 0b00000000, 0b00000000 + 0b00000111, 0b11000000, 0b00000000, 0b00000000, 0b00000000, ] let data = Data(bytes) @@ -49,7 +49,7 @@ struct Base32Tests { @Test func testEncode3() { let bytes: [UInt8] = [ - 0b00000000, 0b00111110, 0b00000000, 0b00000000, 0b00000000 + 0b00000000, 0b00111110, 0b00000000, 0b00000000, 0b00000000, ] let data = Data(bytes) @@ -58,7 +58,7 @@ struct Base32Tests { @Test func testEncode4() { let bytes: [UInt8] = [ - 0b00000000, 0b00000001, 0b11110000, 0b00000000, 0b00000000 + 0b00000000, 0b00000001, 0b11110000, 0b00000000, 0b00000000, ] let data = Data(bytes) @@ -67,7 +67,7 @@ struct Base32Tests { @Test func testEncode5() { let bytes: [UInt8] = [ - 0b00000000, 0b00000000, 0b00001111, 0b10000000, 0b00000000 + 0b00000000, 0b00000000, 0b00001111, 0b10000000, 0b00000000, ] let data = Data(bytes) @@ -76,7 +76,7 @@ struct Base32Tests { @Test func testEncode6() { let bytes: [UInt8] = [ - 0b00000000, 0b00000000, 0b00000000, 0b01111100, 0b00000000 + 0b00000000, 0b00000000, 0b00000000, 0b01111100, 0b00000000, ] let data = Data(bytes) @@ -85,7 +85,7 @@ struct Base32Tests { @Test func testEncode7() { let bytes: [UInt8] = [ - 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11100000 + 0b00000000, 0b00000000, 0b00000000, 0b00000011, 0b11100000, ] let data = Data(bytes) @@ -94,7 +94,7 @@ struct Base32Tests { @Test func testEncode8() { let bytes: [UInt8] = [ - 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011111 + 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00011111, ] let data = Data(bytes) @@ -112,7 +112,7 @@ struct Base32Tests { @Test func testEncodePad2() { let bytes: [UInt8] = [ - 0b10000100, 0b00100001 + 0b10000100, 0b00100001, ] let data = Data(bytes) @@ -121,7 +121,7 @@ struct Base32Tests { @Test func testEncodePad3() { let bytes: [UInt8] = [ - 0b10000100, 0b00100001, 0b00001000 + 0b10000100, 0b00100001, 0b00001000, ] let data = Data(bytes) @@ -130,7 +130,7 @@ struct Base32Tests { @Test func testEncodePad4() { let bytes: [UInt8] = [ - 0b10000100, 0b00100001, 0b00001000, 0b01000010 + 0b10000100, 0b00100001, 0b00001000, 0b01000010, ] let data = Data(bytes) @@ -152,7 +152,7 @@ struct Base32Tests { @Test func testDecodeBase32() throws { let expected: [UInt8] = [ 0x00, 0x00, 0x00, 0x00, 0x01, 0x68, 0x3D, 0xD4, 0x19, 0x86, - 0xFE, 0xB3, 0xF9, 0x57, 0xAF, 0x8B, 0x6F, 0x46, 0x79, 0x77 + 0xFE, 0xB3, 0xF9, 0x57, 0xAF, 0x8B, 0x6F, 0x46, 0x79, 0x77, ] let base32String = "00000001D0YX86C6ZTSZJNXFHDQMCYBQ" @@ -194,7 +194,7 @@ struct Base32Tests { "W": 0x1c, "w": 0x1c, "X": 0x1d, "x": 0x1d, "Y": 0x1e, "y": 0x1e, - "Z": 0x1f, "z": 0x1f + "Z": 0x1f, "z": 0x1f, ] for (char, value) in table { diff --git a/Tests/ULIDTests/ULIDTests.swift b/Tests/ULIDTests/ULIDTests.swift index 1928aee..3d2afda 100644 --- a/Tests/ULIDTests/ULIDTests.swift +++ b/Tests/ULIDTests/ULIDTests.swift @@ -16,7 +16,7 @@ struct ULIDTests { @Test func testGenerateTimestamp() { let expected: [UInt8] = [ - 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0xE5 + 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0xE5, ] let timestamp = Date(timeIntervalSince1970: 1547213173.513) @@ -35,17 +35,17 @@ struct ULIDTests { #expect("01D0YHEWR9" == actual.ulidString.prefix(10)) } - + @Test func testGenerateTimestampAndRandomnes() throws { let timestamp = Date(timeIntervalSince1970: 1547213173.513) let uuidCorrectSize: [UInt8] = [ - 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0x69, 0xF4, 0xA2, 0xB1 + 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0x69, 0xF4, 0xA2, 0xB1, ] - + let actual = try #require(ULID(timestamp: timestamp, randomPartData: Data(uuidCorrectSize))) #expect(timestamp == actual.timestamp) - + #expect(0x01 == actual.ulid.6) #expect(0x68 == actual.ulid.7) #expect(0x3D == actual.ulid.8) @@ -62,13 +62,13 @@ struct ULIDTests { let timestamp = Date(timeIntervalSince1970: 1547213173.513) // Test if initializer discards bytes beyond 10 bytes let uuidTooBigSize: [UInt8] = [ - 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0x69, 0xF4, 0xA2, 0xB1, 0x99, 0x55 + 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0x69, 0xF4, 0xA2, 0xB1, 0x99, 0x55, ] - + let actual = try #require(ULID(timestamp: timestamp, randomPartData: Data(uuidTooBigSize))) #expect(timestamp == actual.timestamp) - + #expect(0x01 == actual.ulid.6) #expect(0x68 == actual.ulid.7) #expect(0x3D == actual.ulid.8) @@ -86,7 +86,7 @@ struct ULIDTests { let uuidTooSmallSize: [UInt8] = [ 0x01, 0x68, 0x3D, 0x17, 0x73, ] - + let actual = ULID(timestamp: timestamp, randomPartData: Data(uuidTooSmallSize)) #expect(actual == nil) @@ -94,7 +94,7 @@ struct ULIDTests { @Test func testGenerateRandomness() { let timestamp = Date(timeIntervalSince1970: 1547213173.513) - var generator = RandomNumberGeneratorStub(value: 0x1122334455667788) + var generator = RandomNumberGeneratorStub(value: 0x1122_3344_5566_7788) let actual = ULID(timestamp: timestamp, generator: &generator) #expect(timestamp == actual.timestamp) @@ -130,7 +130,7 @@ struct ULIDTests { @Test func testParseULIDData() throws { let expected: [UInt8] = [ 0x01, 0x68, 0x3D, 0x17, 0x73, 0x09, 0xE5, 0x2D, - 0xE2, 0x56, 0xBA, 0xB4, 0xC3, 0x4C, 0x07, 0x57 + 0xE2, 0x56, 0xBA, 0xB4, 0xC3, 0x4C, 0x07, 0x57, ] let actual = try #require(ULID(ulidData: Data(expected)))