Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.39 KB | None | 0 0
  1. struct CustomStruct: StringProtocol, BidirectionalCollection {
  2.     init(stringLiteral value: String) {
  3.         self.value = value
  4.     }
  5.    
  6.     init(_ value: String) {
  7.         self.value = value
  8.     }
  9.    
  10.     func uppercased() -> String {
  11.         value.uppercased()
  12.     }
  13.    
  14.     func lowercased() -> String {
  15.         value.lowercased()
  16.     }
  17.    
  18.     var utf8: String.UTF8View { value.utf8 }
  19.    
  20.     var utf16: String.UTF16View { value.utf16 }
  21.    
  22.     var unicodeScalars: String.UnicodeScalarView { value.unicodeScalars }
  23.    
  24.     init<C, Encoding>(decoding codeUnits: C, as sourceEncoding: Encoding.Type) where C : Collection, Encoding : _UnicodeEncoding, C.Element == Encoding.CodeUnit {
  25.         value = String(decoding: codeUnits, as: sourceEncoding)
  26.     }
  27.    
  28.     init(cString nullTerminatedUTF8: UnsafePointer<CChar>) {
  29.         value = String(cString: nullTerminatedUTF8)
  30.     }
  31.    
  32.     init<Encoding>(decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>, as sourceEncoding: Encoding.Type) where Encoding : _UnicodeEncoding {
  33.         value = String(decodingCString: nullTerminatedCodeUnits, as: sourceEncoding)
  34.     }
  35.    
  36.     func withCString<Result>(_ body: (UnsafePointer<CChar>) throws -> Result) rethrows -> Result {
  37.         try value.withCString(body)
  38.     }
  39.    
  40.     func withCString<Result, Encoding>(encodedAs targetEncoding: Encoding.Type, _ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result) rethrows -> Result where Encoding : _UnicodeEncoding {
  41.         try value.withCString(encodedAs: targetEncoding, body)
  42.     }
  43.    
  44.     func index(before i: String.Index) -> String.Index {
  45.         value.index(before: i)
  46.     }
  47.     func index(after i: String.Index) -> String.Index {
  48.         value.index(after: i)
  49.     }
  50.    
  51.     subscript(position: String.Index) -> Character {
  52.         get {
  53.             value[position]
  54.         }
  55.     }
  56.    
  57.  
  58.     var value: String  // <<: this is my value!
  59.    
  60.     mutating func write(_ string: String) {
  61.         value = string    // <<: Done!
  62.     }
  63.    
  64.     func write<Target>(to target: inout Target) where Target : TextOutputStream {
  65.         value.write(to: &target)
  66.     }
  67.    
  68.     var startIndex: String.Index { return value.startIndex }    // <<: Done!
  69.    
  70.     var endIndex: String.Index { return value.endIndex }    // <<: Done!
  71.    
  72.     var description: String { return value }    // <<: Done!
  73.  
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement