Skip to content

Commit 988d01d

Browse files
committed
Removed useless field in Utf8String
1 parent 302ce08 commit 988d01d

File tree

2 files changed

+25
-38
lines changed

2 files changed

+25
-38
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace MCServerSharp
5+
{
6+
public static class EncodingExtensions
7+
{
8+
public static byte[] GetBytes(this Encoding encoding, ReadOnlySpan<char> text)
9+
{
10+
byte[] buffer = new byte[encoding.GetByteCount(text)];
11+
_ = encoding.GetBytes(text, buffer);
12+
return buffer;
13+
}
14+
}
15+
}

MCServerSharp.Base/Text/Utf8String/Utf8String.cs

+10-38
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ namespace MCServerSharp
1818
[SkipLocalsInit]
1919
public partial class Utf8String : IComparable<Utf8String>, IEquatable<Utf8String>, ILongHashable
2020
{
21-
public static Utf8String Empty { get; } = new Utf8String(Array.Empty<byte>());
21+
public static Utf8String Empty { get; } = new Utf8String(ReadOnlyMemory<byte>.Empty);
2222

23-
private byte[]? _byteArray;
2423
private ReadOnlyMemory<byte> _bytes;
2524

2625
public ReadOnlySpan<byte> Bytes => _bytes.Span;
@@ -30,39 +29,26 @@ public partial class Utf8String : IComparable<Utf8String>, IEquatable<Utf8String
3029

3130
#region Constructors
3231

33-
private Utf8String(byte[] bytes)
34-
{
35-
_byteArray = bytes;
36-
_bytes = _byteArray.AsMemory();
37-
}
38-
3932
private Utf8String(ReadOnlyMemory<byte> bytes)
4033
{
4134
_bytes = bytes;
4235
}
4336

44-
private Utf8String(int length) : this(length == 0 ? Array.Empty<byte>() : new byte[length])
45-
{
46-
}
47-
48-
public Utf8String(string value) : this(StringHelper.Utf8.GetByteCount(value))
37+
public Utf8String(string value) : this(StringHelper.Utf8.GetBytes(value).AsMemory())
4938
{
50-
StringHelper.Utf8.GetBytes(value, _byteArray);
5139
}
5240

53-
public Utf8String(ReadOnlySpan<char> chars) : this(StringHelper.Utf8.GetByteCount(chars))
41+
public Utf8String(ReadOnlySpan<char> utf16) : this(StringHelper.Utf8.GetBytes(utf16).AsMemory())
5442
{
55-
StringHelper.Utf8.GetBytes(chars, _byteArray);
5643
}
5744

58-
public Utf8String(ReadOnlySpan<byte> bytes) : this(bytes.Length)
45+
public Utf8String(ReadOnlySpan<byte> utf8) : this(utf8.ToArray().AsMemory())
5946
{
60-
bytes.CopyTo(_byteArray);
6147
}
6248

6349
#endregion
6450

65-
public static Utf8String UnsafeWrap(ReadOnlyMemory<byte> data)
51+
public static Utf8String WrapUnsafe(ReadOnlyMemory<byte> data)
6652
{
6753
return new Utf8String(data);
6854
}
@@ -74,28 +60,13 @@ public static Utf8String Create(ReadOnlySpan<byte> utf8)
7460
return new Utf8String(utf8);
7561
}
7662

77-
public static Utf8String Create(ReadOnlyMemory<byte> utf8)
78-
{
79-
return Create(utf8.Span);
80-
}
81-
82-
public static Utf8String Create(Utf8Memory utf8)
83-
{
84-
return Create(utf8.Span);
85-
}
86-
8763
public static Utf8String Create(ReadOnlySpan<char> utf16)
8864
{
8965
if (utf16.IsEmpty)
9066
return Empty;
9167
return new Utf8String(utf16);
9268
}
9369

94-
public static Utf8String Create(ReadOnlyMemory<char> utf16)
95-
{
96-
return Create(utf16.Span);
97-
}
98-
9970
public static Utf8String Create<TState>(
10071
int length, TState state, SpanAction<byte, TState> action)
10172
{
@@ -105,8 +76,9 @@ public static Utf8String Create<TState>(
10576
if (length == 0)
10677
return Empty;
10778

108-
var str = new Utf8String(length);
109-
action.Invoke(str._byteArray, state);
79+
byte[] buffer = new byte[length];
80+
var str = new Utf8String(buffer.AsMemory());
81+
action.Invoke(buffer, state);
11082
return str;
11183
}
11284

@@ -244,9 +216,9 @@ public static Utf8String Concat(ReadOnlySpan<byte> value1, ReadOnlySpan<byte> va
244216
dst = dst[value2.Length..];
245217

246218
value3.CopyTo(dst);
247-
dst = dst[value3.Length..];
219+
//dst = dst[value3.Length..];
248220

249-
return new Utf8String(bytes);
221+
return new Utf8String(bytes.AsMemory());
250222
}
251223

252224
public static Utf8String Concat(Utf8String? value1, Utf8String? value2, Utf8String? value3)

0 commit comments

Comments
 (0)