From 2c436606bdf6474eb803b97d521ed564da7b6e0b Mon Sep 17 00:00:00 2001 From: Fangjun Kuang Date: Sun, 2 Jul 2023 22:57:48 +0800 Subject: [PATCH] Support .Netstandard 2.0 (#193) --- CMakeLists.txt | 2 +- scripts/dotnet/offline.cs | 21 ++++++++++++++++++- scripts/dotnet/online.cs | 22 ++++++++++++++++++-- scripts/dotnet/sherpa-onnx.csproj.in | 2 +- scripts/dotnet/sherpa-onnx.csproj.runtime.in | 2 +- 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc533dc74..0512038f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.13 FATAL_ERROR) project(sherpa-onnx) -set(SHERPA_ONNX_VERSION "1.4.7") +set(SHERPA_ONNX_VERSION "1.4.8") # Disable warning about # diff --git a/scripts/dotnet/offline.cs b/scripts/dotnet/offline.cs index 295f88f7c..afbec42a1 100644 --- a/scripts/dotnet/offline.cs +++ b/scripts/dotnet/offline.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Collections.Generic; using System.Runtime.InteropServices; +using System.Text; using System; namespace SherpaOnnx @@ -116,7 +117,25 @@ public class OfflineRecognizerResult public OfflineRecognizerResult(IntPtr handle) { Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); - _text = Marshal.PtrToStringUTF8(impl.Text); + + // PtrToStringUTF8() requires .net standard 2.1 + // _text = Marshal.PtrToStringUTF8(impl.Text); + + int length = 0; + + unsafe + { + byte* buffer = (byte*)impl.Text; + while (*buffer != 0) + { + ++buffer; + } + length = (int)(buffer - (byte*)impl.Text); + } + + byte[] stringBuffer = new byte[length]; + Marshal.Copy(impl.Text, stringBuffer, 0, length); + _text = Encoding.UTF8.GetString(stringBuffer); } [StructLayout(LayoutKind.Sequential)] diff --git a/scripts/dotnet/online.cs b/scripts/dotnet/online.cs index d86d15205..de30f459d 100644 --- a/scripts/dotnet/online.cs +++ b/scripts/dotnet/online.cs @@ -1,9 +1,10 @@ /// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang) /// Copyright (c) 2023 by manyeyes -using System.Linq; using System.Collections.Generic; +using System.Linq; using System.Runtime.InteropServices; +using System.Text; using System; namespace SherpaOnnx @@ -116,7 +117,24 @@ public class OnlineRecognizerResult public OnlineRecognizerResult(IntPtr handle) { Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); - _text = Marshal.PtrToStringUTF8(impl.Text); + // PtrToStringUTF8() requires .net standard 2.1 + // _text = Marshal.PtrToStringUTF8(impl.Text); + + int length = 0; + + unsafe + { + byte* buffer = (byte*)impl.Text; + while (*buffer != 0) + { + ++buffer; + } + length = (int)(buffer - (byte*)impl.Text); + } + + byte[] stringBuffer = new byte[length]; + Marshal.Copy(impl.Text, stringBuffer, 0, length); + _text = Encoding.UTF8.GetString(stringBuffer); } [StructLayout(LayoutKind.Sequential)] diff --git a/scripts/dotnet/sherpa-onnx.csproj.in b/scripts/dotnet/sherpa-onnx.csproj.in index 2f40237e6..4063bfe4e 100644 --- a/scripts/dotnet/sherpa-onnx.csproj.in +++ b/scripts/dotnet/sherpa-onnx.csproj.in @@ -4,7 +4,7 @@ README.md Library 10.0 - netstandard2.1;netcoreapp3.1;net6.0;net7.0 + netstandard2.0;netcoreapp3.1;net6.0;net7.0 linux-x64;osx-x64;win-x64 true sherpa-onnx diff --git a/scripts/dotnet/sherpa-onnx.csproj.runtime.in b/scripts/dotnet/sherpa-onnx.csproj.runtime.in index f364f8aaa..335254e16 100644 --- a/scripts/dotnet/sherpa-onnx.csproj.runtime.in +++ b/scripts/dotnet/sherpa-onnx.csproj.runtime.in @@ -3,7 +3,7 @@ Apache-2.0 README.md Library - netstandard2.0;netcoreapp3.1;net6.0 + netstandard2.0;netcoreapp3.1;net6.0;net7.0 {{ dotnet_rid }} sherpa-onnx {{ version }}