Refactored FastCharSearch with more modern string mixin code.
This commit is contained in:
parent
d32addacda
commit
aeee0758a7
|
@ -34,10 +34,12 @@ template FastCharSearch(dstring chars, uint tableSize = 256)
|
||||||
private mixin(searchCode!(chars, tableSize)());
|
private mixin(searchCode!(chars, tableSize)());
|
||||||
}
|
}
|
||||||
|
|
||||||
///Generate the search table and the canFind method.
|
/// Generate the search table and the canFind method.
|
||||||
string searchCode(dstring chars, uint tableSize)() @safe pure nothrow
|
string searchCode(dstring chars, uint tableSize)() @safe pure //nothrow
|
||||||
{
|
{
|
||||||
const tableSizeStr = to!string(tableSize);
|
import std.string;
|
||||||
|
|
||||||
|
const tableSizeStr = tableSize.to!string;
|
||||||
ubyte[tableSize] table;
|
ubyte[tableSize] table;
|
||||||
table[] = 0;
|
table[] = 0;
|
||||||
|
|
||||||
|
@ -46,51 +48,42 @@ string searchCode(dstring chars, uint tableSize)() @safe pure nothrow
|
||||||
|
|
||||||
foreach(c; chars)
|
foreach(c; chars)
|
||||||
{
|
{
|
||||||
if(c < tableSize){table[c] = 1;}
|
if(c < tableSize) { table[c] = 1; }
|
||||||
else {specialChars ~= c;}
|
else { specialChars ~= c; }
|
||||||
}
|
|
||||||
|
|
||||||
string tableCode()
|
|
||||||
{
|
|
||||||
string code = "static immutable ubyte table_[" ~ tableSizeStr ~ "] = [\n";
|
|
||||||
foreach(c; table[0 .. $ - 1])
|
|
||||||
{
|
|
||||||
code ~= c ? "true,\n" : "false,\n";
|
|
||||||
}
|
|
||||||
code ~= table[$ - 1] ? "true\n" : "false\n";
|
|
||||||
code ~= "];\n\n";
|
|
||||||
return code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string specialCharsCode()
|
string specialCharsCode()
|
||||||
{
|
{
|
||||||
string code;
|
return specialChars.map!(c => q{cast(uint)c == %s}.format(cast(uint)c)).join(q{ || });
|
||||||
foreach(c; specialChars[0 .. $ - 1])
|
|
||||||
{
|
|
||||||
code ~= "cast(uint)c == " ~ to!string(cast(uint)c) ~ " || ";
|
|
||||||
}
|
|
||||||
code ~= "cast(uint)c == " ~ to!string(cast(uint)specialChars[$ - 1]);
|
|
||||||
|
|
||||||
return code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
string code = tableSize ? tableCode() : "";
|
const caseInTable =
|
||||||
|
q{
|
||||||
code ~= "bool canFind(in dchar c) pure @safe nothrow @nogc\n"
|
if(c < %s)
|
||||||
"{\n";
|
{
|
||||||
|
return cast(immutable(bool))table_[c];
|
||||||
|
}
|
||||||
|
}.format(tableSize);
|
||||||
|
|
||||||
|
string code;
|
||||||
if(tableSize)
|
if(tableSize)
|
||||||
{
|
{
|
||||||
code ~= " if(c < " ~ tableSizeStr ~ ")\n"
|
code ~=
|
||||||
" {\n"
|
q{
|
||||||
" return cast(immutable(bool))table_[c];\n"
|
static immutable ubyte table_[%s] = [
|
||||||
" }\n";
|
%s];
|
||||||
|
}.format(tableSize, table[].map!(c => c ? q{true} : q{false}).join(q{, }));
|
||||||
}
|
}
|
||||||
|
code ~=
|
||||||
|
q{
|
||||||
|
bool canFind(const dchar c) @safe pure nothrow @nogc
|
||||||
|
{
|
||||||
|
%s
|
||||||
|
|
||||||
code ~= specialChars.length
|
return %s;
|
||||||
? " return " ~ specialCharsCode() ~ ";\n"
|
}
|
||||||
: " return false;";
|
}.format(tableSize ? caseInTable : "",
|
||||||
code ~= "}\n";
|
specialChars.length ? specialCharsCode() : q{false});
|
||||||
|
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue