Esempio n. 1
0
 internal static string SliceToString(slice str, DStringType type)
 {
     try {
         unsafe {
             var bytes = (byte *)str.ptr.ToPointer();
             if (bytes == null)
             {
                 return(null);
             }
             if (type == DStringType._string)
             {
                 return(String.Copy(System.Text.Encoding.UTF8.GetString(bytes, str.length.ToInt32() * (int)type)));
             }
             else if (type == DStringType._wstring)
             {
                 return(String.Copy(System.Text.Encoding.Unicode.GetString(bytes, str.length.ToInt32() * (int)type)));
             }
             else if (type == DStringType._dstring)
             {
                 return(String.Copy(System.Text.Encoding.UTF32.GetString(bytes, str.length.ToInt32() * (int)type)));
             }
             else
             {
                 throw new UnauthorizedAccessException("Unable to convert D string to C# string: Unrecognized string type.");
             }
         }
     } finally {
         SharedFunctions.ReleaseMemory(str.ptr);
     }
 }
Esempio n. 2
0
    internal slice ToSlice(DStringType type = DStringType.None)
    {
        if (type == DStringType.None && _strings != null)
        {
            throw new TypeAccessException("Cannot pass a range of strings to a non-string range.");
        }

        if (_strings == null)
        {
            return(_slice);
        }
        else
        {
            _type = type;
            if (_type == DStringType._string)
            {
                _slice = RangeFunctions.String_Create(new IntPtr(_strings.Count()));
                foreach (var s in _strings)
                {
                    _slice = RangeFunctions.String_AppendValue(_slice, SharedFunctions.CreateString(s));
                }
            }
            else if (_type == DStringType._wstring)
            {
                _slice = RangeFunctions.Wstring_Create(new IntPtr(_strings.Count()));
                foreach (var s in _strings)
                {
                    _slice = RangeFunctions.Wstring_AppendValue(_slice, SharedFunctions.CreateWstring(s));
                }
            }
            else if (_type == DStringType._dstring)
            {
                _slice = RangeFunctions.Dstring_Create(new IntPtr(_strings.Count()));
                foreach (var s in _strings)
                {
                    _slice = RangeFunctions.Dstring_AppendValue(_slice, SharedFunctions.CreateDstring(s));
                }
            }
            _strings = null;
            return(_slice);
        }
    }
Esempio n. 3
0
 internal Range(slice range, DStringType type)
 {
     this._slice = range;
     this._type  = type;
 }