Skip to content

Commit 3b851d4

Browse files
committed
Reverted commit: add string[] support.
1 parent 3bd46ee commit 3b851d4

File tree

4 files changed

+28
-112
lines changed

4 files changed

+28
-112
lines changed

src/NumSharp.Core/Backends/NDArray.String.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,6 @@ public static string AsString(NDArray arr)
3232
}
3333
}
3434

35-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36-
public static string[] AsStringArray(NDArray arr)
37-
{
38-
var chars = arr.ToString();
39-
int pos = chars.IndexOf(' ');
40-
int numOfRows = int.Parse(chars.Substring(0, pos));
41-
int posOfContent = chars.IndexOf(':');
42-
string contents = chars.Substring(posOfContent + 1);
43-
int[] strLengths = chars.Substring(pos + 1, posOfContent - pos - 1)
44-
.Split(' ')
45-
.Select(x => int.Parse(x))
46-
.ToArray();
47-
string[] strArray = new string[numOfRows];
48-
int currentPos = 0;
49-
for (int i = 0; i < numOfRows; i++)
50-
{
51-
strArray[i] = contents.Substring(currentPos, strLengths[i]);
52-
currentPos += strLengths[i];
53-
}
54-
55-
return strArray;
56-
}
57-
5835
/// <summary>
5936
/// Get a string out of a vector of chars.
6037
/// </summary>

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,34 @@ public static NDArray array(Array array, Type dtype = null, int ndmin = 1, bool
4949

5050
public static NDArray array<T>(params T[] data) where T : unmanaged => new NDArray(ArraySlice.FromArray(data), Shape.Vector(data.Length));
5151

52+
/// <summary>
53+
/// Create a vector ndarray of type <see cref="char"/>.
54+
/// </summary>
55+
/// <param name="chars"></param>
56+
/// <returns></returns>
57+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
58+
public static NDArray array(string chars)
59+
{
60+
if (chars == null)
61+
throw new ArgumentNullException(nameof(chars));
62+
if (chars.Length == 0)
63+
return new NDArray(NPTypeCode.Char, 0);
64+
65+
unsafe
66+
{
67+
var ret = new ArraySlice<char>(new UnmanagedMemoryBlock<char>(chars.Length));
68+
fixed (char* strChars = chars)
69+
{
70+
var src = strChars;
71+
var dst = ret.Address;
72+
var len = sizeof(char) * chars.Length;
73+
Buffer.MemoryCopy(src, dst, len, len);
74+
}
75+
76+
return new NDArray(ret);
77+
}
78+
}
79+
5280
public static NDArray array<T>(T[][] data)
5381
{
5482
var array = data.SelectMany(inner => inner).ToArray(); //todo! not use selectmany.

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

Lines changed: 0 additions & 64 deletions
This file was deleted.

test/NumSharp.UnitTest/Backends/Unmanaged/StringArrayApiTests.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)