-
-
Notifications
You must be signed in to change notification settings - Fork 706
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
Issue 18948 - std.uni.toLower and std.uni.toUpper should work with random access ranges #6545
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8997,20 +8997,24 @@ private alias UpperTriple = AliasSeq!(toUpperIndex, MAX_SIMPLE_UPPER, toUpperTab | |
| private alias LowerTriple = AliasSeq!(toLowerIndex, MAX_SIMPLE_LOWER, toLowerTab); | ||
|
|
||
| // generic toUpper/toLower on whole string, creates new or returns as is | ||
| private S toCase(alias indexFn, uint maxIdx, alias tableFn, alias asciiConvert, S)(S s) @trusted pure | ||
| if (isSomeString!S) | ||
| private ElementEncodingType!S[] toCase(alias indexFn, uint maxIdx, alias tableFn, alias asciiConvert, S)(S s) | ||
| if (isSomeString!S || (isRandomAccessRange!S && hasLength!S && hasSlicing!S && isSomeChar!(ElementType!S))) | ||
| { | ||
| import std.array : appender; | ||
| import std.array : appender, array; | ||
| import std.ascii : isASCII; | ||
| import std.utf : byDchar; | ||
|
|
||
| foreach (i, dchar cOuter; s) | ||
| auto r = s.byDchar; | ||
| for (size_t i; !r.empty; ++i, r.popFront()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is incorrect. |
||
| { | ||
| auto cOuter = r.front; | ||
| ushort idx = indexFn(cOuter); | ||
| if (idx == ushort.max) | ||
| continue; | ||
| auto result = appender!S(s[0 .. i]); | ||
| auto result = appender!(ElementEncodingType!S[])(); | ||
| result.put(s[0 .. i]); | ||
| result.reserve(s.length); | ||
| foreach (dchar c; s[i .. $]) | ||
| foreach (dchar c; s[i .. $].byDchar) | ||
| { | ||
| if (c.isASCII) | ||
| { | ||
|
|
@@ -9039,7 +9043,11 @@ if (isSomeString!S) | |
| } | ||
| return result.data; | ||
| } | ||
| return s; | ||
|
|
||
| static if (isSomeString!S) | ||
| return s; | ||
| else | ||
| return s.array; | ||
| } | ||
|
|
||
| @safe unittest //12428 | ||
|
|
@@ -9783,16 +9791,28 @@ dchar toLower(dchar c) | |
| } | ||
|
|
||
| /++ | ||
| Returns a string which is identical to `s` except that all of its | ||
| Creates a new array which is identical to `s` except that all of its | ||
| characters are converted to lowercase (by preforming Unicode lowercase mapping). | ||
| If none of `s` characters were affected, then `s` itself is returned. | ||
| If none of `s` characters were affected, then `s` itself is returned if `s` is a | ||
| `string`-like type. | ||
|
|
||
| Params: | ||
| s = A $(REF_ALTTEXT random access range, isRandomAccessRange, std,range,primitives) | ||
| of characters | ||
| Returns: | ||
| An array with the same element type as `s`. | ||
| +/ | ||
| S toLower(S)(S s) @trusted pure | ||
| if (isSomeString!S) | ||
| ElementEncodingType!S[] toLower(S)(S s) | ||
| if (isSomeString!S || (isRandomAccessRange!S && hasLength!S && hasSlicing!S && isSomeChar!(ElementType!S))) | ||
| { | ||
| static import std.ascii; | ||
| return toCase!(LowerTriple, std.ascii.toLower)(s); | ||
|
|
||
| static if (isSomeString!S) | ||
| return () @trusted { return toCase!(LowerTriple, std.ascii.toLower)(s); } (); | ||
| else | ||
| return toCase!(LowerTriple, std.ascii.toLower)(s); | ||
| } | ||
|
|
||
| // overloads for the most common cases to reduce compile time | ||
| @safe pure /*TODO nothrow*/ | ||
| { | ||
|
|
@@ -9900,6 +9920,15 @@ if (isSomeString!S) | |
| assert(toUpper(c) == '\u1F8F'); | ||
| } | ||
|
|
||
| @safe pure unittest | ||
| { | ||
| import std.algorithm.comparison : cmp, equal; | ||
| import std.utf : byCodeUnit; | ||
| auto r1 = "FoL".byCodeUnit; | ||
| assert(r1.toLower.cmp("fol") == 0); | ||
| auto r2 = "A\u0460B\u0461d".byCodeUnit; | ||
| assert(r2.toLower.cmp("a\u0461b\u0461d") == 0); | ||
| } | ||
|
|
||
| /++ | ||
| If `c` is a Unicode lowercase $(CHARACTER), then its uppercase equivalent | ||
|
|
@@ -9965,16 +9994,28 @@ dchar toUpper(dchar c) | |
| } | ||
|
|
||
| /++ | ||
| Returns a string which is identical to `s` except that all of its | ||
| Allocates a new array which is identical to `s` except that all of its | ||
| characters are converted to uppercase (by preforming Unicode uppercase mapping). | ||
| If none of `s` characters were affected, then `s` itself is returned. | ||
| If none of `s` characters were affected, then `s` itself is returned if `s` | ||
| is a `string`-like type. | ||
|
|
||
| Params: | ||
| s = A $(REF_ALTTEXT random access range, isRandomAccessRange, std,range,primitives) | ||
| of characters | ||
| Returns: | ||
| An new array with the same element type as `s`. | ||
| +/ | ||
| S toUpper(S)(S s) @trusted pure | ||
| if (isSomeString!S) | ||
| ElementEncodingType!S[] toUpper(S)(S s) | ||
| if (isSomeString!S || (isRandomAccessRange!S && hasLength!S && hasSlicing!S && isSomeChar!(ElementType!S))) | ||
| { | ||
| static import std.ascii; | ||
| return toCase!(UpperTriple, std.ascii.toUpper)(s); | ||
|
|
||
| static if (isSomeString!S) | ||
| return () @trusted { return toCase!(UpperTriple, std.ascii.toUpper)(s); } (); | ||
| else | ||
| return toCase!(UpperTriple, std.ascii.toUpper)(s); | ||
| } | ||
|
|
||
| // overloads for the most common cases to reduce compile time | ||
| @safe pure /*TODO nothrow*/ | ||
| { | ||
|
|
@@ -10088,6 +10129,16 @@ if (isSomeString!S) | |
| }} | ||
| } | ||
|
|
||
| // test random access ranges | ||
| @safe pure unittest | ||
| { | ||
| import std.algorithm.comparison : cmp; | ||
| import std.utf : byCodeUnit; | ||
| auto s1 = "FoL".byCodeUnit; | ||
| assert(s1.toUpper.cmp("FOL") == 0); | ||
| auto s2 = "a\u0460B\u0461d".byCodeUnit; | ||
| assert(s2.toUpper.cmp("A\u0460B\u0460D") == 0); | ||
| } | ||
|
|
||
| /++ | ||
| Returns whether `c` is a Unicode alphabetic $(CHARACTER) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We really need to fix our stringLike trait mess :/