Last active
September 16, 2019 01:03
-
-
Save eonil/6aabb7a0975fef4b3eec85ecbcfdc2c3 to your computer and use it in GitHub Desktop.
Swift slices computes hashes based on their contents.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
do { | |
let a = "ABCABC" | |
let i = a.index(a.startIndex, offsetBy: 3) | |
let b = a[a.startIndex..<i] | |
let c = a[i...] | |
var s = Set<Substring>() | |
s.insert(b) | |
s.insert(c) | |
print(b.hashValue) | |
print(c.hashValue) | |
print(s) | |
// Prints same hash with `["ABC"]`. | |
// Hash is based on content and works well. | |
} | |
do { | |
let a = [111,222,333,111,222,333] | |
let i = a.index(a.startIndex, offsetBy: 3) | |
let b = a[a.startIndex..<i] | |
let c = a[i...] | |
var s = Set<ArraySlice<Int>>() | |
s.insert(b) | |
s.insert(c) | |
print(b.hashValue) | |
print(c.hashValue) | |
print(s) | |
// Prints same hash with `[ArraySlice([111, 222, 333])]`. | |
// Hash is based on content and works well. | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment