@@ -348,6 +348,50 @@ function skipwhitespace!(state::State)
348
348
end
349
349
end
350
350
351
+ """
352
+ read_while!(f::Function, state::Base.Stateful, lastchar::Char)
353
+
354
+ Read `state` until `f(::Char)` is `false`.
355
+
356
+ Given a `Stateful` that iterates `(_, char::Char)` pairs, and a predicate
357
+ `f(::Char)::Bool`, return `(str, lastchar)`, where `str::String` contains all the
358
+ `char` for which `f(char) == true`, and `lastchar` the last `char` element seen,
359
+ or the input `lastchar` there are no elements of `state`.
360
+
361
+ # Examples
362
+
363
+ ```julia-repl
364
+ julia> s = Base.Stateful(pairs("abc"));
365
+
366
+ julia> read_while!(isnumeric, s, 'w')
367
+ ("", 'a')
368
+
369
+ julia> first(s) # s is mutated
370
+ 2 => 'b'
371
+
372
+ julia> read_while!(isascii, Base.Stateful(pairs("123Σω")), 'k')
373
+ ("123", 'Σ')
374
+
375
+ julia> read_while!(isascii, Base.Stateful(pairs("abcde")), 'α')
376
+ ("abcde", 'e')
377
+
378
+ julia> read_while!(isascii , Base.Stateful(pairs("")), 'k')
379
+ ("", 'k')
380
+ ```
381
+ """
382
+ function read_while! (f:: Function , state:: Base.Stateful , lastchar:: Char )
383
+ v = Char[]
384
+ for (_, char:: Char ) in state
385
+ lastchar = char
386
+ if f (char)
387
+ push! (v, char)
388
+ else
389
+ break
390
+ end
391
+ end
392
+ if isempty (v) " " else String (v) end , lastchar
393
+ end
394
+
351
395
"""
352
396
begin_style!(state::State, i::Int, char::Char)
353
397
@@ -438,16 +482,8 @@ it to `newstyles`.
438
482
"""
439
483
function read_inlineface! (state:: State , i:: Int , char:: Char , newstyles)
440
484
# Substructure parsing helper functions
441
- function readalph! (state, lastchar)
442
- Iterators. takewhile (
443
- c -> ' a' <= (lastchar = last (c)) <= ' z' , state. s) |>
444
- collect .| > last |> String, lastchar
445
- end
446
- function readsymbol! (state, lastchar)
447
- Iterators. takewhile (
448
- c -> (lastchar = last (c)) ∉ (' ' , ' \t ' , ' \n ' , ' \r ' , ' ,' , ' )' ), state. s) |>
449
- collect .| > last |> String, lastchar
450
- end
485
+ readalph! (state, lastchar) = read_while! (c -> ' a' <= c <= ' z' , state. s, lastchar)
486
+ readsymbol! (state, lastchar) = read_while! (∉ ((' ' , ' \t ' , ' \n ' , ' \r ' , ' ,' , ' )' )), state. s, lastchar)
451
487
function parsecolor (color:: String )
452
488
if color == " nothing"
453
489
elseif startswith (color, ' #' ) && length (color) == 7
@@ -554,18 +590,14 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
554
590
lastchar = last (popfirst! (state. s))
555
591
break
556
592
end
557
- facename = Iterators. takewhile (
558
- c -> (lastchar = last (c)) ∉ (' ,' , ' ]' , ' )' ), state. s) |>
559
- collect .| > last |> String
593
+ facename, lastchar = read_while! (∉ ((' ,' , ' ]' , ' )' )), state. s, lastchar)
560
594
push! (inherit, Symbol (rstrip (facename)))
561
595
if lastchar != ' ,'
562
596
break
563
597
end
564
598
end
565
599
else
566
- facename = Iterators. takewhile (
567
- c -> (lastchar = last (c)) ∉ (' ,' , ' ]' , ' )' ), state. s) |>
568
- collect .| > last |> String
600
+ facename, lastchar = read_while! (∉ ((' ,' , ' ]' , ' )' )), state. s, lastchar)
569
601
push! (inherit, Symbol (rstrip (facename)))
570
602
end
571
603
inherit, lastchar
@@ -614,9 +646,8 @@ function read_inlineface!(state::State, i::Int, char::Char, newstyles)
614
646
if isnextchar (state, ' "' )
615
647
readexpr! (state, first (peek (state. s))) |> first
616
648
else
617
- Iterators. takewhile (
618
- c -> (lastchar = last (c)) ∉ (' ,' , ' )' ), state. s) |>
619
- collect .| > last |> String
649
+ str, lastchar = read_while! (∉ ((' ,' , ' )' )), state. s, lastchar)
650
+ str
620
651
end
621
652
elseif key == :height
622
653
if isnextchar (state, (' .' , ' 0' :' 9' ... ))
0 commit comments