File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
src/NumSharp.Core/Creation Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -32,5 +32,33 @@ public static NDArray array(string[] strArray)
32
32
meta += $ ":{ string . Join ( "" , strArray ) } ";
33
33
return new NDArray ( meta . ToCharArray ( ) ) ;
34
34
}
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
+ }
35
63
}
36
64
}
You can’t perform that action at this time.
0 commit comments