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

foldr fails for unicode strings #31780

Closed
stevengj opened this issue Apr 20, 2019 · 4 comments · Fixed by #31781
Closed

foldr fails for unicode strings #31780

stevengj opened this issue Apr 20, 2019 · 4 comments · Fixed by #31781
Labels
domain:strings "Strings!" domain:unicode Related to unicode characters and encodings kind:bug Indicates an unexpected problem or unintended behavior

Comments

@stevengj
Copy link
Member

stevengj commented Apr 20, 2019

As reported on discourse:

julia> foldr((x, y) -> (x * '|' * y), "λ x.x")
ERROR: StringIndexError("λ x.x", 2)
Stacktrace:
 [1] string_index_err(::String, ::Int64) at ./strings/string.jl:12
 [2] getindex_continued(::String, ::Int64, ::UInt32) at ./strings/string.jl:218
 [3] getindex at ./strings/string.jl:211 [inlined]
 [4] mapfoldr_impl(::typeof(identity), ::getfield(Main, Symbol("##5#6")), ::NamedTuple{(:init,),Tuple{Char}}, ::String, ::Int64) at ./reduce.jl:104
 [5] mapfoldr_impl(::Function, ::Function, ::NamedTuple{(),Tuple{}}, ::String, ::Int64) at ./reduce.jl:115
 [6] #mapfoldr#189 at ./reduce.jl:125 [inlined]
 [7] mapfoldr at ./reduce.jl:125 [inlined]
 [8] #foldr#190 at ./reduce.jl:144 [inlined]
 [9] foldr(::Function, ::String) at ./reduce.jl:144
 [10] top-level scope at none:0

I would consider this a bug, not just a limitation of foldr, because it silently works fine for ASCII strings.

@stevengj stevengj added kind:bug Indicates an unexpected problem or unintended behavior domain:unicode Related to unicode characters and encodings domain:strings "Strings!" labels Apr 20, 2019
@stevengj
Copy link
Member Author

stevengj commented Apr 20, 2019

The issue here is that the mapfoldr implementation does reverse iteration by itr[i - 1], assuming the iterator is indexable and that the indices are consecutive. The right thing is probably to use Iterators.reverse (#24823).

@stevengj
Copy link
Member Author

For example, myfoldr(f, x) = foldl((x,y) -> f(y,x), Iterators.reverse(x)) works.

@stevengj
Copy link
Member Author

I should have a PR shortly.

@stevengj
Copy link
Member Author

Actually there is already a PR #25520 that should do the trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:strings "Strings!" domain:unicode Related to unicode characters and encodings kind:bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant