Skip to content

Commit 58746f9

Browse files
committed
cleanup string/data helpers
1 parent 69bf116 commit 58746f9

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Sources/PostgreSQL/Message/PostgreSQLDataRow.swift

+15-12
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,10 @@ struct PostgreSQLDataRowColumn: Decodable {
7474
}
7575
}
7676

77-
extension String {
78-
/// Parses a Date from this string with the supplied date format.
79-
func parseDate(format: String) throws -> Date {
80-
let formatter = DateFormatter()
81-
formatter.dateFormat = format
82-
guard let date = formatter.date(from: self) else {
83-
throw PostgreSQLError(identifier: "date", reason: "Malformed date: \(self)")
84-
}
85-
return date
86-
}
87-
}
77+
/// MARK: Data Helpers
8878

8979
extension Data {
80+
/// Converts this data to a fixed-width integer.
9081
func makeFixedWidthInteger<I>(_ type: I.Type = I.self) -> I where I: FixedWidthInteger {
9182
return withUnsafeBytes { (pointer: UnsafePointer<I>) -> I in
9283
return pointer.pointee.bigEndian
@@ -103,13 +94,25 @@ extension Data {
10394
}
10495
}
10596

97+
/// MARK: String Helpers
98+
10699
extension String {
100+
/// Parses a Date from this string with the supplied date format.
101+
func parseDate(format: String) throws -> Date {
102+
let formatter = DateFormatter()
103+
formatter.dateFormat = format
104+
guard let date = formatter.date(from: self) else {
105+
throw PostgreSQLError(identifier: "date", reason: "Malformed date: \(self)")
106+
}
107+
return date
108+
}
109+
107110
/// Create `Data` from hexadecimal string representation
108111
///
109112
/// This takes a hexadecimal representation and creates a `Data` object. Note, if the string has any spaces or non-hex characters (e.g. starts with '<' and with a '>'), those are ignored and only hex characters are processed.
110113
///
111114
/// - returns: Data represented by this hexadecimal string.
112-
115+
/// https://stackoverflow.com/questions/26501276/converting-hex-string-to-nsdata-in-swift
113116
func hexadecimal() -> Data? {
114117
var data = Data(capacity: count / 2)
115118

0 commit comments

Comments
 (0)