Skip to content

Commit

Permalink
Move scratch to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
green-nsk committed Jul 2, 2021
1 parent 69d08c8 commit a11ceed
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
1 change: 1 addition & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ include("io.jl")
include("iobuffer.jl")

# strings & printing
include("scratch.jl")
include("intfuncs.jl")
include("strings/strings.jl")
include("regex.jl")
Expand Down
27 changes: 0 additions & 27 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -639,33 +639,6 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba

## integer to string functions ##

const PRINT_SCRATCH_LENGTH = 256
struct Buf
data::NTuple{PRINT_SCRATCH_LENGTH,UInt8}

Buf(::UndefInitializer) = new()
end

struct Scratch
p::Ptr{UInt8}
length::UInt64
end

function with_scratch(f, n)
buf = Ref(Buf(undef))
GC.@preserve buf f(Scratch(pointer_from_objref(buf), unsafe_trunc(UInt64, n)))
end

@propagate_inbounds function setindex!(a::Scratch, v, i)
@boundscheck (i <= a.length || throw(BoundsError(a, i)))
unsafe_store!(a.p, convert(UInt8, v), i)
a
end

write(io::IO, a::Scratch) = unsafe_write(io, a.p, a.length)

String(s::Scratch) = unsafe_string(s.p, s.length)

@inline function bin_impl_(a, x::Unsigned, n::Int, neg::Bool)
# for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
# @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
Expand Down
26 changes: 26 additions & 0 deletions base/scratch.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const PRINT_SCRATCH_LENGTH = 256
struct ScratchBuf
data::NTuple{PRINT_SCRATCH_LENGTH,UInt8}

ScratchBuf(::UndefInitializer) = new()
end

struct Scratch
p::Ptr{UInt8}
length::UInt64
end

function with_scratch(f, n)
buf = Ref(ScratchBuf(undef))
GC.@preserve buf f(Scratch(pointer_from_objref(buf), unsafe_trunc(UInt64, n)))
end

@propagate_inbounds function setindex!(a::Scratch, v, i)
@boundscheck (i <= a.length || throw(BoundsError(a, i)))
unsafe_store!(a.p, convert(UInt8, v), i)
a
end

write(io::IO, a::Scratch) = unsafe_write(io, a.p, a.length)

String(s::Scratch) = unsafe_string(s.p, s.length)

0 comments on commit a11ceed

Please sign in to comment.