feat: Adds cliloc support and fixes valuestringbuilder ctor (#1013)
- [X] Adds cliloc support using the following API:
```cs
public static class Localization
{
string GetText(int number);
string GetText(int number, string lang);
string Format(int number, params object[] args);
string Format(int number, string lang, params object[] args);
string Format(int number, $"{arg1}{arg2}{arg3}");
string Format(int number, lang, $"{arg1}{arg2}{arg3}");
bool TryGetLocalization(int number, out LocalizationEntry entry);
bool TryGetLocalization(int number, string lang, out LocalizationEntry entry);
}
public class LocalizationEntry
{
string Language { get; }
string Number { get; }
string Text { get; }
string?[] TextSlices { get; } // Used for string building
string StringFormatter { get; }
string Format(params object[] args);
string Format($"{arg1}{arg2}{arg3}");
}
```
- [X] Fixes a bug with ValueStringBuilder and default initialization size
- [X] Optimizes ValueStringBuilder to use `ISpanFormattable`
- [X] Adds `Append<T>(T value);` support ValueStringBuilder
- [X] Adds `Append($"");` string interpolation support to ValueStringBuider
This commit is contained in:
parent
790cb5d733
commit
be7da3a3dc
8 changed files with 1046 additions and 156 deletions
71
Projects/Server/Buffers/PooledArraySpanFormattable.cs
Normal file
71
Projects/Server/Buffers/PooledArraySpanFormattable.cs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2022 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: PooledArraySpanFormattable.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Buffers;
|
||||
|
||||
namespace Server.Buffers;
|
||||
|
||||
public struct PooledArraySpanFormattable : ISpanFormattable, IDisposable
|
||||
{
|
||||
private char[] _arrayToReturnToPool;
|
||||
private int _pos;
|
||||
|
||||
public PooledArraySpanFormattable(char[] arrayToReturnToPool, int length)
|
||||
{
|
||||
_arrayToReturnToPool = arrayToReturnToPool;
|
||||
_pos = length;
|
||||
}
|
||||
|
||||
public ReadOnlySpan<char> Chars => _arrayToReturnToPool.AsSpan(.._pos);
|
||||
|
||||
public static implicit operator string(PooledArraySpanFormattable f) => f.ToString();
|
||||
|
||||
public string ToString(string? format = null, IFormatProvider formatProvider = null)
|
||||
{
|
||||
var result = new string(_arrayToReturnToPool.AsSpan(0, _pos));
|
||||
Dispose();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public bool TryFormat(
|
||||
Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default,
|
||||
IFormatProvider provider = null
|
||||
)
|
||||
{
|
||||
if (destination.Length < _pos)
|
||||
{
|
||||
charsWritten = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
_arrayToReturnToPool.AsSpan(0, _pos).CopyTo(destination);
|
||||
Dispose();
|
||||
|
||||
charsWritten = _pos;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_arrayToReturnToPool != null)
|
||||
{
|
||||
ArrayPool<char>.Shared.Return(_arrayToReturnToPool);
|
||||
_arrayToReturnToPool = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue