Skip to content

Commit 932b9cb

Browse files
committed
Fixed splitting string by byte size, improved toy string image formatting
1 parent 7f2821f commit 932b9cb

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

LabExtended/API/Images/Conversion/ToyStringImageConvertor.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using LabExtended.API.Toys;
2-
2+
using LabExtended.Extensions;
33
using NorthwoodLib.Pools;
44

55
using LabExtended.Utilities;
@@ -91,10 +91,22 @@ public static void ConvertImage(ImageFile file)
9191

9292
builder.AppendLine();
9393
}
94+
95+
frame.toyFrameData ??= new();
9496

9597
builder.Append("</color>");
98+
builder.ToString().SplitByLengthUtf8(MirrorMethods.MaxStringLength, frame.toyFrameData);
99+
100+
builder.Clear();
101+
102+
for (var x = 0; x < frame.toyFrameData.Count; x++)
103+
{
104+
builder.Append('{');
105+
builder.Append(x);
106+
builder.Append('}');
107+
}
96108

97-
TextToy.SplitStringNonAlloc(builder.ToString(), frame.toyFrameData, out frame.toyFrameFormat);
109+
frame.toyFrameFormat = builder.ToString();
98110
}
99111

100112
StringBuilderPool.Shared.Return(builder);

LabExtended/Extensions/StringExtensions.cs

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public static class StringExtensions
1010
{
1111
public const char LogAnsiColorEscapeChar = (char)27;
1212

13+
public static UTF8Encoding Utf8 { get; } = new(false, true);
14+
1315
public static readonly Regex NewLineRegex = new Regex("r\n|\r|\n", RegexOptions.Compiled);
1416

1517
public static readonly Regex PascalCaseRegex = new Regex("([a-z,0-9](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", RegexOptions.Compiled);
@@ -92,27 +94,67 @@ public static List<string> SplitByLength(this string str, int maxLength)
9294
{
9395
var list = new List<string>(Mathf.CeilToInt(str.Length / maxLength));
9496

95-
SplitByLengthNonAlloc(str, maxLength, list);
97+
SplitByLength(str, maxLength, list);
9698
return list;
9799
}
98100

99-
public static void SplitByLengthNonAlloc(this string str, int maxLength, ICollection<string> target)
101+
public static void SplitByLength(this string str, int maxLength, ICollection<string> target)
100102
{
101103
if (maxLength <= 0)
102104
throw new ArgumentOutOfRangeException(nameof(maxLength));
105+
106+
if (target is null)
107+
throw new ArgumentNullException(nameof(target));
108+
109+
while (str.Length > maxLength)
110+
{
111+
var otherStr = str.Substring(0, maxLength);
112+
113+
str = str.Remove(0, maxLength);
114+
115+
target.Add(otherStr);
116+
}
103117

104-
var builder = StringBuilderPool.Shared.Rent();
118+
target.Add(str);
119+
}
120+
121+
public static void SplitByLengthUtf8(this string str, int maxLength, ICollection<string> target)
122+
{
123+
if (maxLength <= 0)
124+
throw new ArgumentOutOfRangeException(nameof(maxLength));
125+
126+
if (target is null)
127+
throw new ArgumentNullException(nameof(target));
105128

106-
for (var i = 0; i < str.Length; i++)
129+
if (string.IsNullOrEmpty(str))
130+
return;
131+
132+
var utf8 = Encoding.UTF8;
133+
134+
var start = 0;
135+
var length = str.Length;
136+
137+
while (start < length)
107138
{
108-
builder.Append(str[i]);
139+
var end = start;
140+
var byteCount = 0;
109141

110-
if (builder.Length >= maxLength)
142+
while (end < length)
111143
{
112-
target.Add(builder.ToString());
113-
114-
builder.Clear();
144+
var charSize = utf8.GetByteCount(new char[] { str[end] });
145+
146+
if (byteCount + charSize > maxLength)
147+
break;
148+
149+
byteCount += charSize;
150+
end++;
115151
}
152+
153+
var chunk = str.Substring(start, end - start);
154+
155+
target.Add(chunk);
156+
157+
start = end;
116158
}
117159
}
118160

0 commit comments

Comments
 (0)