Skip to content

Commit b7a59be

Browse files
committed
Replace countsame with skipcharsets (→ skipchars)
1 parent bd015a3 commit b7a59be

File tree

1 file changed

+17
-25
lines changed

1 file changed

+17
-25
lines changed

src/lexer.jl

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function lexnext(state::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
100100
Token(settag(K">item", tag(state.ctx)), start - 0x1, start - 0x1), start
101101
elseif K"clock" state.ctx
102102
Token(K">clock", start - 0x1, start - 0x1), start
103-
elseif chr == UInt8('*') && pos == linestart && ischarat(bytes, pos + countsame(bytes, pos, '*'), ' ')
103+
elseif chr == UInt8('*') && pos == linestart && ischarat(bytes, skipchars(bytes, pos, '*'), ' ')
104104
lex_heading(state, bytes, pos)
105105
elseif chr == UInt8(':')
106106
if length(bytes) > pos && iswhitespace(bytes, pos + 0x1) || islineend(bytes, pos + 0x1)
@@ -186,7 +186,7 @@ end
186186
# Greater element lexing
187187

188188
function lex_heading(::LexerState, bytes::DenseVector{UInt8}, pos::UInt32)
189-
depth = countsame(bytes, pos, '*')
189+
depth = skipchars(bytes, pos, '*') - pos
190190
Token(settag(K"heading", depth % UInt8), pos, lineend(bytes, pos) - 1),
191191
pos + depth
192192
end
@@ -237,8 +237,8 @@ function lex_item(state::LexerState, bytes::DenseVector{UInt8}, linestart::UInt3
237237
nextchar(bytes, pos, (' ', '\n', '\r')) < bulletend
238238
return zero(pos)
239239
end
240-
bulletend == skipcharsets(bytes, pos, '0':'9') ||
241-
bulletend == skipcharsets(bytes, pos, 'a':'z', 'A':'Z') ||
240+
bulletend == skipchars(bytes, pos, '0':'9') ||
241+
bulletend == skipchars(bytes, pos, 'a':'z', 'A':'Z') ||
242242
return zero(pos)
243243
bulletend
244244
end
@@ -365,7 +365,7 @@ function lex_clock(::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
365365
end
366366
hasduration = if hasprefix(bytes, pos, "=>")
367367
pos = skipspaces(bytes, pos + ncodeunits("=>") % UInt32).stop
368-
pos = skipcharsets(bytes, pos, '0':'9', ':')
368+
pos = skipchars(bytes, pos, '0':'9', ':')
369369
pos = skipspaces(bytes, pos).stop
370370
true
371371
else
@@ -464,7 +464,7 @@ function lex_fixedwidth(::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
464464
end
465465

466466
function lex_hrule(::LexerState, bytes::DenseVector{UInt8}, pos::UInt32)
467-
rend = skipcharsets(bytes, pos, '-')
467+
rend = skipchars(bytes, pos, '-')
468468
rend - pos >= 5 || return NONE_TOKEN
469469
lend = lineend(bytes, pos)
470470
rend == lend ||
@@ -476,7 +476,7 @@ end
476476
function lex_latexenv(::LexerState, bytes::DenseVector{UInt8}, start::UInt32)
477477
hasprefix(bytes, start, "\\begin{") || return NONE_TOKEN
478478
namestart = start + ncodeunits("\\begin{") % UInt32
479-
nameend = skipcharsets(bytes, namestart, ('a':'z', 'A':'Z', '0':'9', '*'))
479+
nameend = skipchars(bytes, namestart, ('a':'z', 'A':'Z', '0':'9', '*'))
480480
nameend < length(bytes) && bytes[nameend] == UInt8('}') || return NONE_TOKEN
481481
namelen = nameend - namestart
482482
pos = start
@@ -768,7 +768,7 @@ function skipwords(bytes::DenseVector{UInt8}, pos::I, extras::NTuple{N, C} = ();
768768
end
769769

770770
"""
771-
skipcharsets(bytes::DenseVector{UInt8}, pos::Integer, charsets...) -> Integer
771+
skipchars(bytes::DenseVector{UInt8}, pos::Integer, charsets...) -> Integer
772772
773773
Skip over all characters in `bytes` starting at `pos` that are in the given
774774
character sets.
@@ -780,26 +780,26 @@ Each character set can be a single character or a range of characters.
780780
```julia-repl
781781
julia> strv = codeunits("abc0123 .--");
782782
783-
julia> skipcharsets(strv, 1, 'a':'z')
783+
julia> skipchars(strv, 1, 'a':'z')
784784
4
785785
786-
julia> skipcharsets(strv, 1, 'a':'z', '0':'9')
786+
julia> skipchars(strv, 1, 'a':'z', '0':'9')
787787
8
788788
789-
julia> skipcharsets(strv, 1, 'a':'z', '0':'9', ' ')
789+
julia> skipchars(strv, 1, 'a':'z', '0':'9', ' ')
790790
9
791791
792-
julia> skipcharsets(strv, 1, 'a':'z', '0':'9', ' ', '-')
792+
julia> skipchars(strv, 1, 'a':'z', '0':'9', ' ', '-')
793793
9
794794
795-
julia> skipcharsets(strv, 1, 'a':'z', '0':'9', ' ', '.')
795+
julia> skipchars(strv, 1, 'a':'z', '0':'9', ' ', '.')
796796
10
797797
798-
julia> skipcharsets(strv, 1, 'a':'z', '0':'9', ' ', '.', '-')
798+
julia> skipchars(strv, 1, 'a':'z', '0':'9', ' ', '.', '-')
799799
12
800800
```
801801
"""
802-
function skipcharsets(bytes::DenseVector{UInt8}, pos::Integer, charsets::NTuple{N, Union{StepRange{Char}, Char}}; limit::Integer = length(bytes) % typeof(pos)) where {N}
802+
function skipchars(bytes::DenseVector{UInt8}, pos::Integer, charsets::NTuple{N, Union{StepRange{Char}, Char}}; limit::Integer = length(bytes) % typeof(pos)) where {N}
803803
skipranges = map(c -> if c isa Char UInt8(c) else
804804
UInt8(first(c)):UInt8(last(c)) end,
805805
charsets)
@@ -812,8 +812,8 @@ function skipcharsets(bytes::DenseVector{UInt8}, pos::Integer, charsets::NTuple{
812812
next
813813
end
814814

815-
skipcharsets(bytes::DenseVector{UInt8}, pos::Integer, charsets::Union{StepRange{Char}, Char}...; limit::Integer = length(bytes) % typeof(pos)) =
816-
skipcharsets(bytes, pos, Tuple(charsets); limit)
815+
skipchars(bytes::DenseVector{UInt8}, pos::Integer, charsets::Union{StepRange{Char}, Char}...; limit::Integer = length(bytes) % typeof(pos)) =
816+
skipchars(bytes, pos, Tuple(charsets); limit)
817817

818818
"""
819819
skipbalanced(bytes::DenseVector{UInt8}, pos::Integer, bpair::Pair{Char, Char},
@@ -903,14 +903,6 @@ function hasprefix(bytes::DenseVector{UInt8}, start::Integer, pattern::String; l
903903
true
904904
end
905905

906-
function countsame(bytes::DenseVector{UInt8}, pos::I, char::Char; limit::I = length(bytes) % I)::I where {I <: Integer}
907-
uchar = UInt8(char)
908-
for p in pos:limit
909-
bytes[p] != uchar && return p - pos
910-
end
911-
limit + 0x1
912-
end
913-
914906
function nextchar(bytes::DenseVector{UInt8}, pos::I, char::UInt8; limit::I = length(bytes) % I)::I where {I <: Integer}
915907
for p in pos:limit
916908
bytes[p] == char && return p

0 commit comments

Comments
 (0)