Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a function over a closure to handle poor inference #62

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/internals.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ StyledStrings.StyledMarkup.escaped!
StyledStrings.StyledMarkup.interpolated!
StyledStrings.StyledMarkup.readexpr!
StyledStrings.StyledMarkup.skipwhitespace!
StyledStrings.StyledMarkup.read_while!
StyledStrings.StyledMarkup.begin_style!
StyledStrings.StyledMarkup.end_style!
StyledStrings.StyledMarkup.read_annotation!
Expand Down
69 changes: 50 additions & 19 deletions src/styledmarkup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,50 @@ function skipwhitespace!(state::State)
end
end

"""
read_while!(f::Function, state::Base.Stateful, lastchar::Char)

Read `state` until `f(::Char)` is `false`.

Given a `Stateful` that iterates `(_, char::Char)` pairs, and a predicate
`f(::Char)::Bool`, return `(str, lastchar)`, where `str::String` contains all the
`char` for which `f(char) == true`, and `lastchar` the last `char` element seen,
or the input `lastchar` there are no elements of `state`.

# Examples

```julia-repl
julia> s = Base.Stateful(pairs("abc"));

julia> read_while!(isnumeric, s, 'w')
("", 'a')

julia> first(s) # s is mutated
2 => 'b'

julia> read_while!(isascii, Base.Stateful(pairs("123Σω")), 'k')
("123", 'Σ')

julia> read_while!(isascii, Base.Stateful(pairs("abcde")), 'α')
("abcde", 'e')

julia> read_while!(isascii , Base.Stateful(pairs("")), 'k')
("", 'k')
```
"""
function read_while!(f::Function, state::Base.Stateful, lastchar::Char)
v = Char[]
for (_, char::Char) in state
lastchar = char
if f(char)
push!(v, char)
else
break
end
end
if isempty(v) "" else String(v) end, lastchar
end

"""
begin_style!(state::State, i::Int, char::Char)

Expand Down Expand Up @@ -438,16 +482,8 @@ it to `newstyles`.
"""
function read_inlineface!(state::State, i::Int, char::Char, newstyles)
# Substructure parsing helper functions
function readalph!(state, lastchar)
Iterators.takewhile(
c -> 'a' <= (lastchar = last(c)) <= 'z', state.s) |>
collect .|> last |> String, lastchar
end
function readsymbol!(state, lastchar)
Iterators.takewhile(
c -> (lastchar = last(c)) ∉ (' ', '\t', '\n', '\r', ',', ')'), state.s) |>
collect .|> last |> String, lastchar
end
readalph!(state, lastchar) = read_while!(c -> 'a' <= c <= 'z', state.s, lastchar)
readsymbol!(state, lastchar) = read_while!(∉((' ', '\t', '\n', '\r', ',', ')')), state.s, lastchar)
function parsecolor(color::String)
if color == "nothing"
elseif startswith(color, '#') && length(color) == 7
Expand Down Expand Up @@ -554,18 +590,14 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
lastchar = last(popfirst!(state.s))
break
end
facename = Iterators.takewhile(
c -> (lastchar = last(c)) ∉ (',', ']', ')'), state.s) |>
collect .|> last |> String
facename, lastchar = read_while!(∉((',', ']', ')')), state.s, lastchar)
push!(inherit, Symbol(rstrip(facename)))
if lastchar != ','
break
end
end
else
facename = Iterators.takewhile(
c -> (lastchar = last(c)) ∉ (',', ']', ')'), state.s) |>
collect .|> last |> String
facename, lastchar = read_while!(∉((',', ']', ')')), state.s, lastchar)
push!(inherit, Symbol(rstrip(facename)))
end
inherit, lastchar
Expand Down Expand Up @@ -614,9 +646,8 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
if isnextchar(state, '"')
readexpr!(state, first(peek(state.s))) |> first
else
Iterators.takewhile(
c -> (lastchar = last(c)) ∉ (',', ')'), state.s) |>
collect .|> last |> String
str, lastchar = read_while!(∉((',', ')')), state.s, lastchar)
str
end
elseif key == :height
if isnextchar(state, ('.', '0':'9'...))
Expand Down
Loading