Terminate the paths
This commit is contained in:
Родитель
89a9badaec
Коммит
a0af85182e
|
@ -793,7 +793,7 @@ namespace SkiaSharp
|
|||
|
||||
public void DrawAnnotation (SKRect rect, string key, SKData value)
|
||||
{
|
||||
var bytes = StringUtilities.GetEncodedText (key, SKTextEncoding.Utf8);
|
||||
var bytes = StringUtilities.GetEncodedText (key, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* b = bytes) {
|
||||
SkiaApi.sk_canvas_draw_annotation (base.Handle, &rect, b, value == null ? IntPtr.Zero : value.Handle);
|
||||
}
|
||||
|
|
|
@ -87,7 +87,7 @@ namespace SkiaSharp
|
|||
if (string.IsNullOrEmpty (filename))
|
||||
throw new ArgumentException ("The filename cannot be empty.", nameof (filename));
|
||||
|
||||
var utf8path = StringUtilities.GetEncodedText (filename, SKTextEncoding.Utf8);
|
||||
var utf8path = StringUtilities.GetEncodedText (filename, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* u = utf8path) {
|
||||
return GetObject (SkiaApi.sk_data_new_from_file (u));
|
||||
}
|
||||
|
|
|
@ -91,7 +91,7 @@ namespace SkiaSharp
|
|||
if (path == null)
|
||||
throw new ArgumentNullException (nameof (path));
|
||||
|
||||
var utf8path = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8);
|
||||
var utf8path = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* u = utf8path) {
|
||||
return SKTypeface.GetObject (SkiaApi.sk_fontmgr_create_from_file (Handle, u, index));
|
||||
}
|
||||
|
|
|
@ -274,7 +274,7 @@ namespace SkiaSharp
|
|||
|
||||
private static IntPtr CreateNew (string path)
|
||||
{
|
||||
var bytes = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8);
|
||||
var bytes = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* p = bytes) {
|
||||
return SkiaApi.sk_filestream_new (p);
|
||||
}
|
||||
|
@ -487,7 +487,7 @@ namespace SkiaSharp
|
|||
|
||||
private static IntPtr CreateNew (string path)
|
||||
{
|
||||
var bytes = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8);
|
||||
var bytes = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* p = bytes) {
|
||||
return SkiaApi.sk_filewstream_new (p);
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ namespace SkiaSharp
|
|||
if (path == null)
|
||||
throw new ArgumentNullException (nameof (path));
|
||||
|
||||
var utf8path = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8);
|
||||
var utf8path = StringUtilities.GetEncodedText (path, SKTextEncoding.Utf8, true);
|
||||
fixed (byte* u = utf8path) {
|
||||
return GetObject (SkiaApi.sk_typeface_create_from_file (u, index));
|
||||
}
|
||||
|
|
|
@ -77,6 +77,8 @@ namespace SkiaSharp
|
|||
|
||||
public unsafe static class StringUtilities
|
||||
{
|
||||
internal const string NullTerminator = "\0";
|
||||
|
||||
// GetUnicodeStringLength
|
||||
|
||||
private static int GetUnicodeStringLength (SKTextEncoding encoding) =>
|
||||
|
@ -111,6 +113,14 @@ namespace SkiaSharp
|
|||
public static byte[] GetEncodedText (string text, SKTextEncoding encoding) =>
|
||||
GetEncodedText (text.AsSpan (), encoding);
|
||||
|
||||
internal static byte[] GetEncodedText (string text, SKTextEncoding encoding, bool addNull)
|
||||
{
|
||||
if (!string.IsNullOrEmpty (text) && addNull)
|
||||
text += NullTerminator;
|
||||
|
||||
return GetEncodedText (text.AsSpan (), encoding);
|
||||
}
|
||||
|
||||
public static byte[] GetEncodedText (ReadOnlySpan<char> text, SKTextEncoding encoding) =>
|
||||
encoding switch
|
||||
{
|
||||
|
|
|
@ -37,6 +37,24 @@ namespace SkiaSharp.Tests
|
|||
Assert.Equal(OddData, data.ToArray());
|
||||
}
|
||||
|
||||
[SkippableTheory]
|
||||
[InlineData(null, 0, 0, 0)]
|
||||
[InlineData("", 0, 0, 0)]
|
||||
[InlineData("H", 1, 1, 2)]
|
||||
[InlineData("Hello World!", 12, 12, 13)]
|
||||
[InlineData("Hello World!!", 13, 13, 14)]
|
||||
[InlineData("上田雅美", 4, 12, 13)]
|
||||
public void StringsAreConvertedWithNullTerminator(string str, int length, int byteLength, int terminatedLength)
|
||||
{
|
||||
Assert.Equal(length, str?.Length ?? 0);
|
||||
|
||||
var bytes = StringUtilities.GetEncodedText(str, SKTextEncoding.Utf8);
|
||||
Assert.Equal(byteLength, bytes.Length);
|
||||
|
||||
bytes = StringUtilities.GetEncodedText(str, SKTextEncoding.Utf8, true);
|
||||
Assert.Equal(terminatedLength, bytes.Length);
|
||||
}
|
||||
|
||||
[SkippableFact]
|
||||
public void DataCanBeCreatedFromStream()
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче