Ticket Hash: | d3b877c7e0f31d0fcaa20efd8dbaa33b28f42dff | |||
Title: | Optimize SQLiteConvert.UTF8ToString for .NET Standard 2.1 | |||
Status: | Closed | Type: | Performance | |
Severity: | Important | Priority: | Medium | |
Subsystem: | Convert | Resolution: | Fixed | |
Last Modified: | 2020-09-24 01:10:06 | |||
Version Found In: | 1.0.113.0 | |||
User Comments: | ||||
anonymous added on 2020-07-04 12:46:52:
(text/x-markdown)
.NET Standard 2.1 introduced the method `Marshal.PtrToStringUTF8`, which can be used in `SQLiteConvert.UTF8ToString` (similar to using `Marshal.PtrToStringUni` in `SQLite3_UTF16.UTF16ToString`), in order to save allocation of a byte array and to avoid `Marshal.ReadByte()` when using the standard SQLite library. For example: ```c# public static string UTF8ToString(IntPtr nativestring, int nativestringlen) { if (nativestring == IntPtr.Zero || nativestringlen == 0) return String.Empty; #if NETSTANDARD && !NETSTANDARD2_0 if (nativestringlen < 0) return Marshal.PtrToStringUTF8(nativestring); else return Marshal.PtrToStringUTF8(nativestring, nativestringlen); #else if (nativestringlen < 0) { nativestringlen = 0; while (Marshal.ReadByte(nativestring, nativestringlen) != 0) nativestringlen++; if (nativestringlen == 0) return String.Empty; } byte[] byteArray = new byte[nativestringlen]; Marshal.Copy(nativestring, byteArray, 0, nativestringlen); return _utf8.GetString(byteArray, 0, nativestringlen); #endif } ``` Thank you! mistachkin added on 2020-09-24 01:10:06: (text/x-fossil-plain) Fixed on trunk via check-in [9f4378b8510f62b5]. |