purify std.uni constructs, blocked by std.algortihm.sort

This commit is contained in:
Dmitry Olshansky 2014-02-23 21:49:25 +04:00
parent cbd9bd3b79
commit c264bd6a51
2 changed files with 99 additions and 80 deletions

File diff suppressed because one or more lines are too long

View file

@ -1390,7 +1390,6 @@ private struct SliceOverIndexed(T)
{ {
enum assignableIndex = is(typeof((){ T.init[0] = Item.init; })); enum assignableIndex = is(typeof((){ T.init[0] = Item.init; }));
enum assignableSlice = is(typeof((){ T.init[0..0] = Item.init; })); enum assignableSlice = is(typeof((){ T.init[0..0] = Item.init; }));
auto opIndex(size_t idx)const auto opIndex(size_t idx)const
in in
{ {
@ -1888,6 +1887,7 @@ public alias CodepointSet = InversionList!GcPolicy;
*/ */
public struct CodepointInterval public struct CodepointInterval
{ {
pure:
uint[2] _tuple; uint[2] _tuple;
alias _tuple this; alias _tuple this;
this(uint low, uint high) this(uint low, uint high)
@ -1899,8 +1899,8 @@ public struct CodepointInterval
{ {
return this[0] == val[0] && this[1] == val[1]; return this[0] == val[0] && this[1] == val[1];
} }
@property ref uint a(){ return _tuple[0]; } @property ref inout(uint) a() inout { return _tuple[0]; }
@property ref uint b(){ return _tuple[1]; } @property ref inout(uint) b() inout { return _tuple[1]; }
} }
//@@@BUG another forward reference workaround //@@@BUG another forward reference workaround
@ -1980,10 +1980,11 @@ public struct CodepointInterval
@trusted public struct InversionList(SP=GcPolicy) @trusted public struct InversionList(SP=GcPolicy)
{ {
public: public:
/** /**
Construct from another code point set of any type. Construct from another code point set of any type.
*/ */
this(Set)(Set set) this(Set)(Set set) pure
if(isCodepointSet!Set) if(isCodepointSet!Set)
{ {
uint[] arr; uint[] arr;
@ -1998,7 +1999,7 @@ public:
/** /**
Construct a set from a forward range of code point intervals. Construct a set from a forward range of code point intervals.
*/ */
this(Range)(Range intervals) this(Range)(Range intervals) /*pure */ //@@@BUG@@@ sort is not pure
if(isForwardRange!Range && isIntegralPair!(ElementType!Range)) if(isForwardRange!Range && isIntegralPair!(ElementType!Range))
{ {
auto flattened = roundRobin(intervals.save.map!"a[0]"(), auto flattened = roundRobin(intervals.save.map!"a[0]"(),
@ -2008,7 +2009,7 @@ public:
} }
//helper function that avoids sanity check to be CTFE-friendly //helper function that avoids sanity check to be CTFE-friendly
private static fromIntervals(Range)(Range intervals) private static fromIntervals(Range)(Range intervals) pure
{ {
auto flattened = roundRobin(intervals.save.map!"a[0]"(), auto flattened = roundRobin(intervals.save.map!"a[0]"(),
intervals.save.map!"a[1]"()); intervals.save.map!"a[1]"());
@ -2016,6 +2017,23 @@ public:
set.data = Uint24Array!(SP)(flattened); set.data = Uint24Array!(SP)(flattened);
return set; return set;
} }
//ditto untill sort is CTFE-able
private static fromIntervals()(uint[] intervals...) pure
in
{
assert(intervals.length % 2 == 0, "Odd number of interval bounds [a, b)!");
for(uint i=0; i<intervals.length/2; i++)
{
auto a = intervals[2*i], b = intervals[2*i+1];
assert(a < b, text("illegal interval [a, b): ", a, " > ", b));
}
}
body
{
InversionList set;
set.data = Uint24Array!(SP)(intervals);
return set;
}
/** /**
Construct a set from plain values of code point intervals. Construct a set from plain values of code point intervals.
@ -4790,8 +4808,8 @@ template Utf16Matcher()
auto build(Set)(Set set) auto build(Set)(Set set)
{ {
auto ascii = set & unicode.ASCII; auto ascii = set & unicode.ASCII;
auto bmp = (set & CodepointSet(0x80, 0xFFFF+1)) auto bmp = (set & CodepointSet.fromIntervals(0x80, 0xFFFF+1))
- CodepointSet(0xD800, 0xDFFF+1); - CodepointSet.fromIntervals(0xD800, 0xDFFF+1);
auto other = set - (bmp | ascii); auto other = set - (bmp | ascii);
auto asciiT = ascii.byCodepoint.map!(x=>cast(char)x).buildTrie!(AsciiSpec); auto asciiT = ascii.byCodepoint.map!(x=>cast(char)x).buildTrie!(AsciiSpec);
auto bmpT = bmp.byCodepoint.map!(x=>cast(wchar)x).buildTrie!(BmpSpec); auto bmpT = bmp.byCodepoint.map!(x=>cast(wchar)x).buildTrie!(BmpSpec);
@ -4991,7 +5009,7 @@ public auto utfMatcher(Char, Set)(Set set)
//a range of code units, packed with index to speed up forward iteration //a range of code units, packed with index to speed up forward iteration
public auto decoder(C)(C[] s, size_t offset=0) package auto decoder(C)(C[] s, size_t offset=0)
if(is(C : wchar) || is(C : char)) if(is(C : wchar) || is(C : char))
{ {
static struct Decoder static struct Decoder
@ -5020,7 +5038,7 @@ public auto decoder(C)(C[] s, size_t offset=0)
Expose UTF string $(D s) as a random-access Expose UTF string $(D s) as a random-access
range of $(S_LINK Code unit, code units). range of $(S_LINK Code unit, code units).
*/ */
public auto units(C)(C[] s) package auto units(C)(C[] s)
if(is(C : wchar) || is(C : char)) if(is(C : wchar) || is(C : char))
{ {
static struct Units static struct Units
@ -5562,13 +5580,14 @@ unittest
} }
// Creates a range of $(D CodepointInterval) that lazily decodes compressed data. // Creates a range of $(D CodepointInterval) that lazily decodes compressed data.
@safe package auto decompressIntervals(const(ubyte)[] data) @safe package auto decompressIntervals(const(ubyte)[] data) pure
{ {
return DecompressedIntervals(data); return DecompressedIntervals(data);
} }
@trusted struct DecompressedIntervals @trusted struct DecompressedIntervals
{ {
pure:
const(ubyte)[] _stream; const(ubyte)[] _stream;
size_t _idx; size_t _idx;
CodepointInterval _front; CodepointInterval _front;
@ -5620,7 +5639,7 @@ else
{ {
// helper for looking up code point sets // helper for looking up code point sets
@trusted ptrdiff_t findUnicodeSet(alias table, C)(in C[] name) @trusted ptrdiff_t findUnicodeSet(alias table, C)(in C[] name) pure
{ {
auto range = assumeSorted!((a,b) => propertyNameLess(a,b)) auto range = assumeSorted!((a,b) => propertyNameLess(a,b))
(table.map!"a.name"()); (table.map!"a.name"());
@ -5631,7 +5650,7 @@ else
} }
// another one that loads it // another one that loads it
@trusted bool loadUnicodeSet(alias table, Set, C)(in C[] name, ref Set dest) @trusted bool loadUnicodeSet(alias table, Set, C)(in C[] name, ref Set dest) pure
{ {
auto idx = findUnicodeSet!table(name); auto idx = findUnicodeSet!table(name);
if(idx >= 0) if(idx >= 0)
@ -5643,7 +5662,7 @@ else
} }
@trusted bool loadProperty(Set=CodepointSet, C) @trusted bool loadProperty(Set=CodepointSet, C)
(in C[] name, ref Set target) (in C[] name, ref Set target) pure
{ {
alias ucmp = comparePropertyName; alias ucmp = comparePropertyName;
// conjure cumulative properties by hand // conjure cumulative properties by hand
@ -5731,9 +5750,9 @@ else
target |= asSet(uniProps.So); target |= asSet(uniProps.So);
} }
else if(ucmp(name, "any") == 0) else if(ucmp(name, "any") == 0)
target = Set(0,0x110000); target = Set.fromIntervals(0, 0x110000);
else if(ucmp(name, "ascii") == 0) else if(ucmp(name, "ascii") == 0)
target = Set(0,0x80); target = Set.fromIntervals(0, 0x80);
else else
return loadUnicodeSet!(uniProps.tab)(name, target); return loadUnicodeSet!(uniProps.tab)(name, target);
return true; return true;
@ -5837,7 +5856,7 @@ template SetSearcher(alias table, string kind)
--- ---
*/ */
static @property auto opDispatch(string name)() static @property auto opDispatch(string name)() pure
{ {
static if(findAny(name)) static if(findAny(name))
return loadAny(name); return loadAny(name);
@ -5943,7 +5962,7 @@ private:
|| (ucmp(name[0..2],"In") == 0 && findSetName!(blocks.tab)(name[2..$])); || (ucmp(name[0..2],"In") == 0 && findSetName!(blocks.tab)(name[2..$]));
} }
static auto loadAny(Set=CodepointSet, C)(in C[] name) static auto loadAny(Set=CodepointSet, C)(in C[] name) pure
{ {
Set set; Set set;
bool loaded = loadProperty(name, set) || loadUnicodeSet!(scripts.tab)(name, set) bool loaded = loadProperty(name, set) || loadUnicodeSet!(scripts.tab)(name, set)
@ -8586,7 +8605,7 @@ private:
// load static data from pre-generated tables into usable datastructures // load static data from pre-generated tables into usable datastructures
@safe auto asSet(const (ubyte)[] compressed) @safe auto asSet(const (ubyte)[] compressed) pure
{ {
return CodepointSet.fromIntervals(decompressIntervals(compressed)); return CodepointSet.fromIntervals(decompressIntervals(compressed));
} }