# How to tell if readuntil got a delimiter

**URL:** <https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 17, 2018, 1:35pm UTC](https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068 "2018-09-17T13:35:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 17, 2018, 1:35pm UTC](https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068/1 "2018-09-17T13:35:55Z")

</div>

I find that `readuntil` is very efficient, but in case it reaches EOF, I would like to find a way to verify that it got a delimiter. Eg

```julia
io = IOBuffer("foo")
readuntil(io, ';'))
eof(io) # true, but how can I tell there was no ';'?

```

I could set `keep = true`, and test for it, but that seems cumbersome because then I would need to split it off.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [September 17, 2018, 4:47pm UTC](https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068/2 "2018-09-17T16:47:19Z")

</div>

It **seems** (maybe I am wrong and it could bring disaster 😛 ) that this could help:

```julia
# delimfound has meaning only after readuntil!
julia> delimfound(io, delim::Char) = eof(io) ? Char(take!(io)[end])==delim : true

# or you could do
julia> delim = ';' % UInt8;
julia> delimfound(io, delim::UInt8) = eof(io) ? take!(io)[end]==delim : true
julia> readuntil(io, delim) # this would be a little faster too

```

Idea: if you read to eof then take the buffered data and check if there was delimiter in the end.

It worked in my tests under julia1.0 with IOBuffer and IOStream, but I am not sure if it will work with your Type\<:IO.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 17, 2018, 5:49pm UTC](https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068/3 "2018-09-17T17:49:17Z")

</div>

Perhaps you missed that the `IOBuffer` is there just to create an MWE, the problem is of course for a general `<:IO`.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [September 17, 2018, 7:02pm UTC](https://discourse.julialang.org/t/how-to-tell-if-readuntil-got-a-delimiter/15068/4 "2018-09-17T19:02:17Z")

</div>

> [@Tamas\_Papp](#):
>
> Perhaps you missed that

No. Did you miss that I wrote about IOStream too? Or about Type\<:IO??  
(BTW with IOBuffer it is easier you could do io.Ptr-=1 and then read last byte…)

But there is another problem wich make my workaround useless! For example for `io = open("file")` if file\_size == n \* cache\_buffer\_size (131072 in my case) there is `eof(io) == false` after reading last byte. And unfortunately cache is cleared by reading no bytes to get `eof(io) == true` so `take!(io)` return empty vector.

I am curious if there are other possibilities than fix readuntil to solve your problem.
