Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct CustomStruct: StringProtocol, BidirectionalCollection {
- init(stringLiteral value: String) {
- self.value = value
- }
- init(_ value: String) {
- self.value = value
- }
- func uppercased() -> String {
- value.uppercased()
- }
- func lowercased() -> String {
- value.lowercased()
- }
- var utf8: String.UTF8View { value.utf8 }
- var utf16: String.UTF16View { value.utf16 }
- var unicodeScalars: String.UnicodeScalarView { value.unicodeScalars }
- init<C, Encoding>(decoding codeUnits: C, as sourceEncoding: Encoding.Type) where C : Collection, Encoding : _UnicodeEncoding, C.Element == Encoding.CodeUnit {
- value = String(decoding: codeUnits, as: sourceEncoding)
- }
- init(cString nullTerminatedUTF8: UnsafePointer<CChar>) {
- value = String(cString: nullTerminatedUTF8)
- }
- init<Encoding>(decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>, as sourceEncoding: Encoding.Type) where Encoding : _UnicodeEncoding {
- value = String(decodingCString: nullTerminatedCodeUnits, as: sourceEncoding)
- }
- func withCString<Result>(_ body: (UnsafePointer<CChar>) throws -> Result) rethrows -> Result {
- try value.withCString(body)
- }
- func withCString<Result, Encoding>(encodedAs targetEncoding: Encoding.Type, _ body: (UnsafePointer<Encoding.CodeUnit>) throws -> Result) rethrows -> Result where Encoding : _UnicodeEncoding {
- try value.withCString(encodedAs: targetEncoding, body)
- }
- func index(before i: String.Index) -> String.Index {
- value.index(before: i)
- }
- func index(after i: String.Index) -> String.Index {
- value.index(after: i)
- }
- subscript(position: String.Index) -> Character {
- get {
- value[position]
- }
- }
- var value: String // <<: this is my value!
- mutating func write(_ string: String) {
- value = string // <<: Done!
- }
- func write<Target>(to target: inout Target) where Target : TextOutputStream {
- value.write(to: &target)
- }
- var startIndex: String.Index { return value.startIndex } // <<: Done!
- var endIndex: String.Index { return value.endIndex } // <<: Done!
- var description: String { return value } // <<: Done!
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement