-
Notifications
You must be signed in to change notification settings - Fork 1
/
NativeString.cs
75 lines (63 loc) · 2.78 KB
/
NativeString.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Runtime.InteropServices;
using System.Text;
using Unity.Collections.LowLevel.Unsafe;
namespace IL2CPPUtility {
public unsafe struct MarshaledString:IDisposable {
public override bool Equals(object obj) {
return obj is MarshaledString other && Equals(other);
}
public override int GetHashCode() {
return unchecked((int) (long) Chars);
}
public byte* Chars;
public MarshaledString(string str) {
Chars=(byte*)Marshal.StringToHGlobalAnsi(str);
}
public static bool operator ==(MarshaledString a, MarshaledString b) => a.Equals(b);
public static bool operator !=(MarshaledString a, MarshaledString b) => !a.Equals(b);
public static bool operator ==(MarshaledString a, NativeString b) => a.Equals(b);
public static bool operator !=(MarshaledString a, NativeString b) => !a.Equals(b);
public bool Equals(NativeString other) => NativeString.AnsiEquals(Chars,other.Chars);
public bool Equals(MarshaledString other) => NativeString.AnsiEquals(Chars,other.Chars);
public void Dispose() {
if(Chars==null) return;
Marshal.FreeHGlobal((IntPtr)Chars);
Chars = null;
}
}
public unsafe struct NativeString {
public override bool Equals(object obj) {
return obj is NativeString other && Equals(other);
}
public override int GetHashCode() {
return unchecked((int) (long) Chars);
}
public byte* Chars;
public NativeString(IntPtr str) {
Chars=(byte*)str;
}
public NativeString(byte* str) {
Chars=str;
}
public override string ToString() {
if(Chars==null||Chars[0]==0) return "";
return Marshal.PtrToStringAnsi((IntPtr) Chars);
}
public static explicit operator string(NativeString str) => str.ToString();
public static bool operator ==(NativeString a, NativeString b) => a.Equals(b);
public static bool operator !=(NativeString a, NativeString b) => !a.Equals(b);
public static bool operator ==(NativeString a, MarshaledString b) => a.Equals(b);
public static bool operator !=(NativeString a, MarshaledString b) => !a.Equals(b);
public static bool AnsiEquals(byte* a, byte* b) {
if(a==b) return true;
if(a==null||b==null) return false;
while ((*a & *b)!=0) {
if(*a++!=*b++)return false;
}
return true;
}
public bool Equals(NativeString other) => AnsiEquals(Chars,other.Chars);
public bool Equals(MarshaledString other) => AnsiEquals(Chars,other.Chars);
}
}