ProtobufUtility.swift 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // ProtobufRuntime/Sources/Protobuf/ProtobufUtility.swift - Utility methods
  2. //
  3. // This source file is part of the Swift.org open source project
  4. //
  5. // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See http://swift.org/LICENSE.txt for license information
  9. // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
  10. //
  11. // -----------------------------------------------------------------------------
  12. ///
  13. /// In Swift 3, the standard library does not include == or != for [UInt8],
  14. /// so we define these utilities.
  15. ///
  16. // -----------------------------------------------------------------------------
  17. import Swift
  18. public func ==(lhs: [[UInt8]], rhs: [[UInt8]]) -> Bool {
  19. guard lhs.count == rhs.count else {return false}
  20. for (l,r) in zip(lhs, rhs) {
  21. if l != r {
  22. return false
  23. }
  24. }
  25. return true
  26. }
  27. public func !=(lhs: [[UInt8]], rhs: [[UInt8]]) -> Bool {
  28. return !(lhs == rhs)
  29. }
  30. public func ==<T: Equatable>(lhs: Dictionary<T, [UInt8]>, rhs: Dictionary<T, [UInt8]>) -> Bool {
  31. guard lhs.count == rhs.count else {return false}
  32. for (k,v) in lhs {
  33. let rv = rhs[k]
  34. if rv == nil || rv! != v {
  35. return false
  36. }
  37. }
  38. return true
  39. }
  40. public func !=<T: Equatable>(lhs: Dictionary<T, [UInt8]>, rhs: Dictionary<T, [UInt8]>) -> Bool {
  41. return !(lhs == rhs)
  42. }