Skip to content

Commit

Permalink
Fix utf encode-decode
Browse files Browse the repository at this point in the history
  • Loading branch information
LunaTheFoxgirl committed Dec 10, 2024
1 parent cc63693 commit 0b2e969
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion source/numem/collections/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public:
D slices are short lived and may end up pointing to invalid memory if their string is modified.
*/
@trusted
T[] opSlice(size_t dim = 0)(size_t start, size_t end) {
inout(T)[] opSlice(size_t dim = 0)(size_t start, size_t end) inout {
return memory[start..end];
}

Expand Down
9 changes: 5 additions & 4 deletions source/numem/core/env.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import numem.core.memory;

version(Windows) import core.sys.windows.winbase : GetEnvironmentVariableW, SetEnvironmentVariableW;
else import core.sys.posix.stdlib : setenv, getenv;
import core.sys.windows.raserror;

@nogc:

Expand All @@ -26,7 +27,7 @@ struct Environment {
private:
static nstring get(const(char)* key) {
version(Windows) {
auto utf16k = key.toUTF16;
auto utf16k = key.fromStringz.toUTF16;

// Try getting the size of the env var.
// if this fails, the env var is probably empty.
Expand All @@ -37,7 +38,7 @@ private:
// Windows includes the null terminator, but n*string does too
// so to not have 2 null terminators, subtract 1.
nwstring envstr = nwstring(bufSize-1);
bufSize = GetEnvironmentVariableW(utf16k.ptr, envstr.ptr, envstr.length+1);
bufSize = GetEnvironmentVariableW(utf16k.ptr, cast(wchar*)envstr.ptr, cast(uint)(envstr.length+1));

nogc_delete(utf16k);
return envstr.toUTF8;
Expand All @@ -48,9 +49,9 @@ private:

static bool set(const(char)* key, nstring value) {
version(Windows) {
auto utf16k = key.toUTF16();
auto utf16k = key.fromStringz.toUTF16();
auto utf16v = value.toUTF16();
return SetEnvironmentVariableW(utf16k.ptr, utf16v.ptr);
return cast(bool)SetEnvironmentVariableW(utf16k.ptr, utf16v.ptr);
} else {
return setenv(key, value.ptr, 1) == 0;
}
Expand Down
19 changes: 8 additions & 11 deletions source/numem/string.d
Original file line number Diff line number Diff line change
Expand Up @@ -416,19 +416,16 @@ public:
}

/**
Tests equality between nstrings
Tests equality between strings
*/
@trusted
bool opEquals(R)(R other) if(is(R == basic_string!T)) {
return this.length == other.length && this[0..$] == other[0..$];
}

/**
Tests equality between nstrings
*/
@trusted
bool opEquals(R)(R other) if(is(R == immutable(T)[])) {
return this.size == other.length && this[0..$-1] == other[0..$];
bool opEquals(R)(ref auto inout R other) inout if (isSomeString!R) {
static if (isSomeCString!R)
size_t len = cstrlen(other);
else
size_t len = other.length;

return this.length == len && this.vec_[0..len] == other[0..len];
}

/**
Expand Down
12 changes: 6 additions & 6 deletions source/numem/text/unicode/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ T encode(T)(ref auto UnicodeSequence seq, bool addBOM = false) if (isSomeNString
This will always create a copy.
*/
ref auto toUTF8(FromT)(ref auto T from) if (isSomeSafeString!T) {
static if (StringCharSize!T == 1)
ref auto toUTF8(FromT)(ref auto FromT from) if (isSomeSafeString!FromT) {
static if (StringCharSize!FromT == 1)
return nstring(from);
else
return encode!nstring(decode(from, true), false);
Expand All @@ -122,8 +122,8 @@ ref auto toUTF8(FromT)(ref auto T from) if (isSomeSafeString!T) {
This will always create a copy.
*/
ref auto toUTF16(FromT)(ref auto T from, bool addBOM = false) if (isSomeSafeString!T) {
static if (StringCharSize!T == 2)
ref auto toUTF16(FromT)(ref auto FromT from, bool addBOM = false) if (isSomeSafeString!FromT) {
static if (StringCharSize!FromT == 2)
return nwstring(from);
else
return encode!nwstring(decode(from, true), addBOM);
Expand All @@ -134,8 +134,8 @@ ref auto toUTF16(FromT)(ref auto T from, bool addBOM = false) if (isSomeSafeStri
This will always create a copy.
*/
ref auto toUTF32(FromT)(ref auto T from, bool addBOM = false) if (isSomeSafeString!T) {
static if (StringCharSize!T == 2)
ref auto toUTF32(FromT)(ref auto FromT from, bool addBOM = false) if (isSomeSafeString!FromT) {
static if (StringCharSize!FromT == 2)
return ndstring(from);
else
return encode!ndstring(decode(from, true), addBOM);
Expand Down
3 changes: 2 additions & 1 deletion source/numem/text/unicode/utf8.d
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ UnicodeSequence decode(inout(char)[] str) {

// Validate length, add FFFD if invalid.
size_t clen = str[i].getLength();
if (clen >= i+str.length || clen == 0) {
if (i+clen > str.length || clen == 0) {
code ~= unicodeReplacementCharacter;
i++;
continue;
}

txt[0..clen] = str[i..i+clen];
Expand Down

0 comments on commit 0b2e969

Please sign in to comment.