mirror of
https://github.com/raylib-cs/raylib-cs
synced 2025-09-09 03:01:41 -04:00
Multi-target net5.0
This commit is contained in:
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -20,9 +20,9 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
dotnet-version: 6.0.x
|
dotnet-version: 6.0.x
|
||||||
- name: Build project
|
- name: Build project
|
||||||
run: dotnet build -c Release
|
run: dotnet build -c Release -f net6.0
|
||||||
- name: Test project
|
- name: Test project
|
||||||
run: dotnet test
|
run: dotnet test -c Release -f net6.0
|
||||||
publish:
|
publish:
|
||||||
name: Build Release Package
|
name: Build Release Package
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
@@ -12,7 +12,7 @@ C# bindings for raylib 4.0.0, a simple and easy-to-use library to learn videogam
|
|||||||
|
|
||||||
[](https://github.com/ChrisDill/Raylib-cs/actions?query=workflow%3ABuild)
|
[](https://github.com/ChrisDill/Raylib-cs/actions?query=workflow%3ABuild)
|
||||||
|
|
||||||
Raylib-cs targets net6.0.
|
Raylib-cs targets net5.0 and net6.0.
|
||||||
|
|
||||||
## Installation - NuGet
|
## Installation - NuGet
|
||||||
|
|
||||||
|
@@ -62,7 +62,7 @@ namespace Raylib_cs.Tests
|
|||||||
int count = 0;
|
int count = 0;
|
||||||
string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē";
|
string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē";
|
||||||
int[] codepoints1 = Raylib.LoadCodepoints(input, ref count);
|
int[] codepoints1 = Raylib.LoadCodepoints(input, ref count);
|
||||||
|
|
||||||
for (int i = 0; i < input.Length; i++)
|
for (int i = 0; i < input.Length; i++)
|
||||||
{
|
{
|
||||||
Assert.Equal(input[i], codepoints1[i]);
|
Assert.Equal(input[i], codepoints1[i]);
|
||||||
@@ -81,7 +81,7 @@ namespace Raylib_cs.Tests
|
|||||||
public void TextCodepointsToUTF8()
|
public void TextCodepointsToUTF8()
|
||||||
{
|
{
|
||||||
string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē";
|
string input = "aàáâãäāăąȧXǎȁȃeèéêẽëē";
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
int[] codepoints1 = Raylib.LoadCodepoints(input, ref count);
|
int[] codepoints1 = Raylib.LoadCodepoints(input, ref count);
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
|
||||||
<EnableDefaultItems>false</EnableDefaultItems>
|
<EnableDefaultItems>false</EnableDefaultItems>
|
||||||
<AssemblyName>Raylib-cs</AssemblyName>
|
<AssemblyName>Raylib-cs</AssemblyName>
|
||||||
<RootNamespace>Raylib_cs</RootNamespace>
|
<RootNamespace>Raylib_cs</RootNamespace>
|
||||||
|
@@ -97,7 +97,7 @@ namespace Raylib_cs
|
|||||||
return ImageTextEx(font, p, fontSize, spacing, tint);
|
return ImageTextEx(font, p, fontSize, spacing, tint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void ImageToPOT(ref Image image, Color fill)
|
public static void ImageToPOT(ref Image image, Color fill)
|
||||||
{
|
{
|
||||||
fixed (Image* p = &image)
|
fixed (Image* p = &image)
|
||||||
@@ -274,6 +274,24 @@ namespace Raylib_cs
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="ImageDrawPixel(Image*, int, int, Color)"/>
|
||||||
|
public static void ImageDrawPixel(ref Image dst, int posX, int posY, Color color)
|
||||||
|
{
|
||||||
|
fixed (Image* p = &dst)
|
||||||
|
{
|
||||||
|
ImageDrawPixel(p, posX, posY, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="ImageDrawPixelV(Image*, Vector2, Color)"/>
|
||||||
|
public static void ImageDrawPixelV(ref Image dst, Vector2 position, Color color)
|
||||||
|
{
|
||||||
|
fixed (Image* p = &dst)
|
||||||
|
{
|
||||||
|
ImageDrawPixelV(p, position, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc cref="ImageDrawText(Image*, byte*, int, int, int, Color)"/>
|
/// <inheritdoc cref="ImageDrawText(Image*, byte*, int, int, int, Color)"/>
|
||||||
public static void ImageDrawText(ref Image dst, Utf8String text, int x, int y, int fontSize, Color color)
|
public static void ImageDrawText(ref Image dst, Utf8String text, int x, int y, int fontSize, Color color)
|
||||||
{
|
{
|
||||||
|
@@ -1,30 +1,31 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Runtime.InteropServices;
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Raylib_cs;
|
namespace Raylib_cs
|
||||||
|
|
||||||
[StructLayout(LayoutKind.Sequential)]
|
|
||||||
public readonly struct CBool
|
|
||||||
{
|
{
|
||||||
private readonly byte value;
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
public readonly struct CBool
|
||||||
private CBool(bool value)
|
|
||||||
{
|
{
|
||||||
this.value = Convert.ToByte(value);
|
private readonly byte value;
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator CBool(bool value)
|
private CBool(bool value)
|
||||||
{
|
{
|
||||||
return new CBool(value);
|
this.value = Convert.ToByte(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator bool(CBool x)
|
public static implicit operator CBool(bool value)
|
||||||
{
|
{
|
||||||
return Convert.ToBoolean(x.value);
|
return new CBool(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public static implicit operator bool(CBool x)
|
||||||
{
|
{
|
||||||
return Convert.ToBoolean(value).ToString();
|
return Convert.ToBoolean(x.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return Convert.ToBoolean(value).ToString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,209 +1,210 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace Raylib_cs;
|
namespace Raylib_cs
|
||||||
|
|
||||||
#region LICENSE
|
|
||||||
|
|
||||||
// ** -- TAKEN FROM SQLitePCL.raw -- ** \\
|
|
||||||
/* https://github.com/ericsink/SQLitePCL.raw */
|
|
||||||
/*
|
|
||||||
Copyright 2014-2021 SourceGear, LLC
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// CHANGES:
|
|
||||||
// - Update Method Names
|
|
||||||
// - Implicit Conversion From string
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// A raw string representation suitable for passing into many core SQLite apis. These will normally be pointers to
|
|
||||||
/// utf8 encoded bytes, with a trailing <c>\0</c> terminator. <see langword="null"/> strings can be represented as
|
|
||||||
/// well as empty strings.
|
|
||||||
/// </summary>
|
|
||||||
public readonly ref struct Utf8String
|
|
||||||
{
|
{
|
||||||
// this span will contain a zero terminator byte
|
#region LICENSE
|
||||||
// if sp.Length is 0, it represents a null string
|
|
||||||
// if sp.Length is 1, the only byte must be zero, and it is an empty string
|
|
||||||
readonly ReadOnlySpan<byte> sp;
|
|
||||||
|
|
||||||
public ref readonly byte GetPinnableReference()
|
// ** -- TAKEN FROM SQLitePCL.raw -- ** \\
|
||||||
{
|
/* https://github.com/ericsink/SQLitePCL.raw */
|
||||||
return ref sp.GetPinnableReference();
|
/*
|
||||||
}
|
Copyright 2014-2021 SourceGear, LLC
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
private Utf8String(ReadOnlySpan<byte> a)
|
// CHANGES:
|
||||||
{
|
// - Update Method Names
|
||||||
// no check here. anything that calls this
|
// - Implicit Conversion From string
|
||||||
// constructor must make assurances about the
|
|
||||||
// zero terminator.
|
#endregion
|
||||||
sp = a;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
/// A raw string representation suitable for passing into many core SQLite apis. These will normally be pointers to
|
||||||
/// name="span"/>. The span must contain a valid <see cref="Encoding.UTF8"/> encoded block of memory that
|
/// utf8 encoded bytes, with a trailing <c>\0</c> terminator. <see langword="null"/> strings can be represented as
|
||||||
/// terminates with a <c>\0</c> byte. The span passed in must include the <c>\0</c> terminator.
|
/// well as empty strings.
|
||||||
/// <para/>
|
|
||||||
/// Both <see langword="null"/> and empty strings can be created here. To create a <see langword="null"/> string,
|
|
||||||
/// pass in an empty <see cref="ReadOnlySpan{T}"/>. To create an empty string, pass in a span with length 1, that
|
|
||||||
/// only contains a <c>\0</c>
|
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="ArgumentException">
|
public readonly ref struct Utf8String
|
||||||
/// Thrown if <c>span.Length > 0</c> and <c>span[^1]</c> is not <c>\0</c>.
|
|
||||||
/// </exception>
|
|
||||||
public static Utf8String FromSpan(ReadOnlySpan<byte> span)
|
|
||||||
{
|
{
|
||||||
if (
|
// this span will contain a zero terminator byte
|
||||||
span.Length > 0
|
// if sp.Length is 0, it represents a null string
|
||||||
&& span[^1] != 0
|
// if sp.Length is 1, the only byte must be zero, and it is an empty string
|
||||||
)
|
readonly ReadOnlySpan<byte> sp;
|
||||||
|
|
||||||
|
public ref readonly byte GetPinnableReference()
|
||||||
{
|
{
|
||||||
throw new ArgumentException("zero terminator required");
|
return ref sp.GetPinnableReference();
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Utf8String(span);
|
private Utf8String(ReadOnlySpan<byte> a)
|
||||||
}
|
|
||||||
|
|
||||||
public static Utf8String FromString(string s)
|
|
||||||
{
|
|
||||||
if (s == null)
|
|
||||||
{
|
{
|
||||||
return new Utf8String(ReadOnlySpan<byte>.Empty);
|
// no check here. anything that calls this
|
||||||
|
// constructor must make assurances about the
|
||||||
|
// zero terminator.
|
||||||
|
sp = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Utf8String(s.ToUtf8String());
|
/// <summary>
|
||||||
}
|
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
||||||
|
/// name="span"/>. The span must contain a valid <see cref="Encoding.UTF8"/> encoded block of memory that
|
||||||
static unsafe long GetLength(byte* p)
|
/// terminates with a <c>\0</c> byte. The span passed in must include the <c>\0</c> terminator.
|
||||||
{
|
/// <para/>
|
||||||
var q = p;
|
/// Both <see langword="null"/> and empty strings can be created here. To create a <see langword="null"/> string,
|
||||||
while (*q != 0)
|
/// pass in an empty <see cref="ReadOnlySpan{T}"/>. To create an empty string, pass in a span with length 1, that
|
||||||
|
/// only contains a <c>\0</c>
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="ArgumentException">
|
||||||
|
/// Thrown if <c>span.Length > 0</c> and <c>span[^1]</c> is not <c>\0</c>.
|
||||||
|
/// </exception>
|
||||||
|
public static Utf8String FromSpan(ReadOnlySpan<byte> span)
|
||||||
{
|
{
|
||||||
q++;
|
if (
|
||||||
}
|
span.Length > 0
|
||||||
|
&& span[^1] != 0
|
||||||
return q - p;
|
)
|
||||||
}
|
|
||||||
|
|
||||||
static unsafe ReadOnlySpan<byte> FindZeroTerminator(byte* p)
|
|
||||||
{
|
|
||||||
var len = (int)GetLength(p);
|
|
||||||
return new ReadOnlySpan<byte>(p, len + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
|
||||||
/// name="p"/>. The pointer must either be <see langword="null"/> or point to a valid <see
|
|
||||||
/// cref="Encoding.UTF8"/> encoded block of memory that terminates with a <c>\0</c> byte.
|
|
||||||
/// </summary>
|
|
||||||
public static unsafe Utf8String FromPtr(byte* p)
|
|
||||||
{
|
|
||||||
return p == null ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO maybe remove this and just use FromSpan?
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
|
||||||
/// name="p"/> with length <paramref name="len"/>. The pointer must be to a valid <see cref="Encoding.UTF8"/>
|
|
||||||
/// encoded block of memory that terminates with a <c>\0</c> byte. The <paramref name="len"/> value refers to
|
|
||||||
/// the number of bytes in the utf8 encoded value <em>not</em> including the <c>\0</c> byte terminator.
|
|
||||||
/// <para/>
|
|
||||||
/// <paramref name="p"/> can be <see langword="null"/>, in which case <paramref name="len"/> is ignored
|
|
||||||
/// and a new <see cref="Utf8String"/> instance is created that represents <see langword="null"/>. Note that this
|
|
||||||
/// different from a pointer to a single <c>\0</c> byte and a length of one. That would represent an empty <see
|
|
||||||
/// cref="Utf8String"/> string.
|
|
||||||
/// </summary>
|
|
||||||
/// <exception cref="ArgumentException">
|
|
||||||
/// Thrown if <paramref name="p"/> is not <see langword="null"/> and <c>p[len]</c> is not <c>\0</c>.
|
|
||||||
/// </exception>
|
|
||||||
public static unsafe Utf8String FromPtrLen(byte* p, int len)
|
|
||||||
{
|
|
||||||
if (p == null)
|
|
||||||
{
|
|
||||||
return new Utf8String(ReadOnlySpan<byte>.Empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
// the given len does NOT include the zero terminator
|
|
||||||
var sp = new ReadOnlySpan<byte>(p, len + 1);
|
|
||||||
return FromSpan(sp);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static unsafe Utf8String FromIntPtr(IntPtr p)
|
|
||||||
{
|
|
||||||
return p == IntPtr.Zero ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator((byte*)p.ToPointer()));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// UTF16 String representation
|
|
||||||
/// Returns "NUL" on Null native type
|
|
||||||
/// </summary>
|
|
||||||
/// <returns></returns>
|
|
||||||
public override string ToString()
|
|
||||||
{
|
|
||||||
if (sp.Length == 0)
|
|
||||||
{
|
|
||||||
// object.ToString() should never thrown
|
|
||||||
// throw new NullReferenceException();
|
|
||||||
return "NUL";
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe
|
|
||||||
{
|
|
||||||
fixed (byte* q = sp)
|
|
||||||
{
|
{
|
||||||
return Encoding.UTF8.GetString(q, sp.Length - 1);
|
throw new ArgumentException("zero terminator required");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Utf8String(span);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Utf8String FromString(string s)
|
||||||
|
{
|
||||||
|
if (s == null)
|
||||||
|
{
|
||||||
|
return new Utf8String(ReadOnlySpan<byte>.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Utf8String(s.ToUtf8String());
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsafe long GetLength(byte* p)
|
||||||
|
{
|
||||||
|
var q = p;
|
||||||
|
while (*q != 0)
|
||||||
|
{
|
||||||
|
q++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return q - p;
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsafe ReadOnlySpan<byte> FindZeroTerminator(byte* p)
|
||||||
|
{
|
||||||
|
var len = (int)GetLength(p);
|
||||||
|
return new ReadOnlySpan<byte>(p, len + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
||||||
|
/// name="p"/>. The pointer must either be <see langword="null"/> or point to a valid <see
|
||||||
|
/// cref="Encoding.UTF8"/> encoded block of memory that terminates with a <c>\0</c> byte.
|
||||||
|
/// </summary>
|
||||||
|
public static unsafe Utf8String FromPtr(byte* p)
|
||||||
|
{
|
||||||
|
return p == null ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator(p));
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO maybe remove this and just use FromSpan?
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="Utf8String"/> which directly points at the memory pointed to by <paramref
|
||||||
|
/// name="p"/> with length <paramref name="len"/>. The pointer must be to a valid <see cref="Encoding.UTF8"/>
|
||||||
|
/// encoded block of memory that terminates with a <c>\0</c> byte. The <paramref name="len"/> value refers to
|
||||||
|
/// the number of bytes in the utf8 encoded value <em>not</em> including the <c>\0</c> byte terminator.
|
||||||
|
/// <para/>
|
||||||
|
/// <paramref name="p"/> can be <see langword="null"/>, in which case <paramref name="len"/> is ignored
|
||||||
|
/// and a new <see cref="Utf8String"/> instance is created that represents <see langword="null"/>. Note that this
|
||||||
|
/// different from a pointer to a single <c>\0</c> byte and a length of one. That would represent an empty <see
|
||||||
|
/// cref="Utf8String"/> string.
|
||||||
|
/// </summary>
|
||||||
|
/// <exception cref="ArgumentException">
|
||||||
|
/// Thrown if <paramref name="p"/> is not <see langword="null"/> and <c>p[len]</c> is not <c>\0</c>.
|
||||||
|
/// </exception>
|
||||||
|
public static unsafe Utf8String FromPtrLen(byte* p, int len)
|
||||||
|
{
|
||||||
|
if (p == null)
|
||||||
|
{
|
||||||
|
return new Utf8String(ReadOnlySpan<byte>.Empty);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the given len does NOT include the zero terminator
|
||||||
|
var sp = new ReadOnlySpan<byte>(p, len + 1);
|
||||||
|
return FromSpan(sp);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static unsafe Utf8String FromIntPtr(IntPtr p)
|
||||||
|
{
|
||||||
|
return p == IntPtr.Zero ? new Utf8String(ReadOnlySpan<byte>.Empty) : new Utf8String(FindZeroTerminator((byte*)p.ToPointer()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// UTF16 String representation
|
||||||
|
/// Returns "NUL" on Null native type
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
if (sp.Length == 0)
|
||||||
|
{
|
||||||
|
// object.ToString() should never thrown
|
||||||
|
// throw new NullReferenceException();
|
||||||
|
return "NUL";
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe
|
||||||
|
{
|
||||||
|
fixed (byte* q = sp)
|
||||||
|
{
|
||||||
|
return Encoding.UTF8.GetString(q, sp.Length - 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the <see cref="Encoding.UTF8"/> encoded bytes for the provided <paramref name="value"/>. The array
|
/// Gets the <see cref="Encoding.UTF8"/> encoded bytes for the provided <paramref name="value"/>. The array
|
||||||
/// will include a trailing <c>\0</c> character. The length of the array will <see cref="Encoding.UTF8"/>'s
|
/// will include a trailing <c>\0</c> character. The length of the array will <see cref="Encoding.UTF8"/>'s
|
||||||
/// <see cref="Encoding.GetByteCount(string)"/><c>+1</c> (for the trailing <c>\0</c> byte). These bytes are
|
/// <see cref="Encoding.GetByteCount(string)"/><c>+1</c> (for the trailing <c>\0</c> byte). These bytes are
|
||||||
/// suitable to use with <see cref="FromSpan"/> using the extension <see
|
/// suitable to use with <see cref="FromSpan"/> using the extension <see
|
||||||
/// cref="MemoryExtensions.AsSpan{T}(T[])"/> or <see cref="FromPtr(byte*)"/> or <see cref="FromPtrLen(byte*,
|
/// cref="MemoryExtensions.AsSpan{T}(T[])"/> or <see cref="FromPtr(byte*)"/> or <see cref="FromPtrLen(byte*,
|
||||||
/// int)"/>. Note that for <see cref="FromPtrLen(byte*, int)"/> the length provided should not include the
|
/// int)"/>. Note that for <see cref="FromPtrLen(byte*, int)"/> the length provided should not include the
|
||||||
/// trailing <c>\0</c> terminator.
|
/// trailing <c>\0</c> terminator.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static byte[] GetZeroTerminatedUTF8Bytes(string value)
|
public static byte[] GetZeroTerminatedUTF8Bytes(string value)
|
||||||
{
|
|
||||||
return value.ToUtf8String();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static implicit operator Utf8String(string s)
|
|
||||||
{
|
|
||||||
return FromString(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class Utf8StringUtils
|
|
||||||
{
|
|
||||||
public static byte[] ToUtf8String(this string sourceText)
|
|
||||||
{
|
|
||||||
if (sourceText == null)
|
|
||||||
{
|
{
|
||||||
return null;
|
return value.ToUtf8String();
|
||||||
}
|
}
|
||||||
|
|
||||||
var length = Encoding.UTF8.GetByteCount(sourceText);
|
public static implicit operator Utf8String(string s)
|
||||||
|
{
|
||||||
var byteArray = new byte[length + 1];
|
return FromString(s);
|
||||||
var wrote = Encoding.UTF8.GetBytes(sourceText, 0, sourceText.Length, byteArray, 0);
|
}
|
||||||
byteArray[wrote] = 0;
|
|
||||||
|
|
||||||
return byteArray;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public static class Utf8StringUtils
|
||||||
|
{
|
||||||
|
public static byte[] ToUtf8String(this string sourceText)
|
||||||
|
{
|
||||||
|
if (sourceText == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var length = Encoding.UTF8.GetByteCount(sourceText);
|
||||||
|
|
||||||
|
var byteArray = new byte[length + 1];
|
||||||
|
var wrote = Encoding.UTF8.GetBytes(sourceText, 0, sourceText.Length, byteArray, 0);
|
||||||
|
byteArray[wrote] = 0;
|
||||||
|
|
||||||
|
return byteArray;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user