From 31e811d4d85fb68a4a194367c91ee452154be82f Mon Sep 17 00:00:00 2001 From: User Date: Fri, 4 Apr 2025 12:42:14 +0300 Subject: [PATCH 1/2] added string reverse method --- BeefLibs/corlib/src/String.bf | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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, IEnumerator { char8* mPtr; From cbb4e8b73853ae01183444529f062c72e76a5a79 Mon Sep 17 00:00:00 2001 From: User Date: Fri, 4 Apr 2025 17:17:37 +0300 Subject: [PATCH 2/2] Reverse Method upgraded --- BeefLibs/corlib/src/String.bf | 65 +++++++++++------------------------ tatus | 60 ++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 44 deletions(-) create mode 100644 tatus diff --git a/BeefLibs/corlib/src/String.bf b/BeefLibs/corlib/src/String.bf index 92f9e7fb..a02394d5 100644 --- a/BeefLibs/corlib/src/String.bf +++ b/BeefLibs/corlib/src/String.bf @@ -3025,53 +3025,30 @@ namespace System return sIdStringLiterals[id]; } - public void Reverse(String strBuffer) - { - int_strsize originalLength = this.mLength; - char8* originalPtr = this.Ptr; + public void Reverse() { + if (mLength < 1) + return; - if (originalLength <= 1) - { - strBuffer.Set(.(originalPtr, originalLength)); - return; - } + int start = 0, end = mLength - 1; - char8* tempBuffer = new:this char8[originalLength]* (?); - defer delete tempBuffer; + while (start < end) { + (Ptr[start], Ptr[end]) = (Ptr[end], Ptr[start]); + start++; + end--; + } - 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); + 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, IEnumerator diff --git a/tatus b/tatus new file mode 100644 index 00000000..51bb2f01 --- /dev/null +++ b/tatus @@ -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, IEnumerator + { + char8* mPtr;