Skip to content

Commit

Permalink
Allow nstring to be used in map
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Jun 1, 2024
1 parent d293913 commit 45520e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
35 changes: 31 additions & 4 deletions source/numem/mem/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public:
*/
@trusted
pragma(inline, true)
size_t size() {
size_t size() inout {
size_t sz = this.vec_.size();
return sz > 0 ? sz-1 : sz;
}
Expand All @@ -112,15 +112,15 @@ public:
Gets the length of the string
*/
@trusted
size_t length() {
size_t length() inout {
return size();
}

/**
Gets the capacity of the string
*/
@trusted
size_t capacity() {
size_t capacity() inout {
return this.vec_.capacity();
}

Expand Down Expand Up @@ -153,7 +153,7 @@ public:
Whether the string is empty.
*/
@trusted
bool empty() {
bool empty() inout {
return size > 0;
}

Expand All @@ -165,6 +165,13 @@ public:
vec_.shrinkToFit();
}

/**
Returns C string
*/
inout(T)* toCStringi() inout {
return cast(inout(T)*)this.vec_.idata();
}

/**
Returns C string
*/
Expand Down Expand Up @@ -316,6 +323,17 @@ public:
return this.size == other.length && this[0..$-1] == other[0..$];
}

/**
Allows comparing strings
*/
@trusted
int opCmp(S)(ref inout S s) inout if (is(S : basic_string!T)) {
import core.stdc.string : strncmp;
if (s.size() < this.size()) return -1;
if (s.size() > this.size()) return 1;
return strncmp(this.toCStringIO(), s.toCStringIO(), this.size());
}

/**
To D string
*/
Expand Down Expand Up @@ -359,4 +377,13 @@ unittest {
ndstring wd;
wd.appendCString("ho"d.ptr);
assert(wd.toDString() == "ho"d);
}

@("string in map")
unittest {
import numem.mem.map : map;
map!(nstring, int) kv;
kv[nstring("uwu")] = 42;

assert(kv[nstring("uwu")] == 42);
}
16 changes: 12 additions & 4 deletions source/numem/mem/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ public:
return memory;
}

/**
Gets the C data pointer as an inout pointer
*/
@trusted
inout(T)* idata() inout {
return cast(inout(T)*)memory;
}

/**
Gets the C data pointer atomically
*/
Expand Down Expand Up @@ -213,31 +221,31 @@ public:
Gets whether the vector is empty.
*/
@trusted
bool empty() {
bool empty() inout {
return size_ == 0;
}

/**
Gets the amount of elements in the vector
*/
@trusted
size_t size() {
size_t size() inout {
return size_;
}

/**
Gets the capacity of the vector
*/
@trusted
size_t capacity() {
size_t capacity() inout {
return capacity_;
}

/**
Returns the memory usage of the vector in bytes.
*/
@trusted
size_t usage() {
size_t usage() inout {
return capacity_*T.sizeof;
}

Expand Down

0 comments on commit 45520e3

Please sign in to comment.