Skip to content

Commit fcb6361

Browse files
committed
np.array.string: re-added NDArray array(string str)
1 parent a0fc321 commit fcb6361

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/NumSharp.Core/Creation/np.array.string.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,33 @@ public static NDArray array(string[] strArray)
3232
meta += $":{string.Join("", strArray)}";
3333
return new NDArray(meta.ToCharArray());
3434
}
35+
36+
/// <summary>
37+
/// Create a vector ndarray of type <see cref="char"/>.
38+
/// </summary>
39+
/// <param name="str">The string to turn into NDArray(NPTypeCode.Char)</param>
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
41+
public static NDArray array(string str)
42+
{
43+
if (str == null)
44+
throw new ArgumentNullException(nameof(str));
45+
46+
if (str.Length == 0)
47+
return new NDArray(NPTypeCode.Char, 0);
48+
49+
unsafe
50+
{
51+
var ret = new ArraySlice<char>(new UnmanagedMemoryBlock<char>(str.Length));
52+
fixed (char* strChars = str)
53+
{
54+
var src = strChars;
55+
var dst = ret.Address;
56+
var len = sizeof(char) * str.Length;
57+
Buffer.MemoryCopy(src, dst, len, len);
58+
}
59+
60+
return new NDArray(ret);
61+
}
62+
}
3563
}
3664
}

0 commit comments

Comments
 (0)