[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any easy way to change a string ? and a bug (or maybe a feature ?)



On 10/14/24 11:17, Benoît Minisini wrote:
Le 14/10/2024 à 19:06, Brian G a écrit :
--
I am pretty sure Ben would not like this but....

You are right. I hardly could imagine something more horrible. :-)

When I am using a string as a buffer I use the following function to modify it and resend it, It works with &hff as well

_________________
' Gambas module file

Extern memcpy(src As String, dest As String, len As Integer) As Pointer In "libc:6"

'' Position is 0  relative not 1, value may be numeric or a single character string
Public Sub setChar(var As String, value As Variant, position As Integer)

     Dim myValue As Byte
     Dim myPtr As Pointer

     If position < 0 Or position >= var.len Then
         Error.Raise(("Specified position out of bounds"))
     Endif
     If TypeOf(value) = gb.string Then
         myValue = CByte(Asc(value[0]))
     Else
         myValue = CByte(value)
     Endif
     myPtr = memcpy(var, var, 0)
     Byte@(myPtr + position) = myValue

End

Public Sub Main()

     Dim a As String = "hello world"

     Print a
     setChar(a, "g", 1)
     Print a
     setChar(a, &hff, 4)
     Print a

End
--------------- Output is
hello world
hgllo world
hgll? world
--------------- end output

I hope this helps and not confuses... works for me as needed

~~~~ Brian


Strings are immutable. If you modify them by circumventing the interpreter, it can lead to any crash at any moment.

If you need a byte buffer as as string, use OPEN STRING or Byte[].

Regards,

Open string does not allow to seek and write to buffer position it always appends. so actually useless for buffer access/management.

But yes bad form to try to speed up message transfer by modifying an immutable string value.

Perhaps allowing [] to write to a position would be possible. As now we can read a value using [] from a string.

--
~~~~ Brian

Attachment: OpenPGP_0x78BFB26402F48419.asc
Description: OpenPGP public key

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature