Skip to content

Commit 919b476

Browse files
committed
add StringArray supporting
1 parent 834c7c6 commit 919b476

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

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

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

35+
/// <summary>
36+
/// Convert to String[] from NDArray
37+
/// </summary>
38+
/// <param name="arr"></param>
39+
/// <returns></returns>
40+
public static string[] AsStringArray(NDArray arr)
41+
{
42+
var chars = arr.ToString();
43+
int pos = chars.IndexOf(' ');
44+
int numOfRows = int.Parse(chars.Substring(0, pos));
45+
int posOfContent = chars.IndexOf(':');
46+
string contents = chars.Substring(posOfContent + 1);
47+
int[] strLengths = chars.Substring(pos + 1, posOfContent - pos - 1)
48+
.Split(' ')
49+
.Select(x => int.Parse(x))
50+
.ToArray();
51+
string[] strArray = new string[numOfRows];
52+
int currentPos = 0;
53+
for (int i = 0; i < numOfRows; i++)
54+
{
55+
strArray[i] = contents.Substring(currentPos, strLengths[i]);
56+
currentPos += strLengths[i];
57+
}
58+
59+
return strArray;
60+
}
61+
3562
/// <summary>
3663
/// Get a string out of a vector of chars.
3764
/// </summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using System.Text;
5+
using NumSharp.Backends;
6+
using NumSharp.Backends.Unmanaged;
7+
8+
namespace NumSharp
9+
{
10+
public static partial class np
11+
{
12+
/// <summary>
13+
/// Create a vector ndarray of type <see cref="string"/>.
14+
/// Encode string array.
15+
/// format: [numOfRow lenOfRow1 lenOfRow2 contents]
16+
/// sample: [2 2 4 aacccc]
17+
/// </summary>
18+
/// <param name="strArray"></param>
19+
/// <returns></returns>
20+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
21+
public static NDArray array(string[] strArray)
22+
{
23+
if (strArray == null)
24+
throw new ArgumentNullException(nameof(strArray));
25+
if (strArray.Length == 0)
26+
return new NDArray(NPTypeCode.String, 0);
27+
28+
// convert to bytes
29+
string meta = $"{strArray.Length}";
30+
foreach (var str in strArray)
31+
meta += $" {str.Length}";
32+
meta += $":{string.Join("", strArray)}";
33+
return new NDArray(meta.ToCharArray());
34+
}
35+
}
36+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Linq;
3+
using FluentAssertions;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using NumSharp.UnitTest.Utilities;
6+
7+
namespace NumSharp.UnitTest.Backends.Unmanaged
8+
{
9+
[TestClass]
10+
public class StringArrayApiTests
11+
{
12+
private static string[] strArray = new string[] { "Hello,", " SciSharp Team!"};
13+
14+
[TestMethod]
15+
public void StringArrayConverting()
16+
{
17+
var nd = np.array(strArray);
18+
nd.Should().BeOfType<char>()
19+
.And.BeShaped(28)
20+
.And.BeOfValues(50, 32, 54, 32, 49, 53, 58, 72, 101, 108, 108, 111, 44, 32, 83, 99, 105, 83, 104, 97, 114, 112, 32, 84, 101, 97, 109, 33);
21+
22+
NDArray.AsStringArray(nd).SequenceEqual(strArray);
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)