make simpleCaseFoldings CTFE-able

This commit is contained in:
Dmitry Olshansky 2014-03-12 01:09:08 +04:00
parent 5a90d786ab
commit 48e7c9a9ad

View file

@ -7049,64 +7049,66 @@ unittest
*/ */
package auto simpleCaseFoldings(dchar ch) package auto simpleCaseFoldings(dchar ch)
{ {
alias sTable = simpleCaseTable;
static struct Range static struct Range
{ {
pure nothrow: pure nothrow:
const(SimpleCaseEntry)* s; //if == 0, then read c uint idx; //if == uint.max, then read c.
union union
{ {
dchar c; dchar c; // == 0 - empty range
uint len; uint len;
} }
@property bool isSmall() const { return idx == uint.max; }
this(dchar ch) this(dchar ch)
{ {
idx = uint.max;
c = ch; c = ch;
s = null;
} }
this(const(SimpleCaseEntry)[] slice) this(uint start, uint size)
{ {
s = slice.ptr; idx = start;
len = cast(uint)slice.length; len = size;
} }
@property dchar front() const @property dchar front() const
{ {
assert(!empty); assert(!empty);
if(!s) if(isSmall)
{ {
return c; return c;
} }
auto ch = s.ch; auto ch = sTable[idx].ch;
return ch; return ch;
} }
@property bool empty() const @property bool empty() const
{ {
if(!s) if(isSmall)
{ {
return c == dchar.max; return c == 0;
} }
return len == 0; return len == 0;
} }
@property uint length() const @property uint length() const
{ {
if(!s) if(isSmall)
{ {
return c == dchar.max ? 0 : 1; return c == 0 ? 0 : 1;
} }
return len; return len;
} }
void popFront() void popFront()
{ {
if(!s) if(isSmall)
c = dchar.max; c = 0;
else else
{ {
s++; idx++;
len--; len--;
} }
} }
@ -7114,14 +7116,14 @@ package auto simpleCaseFoldings(dchar ch)
immutable idx = simpleCaseTrie[ch]; immutable idx = simpleCaseTrie[ch];
if (idx == EMPTY_CASE_TRIE) if (idx == EMPTY_CASE_TRIE)
return Range(ch); return Range(ch);
alias sTable = simpleCaseTable;
auto entry = sTable[idx]; auto entry = sTable[idx];
immutable start = idx - entry.n; immutable start = idx - entry.n;
return Range(sTable[start .. start + entry.size]); return Range(start, entry.size);
} }
unittest unittest
{ {
assertCTFEable!((){
auto r = simpleCaseFoldings('Э').array; auto r = simpleCaseFoldings('Э').array;
assert(r.length == 2); assert(r.length == 2);
assert(r.canFind('э') && r.canFind('Э')); assert(r.canFind('э') && r.canFind('Э'));
@ -7131,6 +7133,7 @@ unittest
sr = simpleCaseFoldings('Å'); sr = simpleCaseFoldings('Å');
assert(sr.length == 3); assert(sr.length == 3);
assert(sr.canFind('å') && sr.canFind('Å') && sr.canFind('\u212B')); assert(sr.canFind('å') && sr.canFind('Å') && sr.canFind('\u212B'));
});
} }
/++ /++