Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.uni: use dstring for compositionTable #8886

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 208 additions & 8 deletions std/internal/unicode_comp.d

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions std/internal/unicode_tables.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,6 @@ nothrow @nogc pure @safe:
ubyte entry_len() const { return (x >> 0) & 0xF; }
}

struct CompEntry
{
dchar rhs, composed;
}

struct UnicodeProperty
{
string name;
Expand Down
10 changes: 5 additions & 5 deletions std/uni/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -8458,21 +8458,21 @@ public dchar compose(dchar first, dchar second) pure nothrow @safe
{
import std.algorithm.iteration : map;
import std.internal.unicode_comp : compositionTable, composeCntShift, composeIdxMask;
import std.range : assumeSorted;
import std.range : assumeSorted, stride;
immutable packed = compositionJumpTrie[first];
if (packed == ushort.max)
return dchar.init;
// unpack offset and length
immutable idx = packed & composeIdxMask, cnt = packed >> composeCntShift;
// TODO: optimize this micro binary search (no more then 4-5 steps)
auto r = compositionTable[idx .. idx+cnt].map!"a.rhs"().assumeSorted();
auto r = compositionTable.stride(2)[idx .. idx+cnt].assumeSorted();
immutable target = r.lowerBound(second).length;
if (target == cnt)
return dchar.init;
immutable entry = compositionTable[idx+target];
if (entry.rhs != second)
immutable entry = compositionTable[(idx+target)*2];
if (entry != second)
return dchar.init;
return entry.composed;
return compositionTable[(idx+target)*2 + 1];
}

///
Expand Down
38 changes: 26 additions & 12 deletions tools/unicode_table_generator.d
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@ nothrow @nogc pure @safe:
ubyte entry_len() const { return (x >> 0) & 0xF; }
}

struct CompEntry
{
dchar rhs, composed;
}

struct UnicodeProperty
{
string name;
Expand Down Expand Up @@ -1200,14 +1195,33 @@ void writeCompositionTable(File sink)
write("enum compositionJumpTrieEntries = TrieEntry!(ushort, 12, 9)(");
triT.store(sink.lockingTextWriter());
writeln(");");
writeln("@property immutable(CompEntry[]) compositionTable() nothrow pure @nogc @safe");
writeln("dstring compositionTable() nothrow pure @nogc @safe");
writeln("{");
writeln("alias CE = CompEntry;");
write("static immutable CE[] t = [");
foreach (pair; dupletes)
writef("CE(0x%05x, 0x%05x),", pair[0], pair[1]);
writeln("];");
writeln("return t;");
writef("static immutable dchar[%d] t =\n\"", dupletes.length * 2);
size_t lineCount = 1;
foreach (i, pair; dupletes)
{
static foreach(j; 0 .. 2)
{
if (pair[j] <= 0xFFFF)
{
writef("\\u%04X", pair[j]);
lineCount += 6;
}
else
{
writef("\\U%08X", pair[j]);
lineCount += 10;
}
if (lineCount >= 110)
{
write("\"~\n\"");
lineCount = 1;
}
}
}
writeln("\"d;");
writeln("return t[];");
writeln("}");
}
}
Expand Down