I've built a little structure which holds a pseudo-C-String UnsafeMutablePointer. Now I wanted to add a way to get the length of the String, but something behaved very strange:
struct Char {
let value: CChar
init(_ char: CChar) {
self.value = char
}
}
extension Char: CustomStringConvertible {
var description: String {
return String(UnicodeScalar(UInt8(bitPattern: self.value)))
}
}
extension Char: Equatable {
public static func ==(lhs: Char, rhs: Char) -> Bool {
return lhs.value == rhs.value
}
}
struct CharPointer {
let pointer: UnsafeMutablePointer<Char>
init(_ string: String) {
let chars = string.cString(using: .utf8)!.map { Char($0) }
self.pointer = UnsafeMutablePointer(mutating: chars)
}
subscript(_ offset: Int) -> Char {
get { return self.pointer[offset] }
set { self.pointer[offset] = newValue }
}
var length: Int {
var ptr = self.pointer
while ptr.pointee != Char(0) {
ptr += 1
}
return ptr - self.pointer
}
}
let pointer = CharPointer("Hello World!")
print(pointer[0])
// print(pointer.length)
If I don't use the length
property, everything works just fine and the print statement prints an "H". But if I try to access pointer.length
, it prints a newline, although I use the property after the print statement, and the length is 0. Is this a bug or have I made a mistake?
Thanks to @Hamish I found a solution, I just initialize a second pointer with the pointer from the array: