@@ -287,6 +287,24 @@ extension String {
287
287
public func _stdlib_compareNSStringDeterministicUnicodeCollation(
288
288
lhs: AnyObject , rhs: AnyObject
289
289
) -> Int32
290
+ #else
291
+ /// Compare two strings using the current locale and as defined by the
292
+ /// platform's clib. Unlike the above, this may or may not do normalization,
293
+ /// depending on the platform.
294
+ /// FIXME: Make this do unicode normalization?
295
+ @asmname ( " strcoll " )
296
+ public func _stdlib_strcoll(
297
+ lhs: UnsafePointer < Int8 > , rhs: UnsafePointer < Int8 >
298
+ ) -> CInt
299
+
300
+ public func _stdlib_strcoll_wrap( lhs: String , rhs: String ) -> CInt {
301
+ var res : CInt
302
+ return lhs. withCString { lhs -> CInt in
303
+ return rhs. withCString { rhs -> CInt in
304
+ return _stdlib_strcoll ( lhs, rhs)
305
+ }
306
+ }
307
+ }
290
308
#endif
291
309
292
310
extension String : Equatable {
@@ -308,9 +326,7 @@ public func ==(lhs: String, rhs: String) -> Bool {
308
326
return _stdlib_compareNSStringDeterministicUnicodeCollation (
309
327
lhs. _bridgeToObjectiveCImpl ( ) , rhs. _bridgeToObjectiveCImpl ( ) ) == 0
310
328
#else
311
- // FIXME: Actually implement. For now, all strings are unequal.
312
- // rdar://problem/18878343
313
- return false
329
+ return _stdlib_strcoll_wrap ( lhs, rhs) == 0
314
330
#endif
315
331
}
316
332
@@ -324,9 +340,7 @@ extension String {
324
340
return _stdlib_compareNSStringDeterministicUnicodeCollation (
325
341
self . _bridgeToObjectiveCImpl ( ) , rhs. _bridgeToObjectiveCImpl ( ) ) < 0
326
342
#else
327
- // FIXME: Actually implement. For now, all strings are unequal
328
- // rdar://problem/18878343
329
- return false
343
+ return _stdlib_strcoll_wrap ( self , rhs) < 0
330
344
#endif
331
345
}
332
346
@@ -391,23 +405,29 @@ extension String : Hashable {
391
405
/// different invocations of the same program. Do not persist the
392
406
/// hash value across program runs.
393
407
public var hashValue : Int {
408
+ #if _runtime(_ObjC)
394
409
// Mix random bits into NSString's hash so that clients don't rely on
395
410
// Swift.String.hashValue and NSString.hash being the same.
396
411
#if arch(i386) || arch(arm)
397
412
let hashOffset = 0x88ddcc21
398
413
#else
399
414
let hashOffset = 0x429b126688ddcc21
400
415
#endif
401
- #if _runtime(_ObjC)
402
416
// FIXME(performance): constructing a temporary NSString is extremely
403
417
// wasteful and inefficient.
404
418
let cocoaString =
405
419
unsafeBitCast ( self . _bridgeToObjectiveCImpl ( ) , _NSStringCoreType. self)
406
420
return hashOffset ^ _stdlib_NSStringNFDHashValue ( cocoaString)
407
421
#else
408
- // FIXME: Actually implement. For now, all strings have the same hash.
409
- // rdar://problem/18878343
410
- return hashOffset
422
+ // FIXME: A better algorithm? Maybe murmur? This is djb2, which is fairly
423
+ // common and simple.
424
+ // FIXME: Like string comparison above, this does not normalize the unicode
425
+ // string like the NSString callout does.
426
+ var hash = 5381
427
+ for char in UTF8View ( _core) {
428
+ hash = ( ( hash << 5 ) &+ hash) &+ Int ( char)
429
+ }
430
+ return hash
411
431
#endif
412
432
}
413
433
}
0 commit comments