sharp-chat/SharpChatCommon/StringExtensions.cs
flashwave 5a7756894b
First bits of the Context overhaul.
Reintroduces separate contexts for users, channels, connections (now split into sessions and connections) and user-channel associations.
It builds which is as much assurance as I can give about the stability of this commit, but its also the bare minimum of what i like to commit sooooo
A lot of things still need to be broadcast through events throughout the application in order to keep states consistent but we'll cross that bridge when we get to it.
I really need to stop using that phrase thingy, I'm overusing it.
2025-05-03 02:49:51 +00:00

53 lines
1.9 KiB
C#

using System.Globalization;
using System.Text;
namespace SharpChat;
public static class StringExtensions {
public static byte[] GetUtf8Bytes(this string str) {
return Encoding.UTF8.GetBytes(str);
}
public static byte[] GetUtf8Bytes(this string str, int index, int count) {
return Encoding.UTF8.GetBytes(str, index, count);
}
public static int CountUtf8Bytes(this string str) {
return Encoding.UTF8.GetByteCount(str);
}
public static int CountUnicodeGraphemes(this string str) {
return new StringInfo(str).LengthInTextElements;
}
public static bool SlowUtf8Equals(this string str, string other) {
return str.GetUtf8Bytes().SlowEquals(other.GetUtf8Bytes());
}
public static string TruncateIfTooLong(this string str, int? maxGraphemes = null, int? maxBytes = null) {
StringInfo info = new(str);
if(maxGraphemes.HasValue) {
if(maxGraphemes.Value == 0)
return string.Empty;
if(maxGraphemes.Value < 0)
throw new ArgumentException("Maximum Unicode Grapheme Cluster count must be a positive integer.", nameof(maxGraphemes));
if(info.LengthInTextElements > maxGraphemes.Value)
return info.SubstringByTextElements(0, maxGraphemes.Value);
}
if(maxBytes.HasValue) {
if(maxBytes.Value == 0)
return string.Empty;
if(maxBytes.Value < 0)
throw new ArgumentException("Maximum bytes must be a positive integer.", nameof(maxBytes));
if(str.CountUtf8Bytes() > maxBytes.Value)
return maxGraphemes.HasValue
? info.SubstringByTextElements(0, Math.Min(info.LengthInTextElements, maxGraphemes.Value))
: str.GetUtf8Bytes(0, maxBytes.Value).GetUtf8String();
}
return str;
}
}