Element type of string ranges
This article attempts to summarize the arguments in the thread Major performance problem with std.array.front().
Comparison
One of the proposals in the thread is to switch the iteration type of string ranges from dchar to the string's character type.
Argument | Current behavior (code point iteration) |
Code unit iteration | Code unit iteration + forbid implicit char <=> dchar conversions | Notes |
---|---|---|---|---|
Status quo | The current situation, with its (dubious) advantages and known disadvantages | Will cause breakage | Will cause more breakage | |
Will cause silent breakage | No silent breakage | |||
Searching for a particular dchar in a string. | s.canFind('é') | Will result in a pragma warning in some places, will fail silently in others (when specified via predicate). | Will emit a warning or fail to compile | This should not be recommended practice (not all languages have notions of characters, and not all characters (glyphs/graphemes) can be represented in one dchar). |
Searching for a particular dchar in a non-normalized string. | Above fails for combining marks, as that requires normalization. | |||
Case conversion, insensitive comparison in ranges for certain languages | s.count!((a, b) => std.uni.toLower(a) == std.uni.toLower(b))("é") | Fails silently. | Will emit a warning or fail to compile | This should not be recommended practice (correct case conversion and comparison for all languages is more complicated, and depends on locale - e.g. Turkish I / ı and İ / i). |
Case conversion, insensitive comparison in ranges for other languages | Fails. | |||
Correctness | Only works for certain languages and alphabets | Only works for ASCII | Only works for ASCII; enforces correct character type conversions | |
Performance | Implicit decoding everywhere, unless each algorithm is specialized not to | As fast as ubyte[] | ||
Implementation difficulty | Requires quite a bit of scaffolding: ElementEncodingType - 80 instances isSomeString - 138 instances isExactSomeString - 23 instances isSomeChar - 129 instances |
Strings are treated as any other arrays | ||
Consistency | Inconsistencies between array and range types Range algorithms return values different from array algorithms |
String ranges work like ranges of any other arrays |