Skip to content

Commit

Permalink
Merge pull request #4297 from jondegenhardt/toCaser-Ascii-check
Browse files Browse the repository at this point in the history
ASCII optimization check in toCaser, for asLowerCase and asUpperCase
  • Loading branch information
DmitryOlshansky committed May 10, 2016
2 parents 1db9c43 + 49f9c82 commit f257e83
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions std/uni.d
Original file line number Diff line number Diff line change
Expand Up @@ -8094,7 +8094,7 @@ unittest //12428


// generic toUpper/toLower on whole range, returns range
private auto toCaser(alias indexFn, uint maxIdx, alias tableFn, Range)(Range str)
private auto toCaser(alias indexFn, uint maxIdx, alias tableFn, alias asciiConvert, Range)(Range str)
// Accept range of dchar's
if (isInputRange!Range &&
isSomeChar!(ElementEncodingType!Range) &&
Expand All @@ -8109,31 +8109,41 @@ private auto toCaser(alias indexFn, uint maxIdx, alias tableFn, Range)(Range str

@property auto front()
{
import std.ascii : isASCII;

if (!nLeft)
{
dchar c = r.front;
const idx = indexFn(c);
if (idx == ushort.max)
if (c.isASCII)
{
buf[0] = c;
nLeft = 1;
}
else if (idx < maxIdx)
{
buf[0] = tableFn(idx);
buf[0] = asciiConvert(c);
nLeft = 1;
}
else
{
auto val = tableFn(idx);
// unpack length + codepoint
nLeft = val >> 24;
if (nLeft == 0)
const idx = indexFn(c);
if (idx == ushort.max)
{
buf[0] = c;
nLeft = 1;
assert(nLeft <= buf.length);
buf[nLeft - 1] = cast(dchar)(val & 0xFF_FFFF);
foreach (j; 1 .. nLeft)
buf[nLeft - j - 1] = tableFn(idx + j);
}
else if (idx < maxIdx)
{
buf[0] = tableFn(idx);
nLeft = 1;
}
else
{
auto val = tableFn(idx);
// unpack length + codepoint
nLeft = val >> 24;
if (nLeft == 0)
nLeft = 1;
assert(nLeft <= buf.length);
buf[nLeft - 1] = cast(dchar)(val & 0xFF_FFFF);
foreach (j; 1 .. nLeft)
buf[nLeft - j - 1] = tableFn(idx + j);
}
}
}
return buf[nLeft - 1];
Expand Down Expand Up @@ -8194,11 +8204,12 @@ auto asLowerCase(Range)(Range str)
import std.utf : byDchar;

// Decode first
return toCaser!LowerTriple(str.byDchar);
return asLowerCase(str.byDchar);
}
else
{
return toCaser!LowerTriple(str);
static import std.ascii;
return toCaser!(LowerTriple, std.ascii.toLower)(str);
}
}

Expand All @@ -8212,11 +8223,12 @@ auto asUpperCase(Range)(Range str)
import std.utf : byDchar;

// Decode first
return toCaser!UpperTriple(str.byDchar);
return asUpperCase(str.byDchar);
}
else
{
return toCaser!UpperTriple(str);
static import std.ascii;
return toCaser!(UpperTriple, std.ascii.toUpper)(str);
}
}

Expand Down

0 comments on commit f257e83

Please sign in to comment.