12-26-2025, 10:41 AM
@ahenry3068
I really like the _mem approach, so I decided to tackle the ridiculous limit on strings. I’ve modified the program to support dynamic strings since the length is known, even if it varies. It seems to work fine. For now, at least...
I really like the _mem approach, so I decided to tackle the ridiculous limit on strings. I’ve modified the program to support dynamic strings since the length is known, even if it varies. It seems to work fine. For now, at least...
Code: (Select All)
Dim S As String
S = "ABCDEF "
Print S
Print
SwapChars S$, 2, 3 'ACBDEF
Print S
SwapChars S$, 3, 5 'ACEDBF'
Print S
End
Sub SwapChars (s As String, idx1 As Long, idx2 As Long)
Dim L As Long: L = Len(s)
If idx1 < 1 Or idx2 < 1 Then Exit Sub
If idx1 > L Or idx2 > L Then Exit Sub
If idx1 = idx2 Then Exit Sub
Dim m As _MEM
m = _MemNew(L)
' copy string data to own buffer
_MemPut m, m.OFFSET, s
Dim b1 As _Unsigned _Byte, b2 As _Unsigned _Byte
_MemGet m, m.OFFSET + (idx1 - 1), b1
_MemGet m, m.OFFSET + (idx2 - 1), b2
_MemPut m, m.OFFSET + (idx1 - 1), b2
_MemPut m, m.OFFSET + (idx2 - 1), b1
' construct new string
s = Space$(L)
Dim i As Long, bt As _Unsigned _Byte
For i = 0 To L - 1
_MemGet m, m.OFFSET + i, bt
Mid$(s, i + 1, 1) = Chr$(bt)
Next
_MemFree m
End Sub

