Keeping track of how many characters written to an IO


#1

I want to create my own IO that can also keep track of how many characters that has been written to it. AFAIU, everything written to an IO boils down to a call to Base.unsafe_write(io::IOIndent, p::Ptr{UInt8}, n::UInt), however that is a bit too low level to count characters at.

Trying to intercept the writing at a higher level, by for example overloading something like Base.write(io::MyIO, x) doesn’t really work because it leads to ambiguities with e.g. Base.write(io::IO, x::Int).

Perhaps just overloading Base.write(io::MyIO, Union{String, Int64, Int32, ....}) where I list all the types that can be written in Base would work? Anyone has any better idea?


#2

Why wouldn’t you want to intercept the IO at the most primitive layer? For example, there’s an entire IO object (Core.STDOUT in base/coreio.jl) that consists of just that method. The number of bytes written at that level is simply n.


#3

Because it is much harder to count characters at the lowest level.


#4

er, yes, I saw that, but you also mentioned overloading Base.write(io::MyIO, ::Int64), which is 8 bytes and no characters.


#5

Okay, that was a mistake by me to mention it then. To give some context, I am trying to fix https://github.com/KristofferC/IOIndents.jl. It is bugged right now :stuck_out_tongue:


#6

Looking at IOIndents.jl (which looks interesting), I think you really need to keep track of the character widths, not the number of characters output, and also be careful of tab stops (which according to different standards are set to every 8 single width positions by default).
The Unicode database has information with 6 different widths (full, half, narrow, wide, ambiguous, neutral).