1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 04:22:20 +02:00
This commit is contained in:
larkliy 2025-04-06 14:53:27 +03:00 committed by GitHub
commit 15d66a9e93
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 86 additions and 0 deletions

View file

@ -3025,6 +3025,32 @@ namespace System
return sIdStringLiterals[id];
}
public void Reverse() {
if (mLength < 1)
return;
int start = 0, end = mLength - 1;
while (start < end) {
(Ptr[start], Ptr[end]) = (Ptr[end], Ptr[start]);
start++;
end--;
}
int charStartByteIndex = 0;
for (var currentByteIndex = 0; currentByteIndex < mLength - 1; currentByteIndex++) {
if ((Ptr[currentByteIndex] & (.)0xC0) != (.)0x80) {
int i = charStartByteIndex, j = currentByteIndex;
while (i < j) {
(Ptr[i], mPtrOrBuffer[j]) = (Ptr[j], Ptr[i]);
i++;
j--;
}
charStartByteIndex = currentByteIndex + 1;
}
}
}
public struct RawEnumerator : IRefEnumerator<char8*>, IEnumerator<char8>
{
char8* mPtr;

60
tatus Normal file
View file

@ -0,0 +1,60 @@
diff --git a/BeefLibs/corlib/src/String.bf b/BeefLibs/corlib/src/String.bf
index 088cfa81..92f9e7fb 100644
--- a/BeefLibs/corlib/src/String.bf
+++ b/BeefLibs/corlib/src/String.bf
@@ -3025,6 +3025,55 @@ namespace System
return sIdStringLiterals[id];
}

+ public void Reverse(String strBuffer)
+ {
+  int_strsize originalLength = this.mLength;
+  char8* originalPtr = this.Ptr;
+
+  if (originalLength <= 1)
+  {
+  strBuffer.Set(.(originalPtr, originalLength));
+  return;
+  }
+
+  char8* tempBuffer = new:this char8[originalLength]* (?);
+  defer delete tempBuffer;
+
+  int_strsize writeIdx = 0;
+  int_strsize readPos = originalLength;
+
+  while (readPos > 0) {
+  int_strsize currentReadIdx = readPos - 1;
+  int_strsize startIdx = currentReadIdx;
+  char8 c = originalPtr[startIdx];
+  int8 len = 1;
+
+  if (c >= (.)0x80) {
+  while (startIdx > 0 && ((uint8)originalPtr[startIdx] & 0xC0) == 0x80)
+  startIdx--;
+
+  var (decodedC32, decodedLen) = UTF8.Decode(originalPtr + startIdx, originalLength - startIdx);
+
+  if (decodedLen <= 0) {
+  len = 1;
+  startIdx = currentReadIdx;
+  } else {
+  len = decodedLen;
+  }
+  }
+  Internal.MemCpy(tempBuffer + writeIdx, originalPtr + startIdx, len);
+
+  writeIdx += len;
+  readPos = startIdx;
+  }
+
+  Runtime.Assert(writeIdx == originalLength);
+
+  strBuffer.Clear();
+  strBuffer.Reserve(originalLength);
+  strBuffer.Append(tempBuffer, originalLength);
+ }
+
public struct RawEnumerator : IRefEnumerator<char8*>, IEnumerator<char8>
{
char8* mPtr;