Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"indentation": {
"spaces": 4
},
"indentConditionalCompilationBlocks": false,
"lineLength": 200,
"spacesAroundRangeFormationOperators": true,
"version": 1
}
6 changes: 3 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
50 changes: 25 additions & 25 deletions Sources/ULID/Data+Base32.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
]

}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 7 additions & 7 deletions Sources/ULID/ULID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand Down
28 changes: 14 additions & 14 deletions Tests/ULIDTests/Data+Base32Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -112,7 +112,7 @@ struct Base32Tests {

@Test func testEncodePad2() {
let bytes: [UInt8] = [
0b10000100, 0b00100001
0b10000100, 0b00100001,
]
let data = Data(bytes)

Expand All @@ -121,7 +121,7 @@ struct Base32Tests {

@Test func testEncodePad3() {
let bytes: [UInt8] = [
0b10000100, 0b00100001, 0b00001000
0b10000100, 0b00100001, 0b00001000,
]
let data = Data(bytes)

Expand All @@ -130,7 +130,7 @@ struct Base32Tests {

@Test func testEncodePad4() {
let bytes: [UInt8] = [
0b10000100, 0b00100001, 0b00001000, 0b01000010
0b10000100, 0b00100001, 0b00001000, 0b01000010,
]
let data = Data(bytes)

Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions Tests/ULIDTests/ULIDTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -86,15 +86,15 @@ struct ULIDTests {
let uuidTooSmallSize: [UInt8] = [
0x01, 0x68, 0x3D, 0x17, 0x73,
]

let actual = ULID(timestamp: timestamp, randomPartData: Data(uuidTooSmallSize))

#expect(actual == nil)
}

@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)
Expand Down Expand Up @@ -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)))
Expand Down
7 changes: 7 additions & 0 deletions swift-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env zsh

set -eu

SCRIPT_DIR=$(cd "$(dirname "$0")"; pwd)

swift format --in-place --parallel --recursive "$SCRIPT_DIR"
Loading