正在显示
5 个修改的文件
包含
43 行增加
和
6 行删除
| @@ -4,6 +4,7 @@ | @@ -4,6 +4,7 @@ | ||
| 4 | using System.Linq; | 4 | using System.Linq; |
| 5 | using System.Collections.Generic; | 5 | using System.Collections.Generic; |
| 6 | using System.Runtime.InteropServices; | 6 | using System.Runtime.InteropServices; |
| 7 | +using System.Text; | ||
| 7 | using System; | 8 | using System; |
| 8 | 9 | ||
| 9 | namespace SherpaOnnx | 10 | namespace SherpaOnnx |
| @@ -116,7 +117,25 @@ namespace SherpaOnnx | @@ -116,7 +117,25 @@ namespace SherpaOnnx | ||
| 116 | public OfflineRecognizerResult(IntPtr handle) | 117 | public OfflineRecognizerResult(IntPtr handle) |
| 117 | { | 118 | { |
| 118 | Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); | 119 | Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); |
| 119 | - _text = Marshal.PtrToStringUTF8(impl.Text); | 120 | + |
| 121 | + // PtrToStringUTF8() requires .net standard 2.1 | ||
| 122 | + // _text = Marshal.PtrToStringUTF8(impl.Text); | ||
| 123 | + | ||
| 124 | + int length = 0; | ||
| 125 | + | ||
| 126 | + unsafe | ||
| 127 | + { | ||
| 128 | + byte* buffer = (byte*)impl.Text; | ||
| 129 | + while (*buffer != 0) | ||
| 130 | + { | ||
| 131 | + ++buffer; | ||
| 132 | + } | ||
| 133 | + length = (int)(buffer - (byte*)impl.Text); | ||
| 134 | + } | ||
| 135 | + | ||
| 136 | + byte[] stringBuffer = new byte[length]; | ||
| 137 | + Marshal.Copy(impl.Text, stringBuffer, 0, length); | ||
| 138 | + _text = Encoding.UTF8.GetString(stringBuffer); | ||
| 120 | } | 139 | } |
| 121 | 140 | ||
| 122 | [StructLayout(LayoutKind.Sequential)] | 141 | [StructLayout(LayoutKind.Sequential)] |
| 1 | /// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang) | 1 | /// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang) |
| 2 | /// Copyright (c) 2023 by manyeyes | 2 | /// Copyright (c) 2023 by manyeyes |
| 3 | 3 | ||
| 4 | -using System.Linq; | ||
| 5 | using System.Collections.Generic; | 4 | using System.Collections.Generic; |
| 5 | +using System.Linq; | ||
| 6 | using System.Runtime.InteropServices; | 6 | using System.Runtime.InteropServices; |
| 7 | +using System.Text; | ||
| 7 | using System; | 8 | using System; |
| 8 | 9 | ||
| 9 | namespace SherpaOnnx | 10 | namespace SherpaOnnx |
| @@ -116,7 +117,24 @@ namespace SherpaOnnx | @@ -116,7 +117,24 @@ namespace SherpaOnnx | ||
| 116 | public OnlineRecognizerResult(IntPtr handle) | 117 | public OnlineRecognizerResult(IntPtr handle) |
| 117 | { | 118 | { |
| 118 | Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); | 119 | Impl impl = (Impl)Marshal.PtrToStructure(handle, typeof(Impl)); |
| 119 | - _text = Marshal.PtrToStringUTF8(impl.Text); | 120 | + // PtrToStringUTF8() requires .net standard 2.1 |
| 121 | + // _text = Marshal.PtrToStringUTF8(impl.Text); | ||
| 122 | + | ||
| 123 | + int length = 0; | ||
| 124 | + | ||
| 125 | + unsafe | ||
| 126 | + { | ||
| 127 | + byte* buffer = (byte*)impl.Text; | ||
| 128 | + while (*buffer != 0) | ||
| 129 | + { | ||
| 130 | + ++buffer; | ||
| 131 | + } | ||
| 132 | + length = (int)(buffer - (byte*)impl.Text); | ||
| 133 | + } | ||
| 134 | + | ||
| 135 | + byte[] stringBuffer = new byte[length]; | ||
| 136 | + Marshal.Copy(impl.Text, stringBuffer, 0, length); | ||
| 137 | + _text = Encoding.UTF8.GetString(stringBuffer); | ||
| 120 | } | 138 | } |
| 121 | 139 | ||
| 122 | [StructLayout(LayoutKind.Sequential)] | 140 | [StructLayout(LayoutKind.Sequential)] |
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | <PackageReadmeFile>README.md</PackageReadmeFile> | 4 | <PackageReadmeFile>README.md</PackageReadmeFile> |
| 5 | <OutputType>Library</OutputType> | 5 | <OutputType>Library</OutputType> |
| 6 | <LangVersion>10.0</LangVersion> | 6 | <LangVersion>10.0</LangVersion> |
| 7 | - <TargetFrameworks>netstandard2.1;netcoreapp3.1;net6.0;net7.0</TargetFrameworks> | 7 | + <TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net7.0</TargetFrameworks> |
| 8 | <RuntimeIdentifiers>linux-x64;osx-x64;win-x64</RuntimeIdentifiers> | 8 | <RuntimeIdentifiers>linux-x64;osx-x64;win-x64</RuntimeIdentifiers> |
| 9 | <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | 9 | <AllowUnsafeBlocks>true</AllowUnsafeBlocks> |
| 10 | <AssemblyName>sherpa-onnx</AssemblyName> | 10 | <AssemblyName>sherpa-onnx</AssemblyName> |
| @@ -3,7 +3,7 @@ | @@ -3,7 +3,7 @@ | ||
| 3 | <PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> | 3 | <PackageLicenseExpression>Apache-2.0</PackageLicenseExpression> |
| 4 | <PackageReadmeFile>README.md</PackageReadmeFile> | 4 | <PackageReadmeFile>README.md</PackageReadmeFile> |
| 5 | <OutputType>Library</OutputType> | 5 | <OutputType>Library</OutputType> |
| 6 | - <TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0</TargetFrameworks> | 6 | + <TargetFrameworks>netstandard2.0;netcoreapp3.1;net6.0;net7.0</TargetFrameworks> |
| 7 | <RuntimeIdentifier>{{ dotnet_rid }}</RuntimeIdentifier> | 7 | <RuntimeIdentifier>{{ dotnet_rid }}</RuntimeIdentifier> |
| 8 | <AssemblyName>sherpa-onnx</AssemblyName> | 8 | <AssemblyName>sherpa-onnx</AssemblyName> |
| 9 | <Version>{{ version }}</Version> | 9 | <Version>{{ version }}</Version> |
-
请 注册 或 登录 后发表评论