Class ToStringCastingImpl
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
A cast INTO a string class from any other non-datetime class — a numeric class, or the other string class —
NumPy's legacy_cast_to_string (add_other_to_and_from_string_cast +
cast_to_string_resolve_descriptors). Its class-level safety is same_kind ("safe when the
destination is long enough") for every source but U, and unsafe for U → S (a code point
above 127 cannot be stored in a byte string).
public sealed class ToStringCastingImpl : CastingImpl
- Inheritance
-
ToStringCastingImpl
- Inherited Members
Remarks
The length a source needs is NumPy's REQUIRED_STR_LEN estimate, in characters: 5 for bool
(False); 3/5/10/20 for 1/2/4/8-byte unsigned integers and one more for the sign of a signed one;
32 for every float; 64 for complex; the source's own length for S and U. NumSharp's two extra
numeric classes take the numeric class they masquerade as: char sizes as uint16 (5) and
decimal as a float (32, enough for the 31 characters of -0.0000000000000000000000000001).
Its strided loop (get_strided_loop(ArrayMethodContext, bool, long[], out NDArrayMethodFlags)) formats a number as its scalar str()
(FormatRun(byte*, long, NPTypeCode, byte*, long, bool, int, long)) and moves an S ↔ U element after its
ASCII check (TransferElements(byte*, long, int, bool, byte*, long, int, bool, long)); a whole-array S ↔ U
copy keeps the bulk unit-view route, which checks every element before it writes.
Methods
get_strided_loop(ArrayMethodContext, bool, long[], out NDArrayMethodFlags)
NumPy's legacy casts into a text class, run per strided run: a number's scalar str() truncated to the
destination width and NUL-padded (@TYPE@_to_STRING / @TYPE@_to_UNICODE,
FormatRun(byte*, long, NPTypeCode, byte*, long, bool, int, long); formatting cannot fail), or an element of the OTHER
text class decoded / encoded whole, then truncated or padded (STRING_to_UNICODE /
UNICODE_to_STRING, TransferElements(byte*, long, int, bool, byte*, long, int, bool, long): an element that is not
ASCII raises before the run writes anything).
public override NDInnerLoopFunc get_strided_loop(ArrayMethodContext context, bool aligned, long[] strides, out NDArrayMethodFlags flags)
Parameters
contextArrayMethodContextThe resolved descriptors: [0] the source (a number's lane, or the other text class and its width), [1] the text destination (its class and width).
alignedboolUnused: the loop reads and writes unaligned elements.
strideslong[]Unused: the loop takes the strides of each call.
flagsNDArrayMethodFlagsReceives the method's flags.
Returns
- NDInnerLoopFunc
The loop: [0] the source run, [1] the text run, byte strides.
resolve_descriptors(DType[], DType[], out long)
NumPy's cast_to_string_resolve_descriptors: the destination length is the source's
RequiredLength(DType) when none is given; a given destination is used (canonicalised) whatever its length
— the cast is then safe when it is long enough and same_kind otherwise (always unsafe for
U → S).
public override NPY_CASTING resolve_descriptors(DType[] givenDescrs, DType[] loopDescrs, out long viewOffset)
Parameters
givenDescrsDType[]The source descriptor and the requested destination (null = "pick the length").
loopDescrsDType[]Receives the descriptors the cast runs on.
viewOffsetlongAlways NoView: a conversion to text is never a view.
Returns
- NPY_CASTING
The cast safety.
Exceptions
- TypeError
A
Udestination longer than a descriptor can describe (string of length N is too large to store inside array.).