Skip to content
Merged
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
48 changes: 27 additions & 21 deletions std/uni.d
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,10 @@ import std.range.primitives; // back, ElementEncodingType, ElementType, empty,
// save
import std.traits; // isConvertibleToString, isIntegral, isSomeChar,
// isSomeString, Unqual

import std.exception;// : enforce;
import core.memory; //: pureMalloc, pureRealloc, pureFree;
import core.exception; // : onOutOfMemoryError;
static import std.ascii;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What error do you get if you make those imports function local? Also if you can't make them function local, you should make them non-selective as currently there's a compiler bug causing them to behave as they were public.

@ZombineDev It's the other way around: every new non-selective import we add, is public.
In other words, the following is now possible:

import std.uni : assumeUnique, pureMalloc; // ...

#5584 (comment)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I remembered that bug report backwards.

// debug = std_uni;

debug(std_uni) import std.stdio; // writefln, writeln
Expand Down Expand Up @@ -6289,7 +6292,7 @@ enum controlSwitch = `
// TODO: redo the most of hangul stuff algorithmically in case of Graphemes too
// kill unrolled switches

private static bool isRegionalIndicator(dchar ch) @safe
private static bool isRegionalIndicator(dchar ch) @safe pure @nogc nothrow
{
return ch >= '\U0001F1E6' && ch <= '\U0001F1FF';
}
Expand Down Expand Up @@ -6500,9 +6503,9 @@ auto byGrapheme(Range)(Range range)
if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar))
{
// TODO: Bidirectional access
static struct Result
static struct Result(R)
{
private Range _range;
private R _range;
private Grapheme _front;

bool empty() @property
Expand All @@ -6520,7 +6523,7 @@ if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar))
_front = _range.empty ? Grapheme.init : _range.decodeGrapheme();
}

static if (isForwardRange!Range)
static if (isForwardRange!R)
{
Result save() @property
{
Expand All @@ -6529,7 +6532,7 @@ if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar))
}
}

auto result = Result(range);
auto result = Result!(Range)(range);
result.popFront();
return result;
}
Expand Down Expand Up @@ -6702,7 +6705,6 @@ if (isInputRange!Range && is(Unqual!(ElementType!Range) == dchar))
+/
@trusted struct Grapheme
{
import std.exception : enforce;
import std.traits : isDynamicArray;

public:
Expand Down Expand Up @@ -6787,7 +6789,6 @@ public:
{
static if (op == "~")
{
import core.stdc.stdlib : realloc;
if (!isBig)
{
if (slen_ == small_cap)
Expand All @@ -6808,9 +6809,8 @@ public:
cap_ = addu(cap_, grow, overflow);
auto nelems = mulu(3, addu(cap_, 1, overflow), overflow);
if (overflow) assert(0);

ptr_ = cast(ubyte*) enforce(realloc(ptr_, nelems),
"realloc failed");
ptr_ = cast(ubyte*) pureRealloc(ptr_, nelems);
if (ptr_ is null) onOutOfMemoryError();
}
write24(ptr_, ch, len_++);
return this;
Expand Down Expand Up @@ -6865,28 +6865,27 @@ public:
return r.length == 0;
}

this(this)
this(this) pure @nogc nothrow
{
import core.stdc.stdlib : malloc;
if (isBig)
{// dup it
import core.checkedint : addu, mulu;
bool overflow;
auto raw_cap = mulu(3, addu(cap_, 1, overflow), overflow);
if (overflow) assert(0);

auto p = cast(ubyte*) enforce(malloc(raw_cap), "malloc failed");
auto p = cast(ubyte*) pureMalloc(raw_cap);
if (p is null) onOutOfMemoryError();
p[0 .. raw_cap] = ptr_[0 .. raw_cap];
ptr_ = p;
}
}

~this()
~this() pure @nogc nothrow
{
import core.stdc.stdlib : free;
if (isBig)
{
free(ptr_);
pureFree(ptr_);
}
}

Expand Down Expand Up @@ -6915,14 +6914,13 @@ private:
}
}

void convertToBig()
void convertToBig() pure @nogc nothrow
{
import core.stdc.stdlib : malloc;

static assert(grow.max / 3 - 1 >= grow);
enum nbytes = 3 * (grow + 1);
size_t k = smallLength;
ubyte* p = cast(ubyte*) enforce(malloc(nbytes), "malloc failed");
ubyte* p = cast(ubyte*) pureMalloc(nbytes);
if (p is null) onOutOfMemoryError();
for (int i=0; i<k; i++)
write24(p, read24(small_.ptr, i), i);
// now we can overwrite small array data
Expand All @@ -6947,6 +6945,14 @@ private:

static assert(Grapheme.sizeof == size_t.sizeof*4);


@system pure /*nothrow @nogc*/ unittest // TODO: string .front is GC and throw
{
import std.algorithm.comparison : equal;
Grapheme[3] data = [Grapheme("Ю"), Grapheme("У"), Grapheme("З")];
assert(byGrapheme("ЮУЗ").equal(data[]));
}

///
@system unittest
{
Expand Down