Skip to content

Commit fc9d1f8

Browse files
committed
Fix infinite recursion with _LatinChr, improve performance
1 parent 69ff43a commit fc9d1f8

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/latin.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,10 @@ function convert(::Type{<:Str{LatinCSE}}, str::MS_ByteStr)
147147
end
148148
end
149149

150-
_convert(::Type{_LatinCSE}, ch::UInt8) = _convert(ch <= 0x7f ? ASCIICSE : _LatinCSE, ch)
151150
convert(::Type{<:Str{_LatinCSE}}, ch::Unsigned) =
152-
ch < 0xff ? _convert(_LatinCSE, ch%UInt8) : strerror(StrErrors.LATIN1, ch)
151+
(ch <= 0xff
152+
? _convert(ch <= 0x7f ? ASCIICSE : _LatinCSE, ch%UInt8)
153+
: strerror(StrErrors.LATIN1, ch))
153154

154155
function convert(::Type{<:Str{_LatinCSE}}, str::MS_ByteStr)
155156
# handle zero length string quickly

src/support.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,9 @@ const Chrs = @static V6_COMPAT ? Union{Char,AbstractChar} : Chr
701701
function repeat(ch::CP, cnt::Integer) where {CP <: Chrs}
702702
C = codepoint_cse(CP)
703703
cnt > 1 && return Str(C, _repeat(EncodingStyle(C), C, codepoint(ch), cnt))
704-
cnt < 0 && repeaterr(cnt)
705-
cnt == 0 ? empty_str(C) : _convert(C, codepoint(ch))
704+
cnt == 1 && return _convert(C, codepoint(ch))
705+
cnt == 0 && return empty_str(C)
706+
repeaterr(cnt)
706707
end
707708

708709
(^)(ch::CP, cnt::Integer) where {CP <: Chrs} = repeat(ch, cnt)
@@ -849,8 +850,8 @@ end
849850
_repeat(::MultiCU, ::Type{UTF16CSE}, ch, cnt) =
850851
ch <= 0xffff ? _repeat_chr(UInt16, ch, cnt) : _repeat_chr(UInt32, get_utf16_32(ch), cnt)
851852

852-
function repeat(str::T, cnt::Integer) where {C<:CSE,T<:Str{C}}
853-
cnt < 2 && return cnt == 1 ? str : (cnt == 0 ? empty_str(C) : repeaterr(cnt))
853+
function _repeat_str(str::T, cnt) where {C<:CSE,T<:Str{C}}
854+
cnt <= 0 && (cnt < 0 ? repeaterr(cnt) : return empty_str(C))
854855
CU = codeunit(T)
855856
@preserve str begin
856857
len = ncodeunits(str)
@@ -871,6 +872,9 @@ function repeat(str::T, cnt::Integer) where {C<:CSE,T<:Str{C}}
871872
end
872873
Str(C, buf)
873874
end
875+
876+
@inline repeat(str::Str, cnt::Integer) = cnt == 1 ? str : _repeat_str(str, cnt)
877+
874878
(^)(str::T, cnt::Integer) where {T<:Str} = repeat(str, cnt)
875879

876880
# Definitions for C compatible strings, that don't allow embedded

0 commit comments

Comments
 (0)