Fix #946: Add standard iterator traits to all CharPointer types for STL algorithm compatibility#1562
Conversation
|
This adds iterator traits to Another practical concern is the use of Anyway, this is why |
…er types for STL algorithm compatibility Add the five standard iterator traits (difference_type, value_type, pointer, reference, iterator_category) to all four CharPointer types: - CharPointer_UTF8 - CharPointer_UTF16 - CharPointer_UTF32 - CharPointer_ASCII Uses std::input_iterator_tag, which correctly reflects that operator*() returns juce_wchar by value (proxy iterator) and that variable-width encodings (UTF-8, UTF-16) do not support O(1) random access. This ensures String::begin()/end() work with standard library algorithms (e.g. std::all_of, std::find_if) regardless of the JUCE_STRING_UTF_TYPE setting. Fixes juce-framework#946
ff23b25 to
6d35b10
Compare
|
@jrlanglois Thanks for the thorough review! I've addressed both of your concerns: 1. Coverage of all CharPointer types 2. Iterator category Additionally, The branch has been rebased onto the latest upstream master and squashed into a single commit. All changes verified to compile cleanly with GCC and Clang, including Ref: #946 |
Fixes #946: Add standard iterator traits to all CharPointer types for compatibility with std algorithms
Changes
Adds the five standard iterator traits (
difference_type,value_type,pointer,reference,iterator_category) to all four CharPointer types:CharPointer_UTF8CharPointer_UTF16CharPointer_UTF32CharPointer_ASCIIThis ensures that
String::begin()/String::end()work with standard library algorithms (e.g.std::all_of,std::find_if,std::any_of) regardless of theJUCE_STRING_UTF_TYPEsetting (8, 16, or 32).Design Decisions
std::input_iterator_tagis used for all types. UTF-8 and UTF-16 are variable-width encodings, sooperator+(n)is not O(1) —random_access_iterator_tagorbidirectional_iterator_tagwould misrepresent the performance contract.input_iterator_tagis the most honest category and is sufficient for the algorithms users need (the exact scenario from Some custom iterators are incompatible with standard algorithms #946).reference = juce_wchar(notjuce_wchar&) becauseoperator*()returns by value in all CharPointer types — these are proxy iterators that decode the underlying encoding intojuce_wchar.difference_type = std::ptrdiff_t— the standard choice.value_type = juce_wchar— consistent across all CharPointer types since they all decode tojuce_wchar.Testing
std::all_of,std::find_if, and other STL algorithmsstatic_assertverified thatstd::iterator_traitsresolves correctly for all four typesRelated