Skip to content

Commit

Permalink
fix 32-bit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stevengj committed Mar 24, 2024
1 parent b3c4f61 commit 35818a7
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Currently we only have some basic features like reading a line and splitting it.
For examples on how to generate test data and run the codes below see [`test/runtest.jl`](https://github.com/JuliaStrings/ViewReader.jl/blob/master/test/runtest.jl)

#### 1. eachlineV
**`eachlineV(file_path::String; buffer_size::Int64=10_000)`**
**`eachlineV(file_path::String; buffer_size::Int=10_000)`**


This function can be used just like the base[ `eachline` ](https://docs.julialang.org/en/v1/base/io-network/#Base.eachline " `eachline` ") in Julia. The argument `buffer_size` determines the size of the underlaying UInt8 vector. The `buffer_size` should be bigger than the longest line in a file. If this is uknown just use a big number like 1M. This function will throw a warning if no new line is found when the eof is not reached yet - giving a clue to increase the `buffer_size`.
Expand Down
14 changes: 7 additions & 7 deletions src/FileReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ using StringViews

struct BufferedReader{IOT <: IO}
io::IOT
buffer::Int64
tot_alloc::Int64
buffer::Int
tot_alloc::Int
arr::Vector{UInt8}
end

# Function to flip elements in an array to a specified offset(buffer size here)
function flip!(arr::Vector{UInt8}, buffer::Int64)
function flip!(arr::Vector{UInt8}, buffer::Int)
@inbounds @simd for i in 1:buffer
arr[i] = arr[i+buffer]
end
Expand All @@ -36,7 +36,7 @@ function read_next_chunk!(reader::BufferedReader)
end
end

function find_newline(reader::BufferedReader, state::Int64)
function find_newline(reader::BufferedReader, state::Int)
cur_stop = copy(state) + 1

@inbounds for i in (state + 1):reader.tot_alloc
Expand All @@ -48,7 +48,7 @@ function find_newline(reader::BufferedReader, state::Int64)
return 0:0, cur_stop
end

function eachlineV(io::IO; buffer_size::Int64=10_000)
function eachlineV(io::IO; buffer_size::Int=10_000)
# Allocate buffer array
tot_alloc = buffer_size * 2
buffer_arr = zeros(UInt8, tot_alloc)
Expand All @@ -62,7 +62,7 @@ function eachlineV(io::IO; buffer_size::Int64=10_000)
return reader
end

function eachlineV(file_path::String; buffer_size::Int64=10_000)
function eachlineV(file_path::String; buffer_size::Int=10_000)
io = open(file_path, "r")
return eachlineV(io, buffer_size=buffer_size)
end
Expand All @@ -84,7 +84,7 @@ end
return StringView(view(reader.arr, r)), state
end

@inline function Base.iterate(reader::BufferedReader, state::Int64)
@inline function Base.iterate(reader::BufferedReader, state::Int)
r, state = find_newline(reader, state)
if r.start == 0
if !eof(reader.io)
Expand Down
24 changes: 12 additions & 12 deletions src/LineReader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
# - and split the line on a specified delimiter
###########################################################################

const Sview = StringView{SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int64}}, true}}
const Bview = SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int64}}, true}
const Sview = StringView{SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int}}, true}}
const Bview = SubArray{UInt8, 1, Vector{UInt8}, Tuple{UnitRange{Int}}, true}

struct Line
arr::Bview
delimiter::UInt8
end

function Base.getindex(l::Line, index::Int64)
function Base.getindex(l::Line, index::Int)
for (i, item) in enumerate(l)
if i == index
return item
end
end
return item
end
end
error("Index out of range")
end


function find_delimiter(line::Bview, delimiter::UInt8, state::Int)
function find_delimiter(line::Bview, delimiter::UInt8, state::Int)
# State refers to the last location we scanned
@inbounds for i in state+1:length(line)
if line[i] == delimiter
return i
return i
elseif i == length(line) # i.e. last cut of this line
return i + 1 # For other del. we do -1 later to exclude it hence + 1 here
end
Expand All @@ -42,17 +42,17 @@ end
return StringView(view(line.arr, 1:loc-1)), loc
end

@inline function Base.iterate(line::Line, state::Int64)
@inline function Base.iterate(line::Line, state::Int)
loc = find_delimiter(line.arr, line.delimiter, state)
if loc == 0
return nothing
return nothing
end
return StringView(view(line.arr, state+1:loc-1)), loc
end

# For now this only support a single Char, but technically
# For now this only support a single Char, but technically
# we can just expand this to an arbitrary String
@inline function splitV(line::Sview, delimiter::Char)
length(line) > 0 || error("Empty line given")
return Line(line.data, UInt8(delimiter))
end
end
6 changes: 3 additions & 3 deletions src/Utils.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
###########################################################################
# Some basic non-alloc helpers
# Some basic non-alloc helpers
######################################eachlineV#####################################
# just for illustration now
# just for illustration now

function parseV(t::Type, lineSub::Sview)
parse(t, StringView(lineSub))
Expand All @@ -12,7 +12,7 @@ end
# end

# function Int64V(lineSub::Sview)
# parse(Int64, StringView(lineSub))
# parse(Int, StringView(lineSub))
# end

# function UInt64V(lineSub::Sview)
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const stringFile = "data/test.txt"
const numbFile = "data/numbs.txt"

# To create some random line data
function gen_string_data(copies::Int64)
function gen_string_data(copies::Int)
open(stringFile, "w") do handle
txt = "Text\twithout\tletter\nbla\tbla\tTARGET\tbla\tbla\nblablabla\nTEST\n"
corpus = txt^copies
Expand All @@ -13,7 +13,7 @@ function gen_string_data(copies::Int64)
end

# To create some random number data
function gen_numb_data(copies::Int64)
function gen_numb_data(copies::Int)
open(numbFile, "w") do handle
write(handle, "1\n13\t15\t18\n11\t10\t15\n"^copies)
end
Expand Down

0 comments on commit 35818a7

Please sign in to comment.