-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Feature/unsigned int #1091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Feature/unsigned int #1091
Changes from all commits
e58e323
17d4b90
69bac35
14734e0
9a75697
6537109
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,27 @@ extension Int64: Number, Value { | |
|
||
} | ||
|
||
extension UInt64: Number, Value { | ||
|
||
public static let declaredDatatype = Blob.declaredDatatype | ||
|
||
public static func fromDatatypeValue(_ datatypeValue: Blob) -> UInt64 { | ||
guard datatypeValue.bytes.count >= MemoryLayout<UInt64>.size else { return 0 } | ||
let bigEndianUInt64 = datatypeValue.bytes.withUnsafeBytes({ $0.load(as: UInt64.self )}) | ||
return UInt64(bigEndian: bigEndianUInt64) | ||
} | ||
|
||
public var datatypeValue: Blob { | ||
var bytes: [UInt8] = [] | ||
withUnsafeBytes(of: self) { pointer in | ||
// little endian by default on iOS/macOS, so reverse to get bigEndian | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about Linux? Will this work everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah you're right, this would likely fail on non-macOS. i'll look for the right way to get defined bit order regardless of system. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the simplest way here would be withUnsafeBytes(of: bigEndian) { pointer in
bytes.append(contentsOf: pointer)
} however, why is it stored as big endian by default? given that it implies a conversion in most cases. |
||
bytes.append(contentsOf: pointer.reversed()) | ||
} | ||
return Blob(bytes: bytes) | ||
} | ||
|
||
} | ||
|
||
extension String: Binding, Value { | ||
|
||
public static let declaredDatatype = "TEXT" | ||
|
@@ -130,3 +151,17 @@ extension Int: Number, Value { | |
} | ||
|
||
} | ||
|
||
extension UInt32: Number, Value { | ||
|
||
public static var declaredDatatype = Int64.declaredDatatype | ||
|
||
public static func fromDatatypeValue(_ datatypeValue: Int64) -> UInt32 { | ||
UInt32(datatypeValue) | ||
} | ||
|
||
public var datatypeValue: Int64 { | ||
Int64(self) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wouldn't it be easier to change the iteration from
0
tobytes.count
to avoid having to doreversed
in the first place?