Skip to content

Enchancement: use CFStringGetCharacters over iteration and CFStringGetCharacterAtIndex #5

@jakewilliami

Description

@jakewilliami

Currently we use CFStringGetCharacterAtIndex and iterate over the CF String to construct the String. This is obviously inefficient, and we should use CFStringGetCharacters instead.

Some example code which I haven't quite got working yet:

mutable struct CFRange
    length::Int
    location::Int
end
function _cfstring_get_characters(cfstr::Cstring, range::CFRange)
    charbuf = Vector{UInt8}(undef, range.length)
    ccall(:CFStringGetCharacters, Ptr{UInt32}, (Cstring, Ptr{Cvoid}, Ptr{Cvoid}), cfstr, pointer_from_objref(range), charbuf)
    return charbuf
end
function _string_from_cfstring(cfstr::Cstring, encoding::Unsigned = K_CFSTRING_ENCODING_MACROMAN)
    strlen = _cfstring_get_length(cfstr)
    maxsz = _cfstring_get_maximum_size_for_encoding(strlen, encoding)
    return _cfstring_get_characters(cfstr, CFRange(maxsz, 0))
end

Then, we will simple be able to write

function _string_from_cf_string(cfstr::Cstring)
    charbuf = _string_from_cfstring(cfstr)
    return String(charbuf)
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions