public Bytes tobytes() { CheckBuffer(); if (_matchesBuffer && _step == 1) { return(_buffer.ToBytes(_start / _itemsize, _end / _itemsize)); } byte[] bytes = getByteRange(_start, numberOfElements() * _itemsize); if (_step == 1) { return(Bytes.Make(bytes)); } // getByteRange() doesn't care about our _step, so if we have one // that isn't 1, we will need to get rid of any bytes we don't care // about and potentially adjust for a reversed memoryview. byte[] stridedBytes = new byte[bytes.Length / _itemsize]; for (int indexStrided = 0; indexStrided < stridedBytes.Length; indexStrided += _itemsize) { int indexInBytes = indexStrided * _step; for (int j = 0; j < _itemsize; j++) { stridedBytes[indexStrided + j] = bytes[indexInBytes + j]; } } return(Bytes.Make(stridedBytes)); }
public Bytes tobytes() { CheckBuffer(); if (_shape.Contains(0)) { return(Bytes.Empty); } var buf = _buffer !.AsReadOnlySpan(); if (_isCContig) { return(Bytes.Make(buf.Slice(_offset, _numItems * _itemSize).ToArray())); } byte[] bytes = new byte[_numItems * _itemSize]; int i = 0; foreach (byte b in this.EnumerateBytes()) { bytes[i++] = b; } return(Bytes.Make(bytes)); }
private static object packBytes(string format, ReadOnlySpan <byte> bytes) { if (TypecodeOps.TryGetFromBytes(format, bytes, out object?result)) { return(result); } else { return(Bytes.Make(bytes.ToArray())); } }
public Bytes get_data(CodeContext /*!*/ context, string path) { if (path.Length >= MAXPATHLEN) { throw MakeError(context, "path too long"); } path = path.Replace(_archive, string.Empty).TrimStart(Path.DirectorySeparatorChar); if (!__files.ContainsKey(path)) { throw PythonOps.IOError(path); } var data = GetData(context, _archive, __files[path] as PythonTuple); return(Bytes.Make(data)); }
private object?ReadMappingKey() { // Caller has set _curCh to the character past the %, and // _index to 2 characters past the original '%'. Debug.Assert(_curCh == _str[_index - 1]); Debug.Assert(_str[_index - 2] == '%'); if (_curCh != '(') { // No parenthesized key. return(null); } // CPython supports nested parenthesis (See "S3.6.2:String Formatting Operations"). // Keywords inbetween %(...)s can contain parenthesis. // // For example, here are the keys returned for various format strings: // %(key)s - return 'key' // %((key))s - return '(key)' // %()s - return '' // %((((key))))s - return '(((key)))' // %((%)s)s - return '(%)s' // %((%s))s - return (%s) // %(a(b)c)s - return a(b)c // %((a)s)s - return (a)s // %(((a)s))s - return ((a)s) // Use a counter rule. int nested = 1; // already passed the 1st '(' int start = _index; // character index after 1st opening '(' int end = start; while (end < _str.Length) { if (_str[end] == '(') { nested++; } else if (_str[end] == ')') { nested--; } if (nested == 0) { // Found final matching closing parent string key = _str.Substring(_index, end - start); // Update fields _index = end + 1; if (_index == _str.Length) { // This error could happen with a format string like '%((key))' throw PythonOps.ValueError("incomplete format"); } _curCh = _str[_index++]; if (_asBytes) { return(Bytes.Make(key.MakeByteArray())); } return(key); } end++; } // Error: missing closing ')'. // This could happen with '%((key)s' throw PythonOps.ValueError("incomplete format key"); }
public MemoryView(string @object) : this(Bytes.Make(@object.MakeByteArray())) { }