-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
namespace Whisper.Utils | ||
{ | ||
public static class TextUtils | ||
{ | ||
/// <summary> | ||
/// Copy null-terminated Utf8 string from native memory to managed. | ||
/// </summary> | ||
public static string StringFromNativeUtf8(IntPtr nativeUtf8) | ||
{ | ||
// check input null | ||
if (nativeUtf8 == IntPtr.Zero) | ||
return null; | ||
|
||
// find null terminator | ||
var len = 0; | ||
while (Marshal.ReadByte(nativeUtf8, len) != 0) ++len; | ||
|
||
// check empty string | ||
if (len == 0) | ||
return ""; | ||
|
||
// copy buffer from beginning to null position | ||
var buffer = new byte[len]; | ||
Marshal.Copy(nativeUtf8, buffer, 0, buffer.Length); | ||
return Encoding.UTF8.GetString(buffer); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters