Skip to content

Commit

Permalink
Add ASCII optimization check to toCaser, for asLowerCase and asUpperC…
Browse files Browse the repository at this point in the history
…ase.
  • Loading branch information
jondegenhardt committed May 10, 2016
1 parent bcfc8ba commit afc28fb
Showing 1 changed file with 34 additions and 23 deletions.
57 changes: 34 additions & 23 deletions std/uni.d
Original file line number Diff line number Diff line change
Expand Up @@ -8086,7 +8086,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 @@ -8101,31 +8101,40 @@ 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)
{
buf[0] = c;
nLeft = 1;
}
else if (idx < maxIdx)
if (c.isASCII)
{
buf[0] = tableFn(idx);
buf[0] = asciiConvert(c);
nLeft = 1;
}
else
{
auto val = tableFn(idx);
// unpack length + codepoint
nLeft = val >> 24;
if (nLeft == 0)
else {
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 @@ -8186,11 +8195,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 @@ -8204,11 +8214,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 afc28fb

Please sign in to comment.