B4J Question Byte vs String - who wins?

amorosik

Expert
Licensed User
I can't quite understand the difference between routine 1 and routine 2
Both search and replace a substring within a file
One acts by working on bytes, the other transforms into a string and uses Replace
Are they usable with the same effectiveness?
What are the pros and cons of both?

B4X:
' ROUTINE 1
Dim bb As B4XBytesBuilder
bb.Initialize
bb.Append(File.ReadBytes("", nome_file_completo))
SingleReplace(bb, data_da_cercare.GetBytes("ascii"), data_da_impostare_stringa.GetBytes("ascii"))
File.WriteBytes("", nome_file_destinazione, bb.ToArray)

' ROUTINE 2
Dim st As String
Dim by() As Byte= File.ReadBytes("",nome_file_completo)
st=BytesToString(by,0,by.Length,"ASCII")
st=st.Replace(data_da_cercare,data_da_impostare_stringa)
by= st.GetBytes("ASCII")
File.WriteBytes("",nome_file_destinazione,by)

Private Sub SingleReplace(bb As B4XBytesBuilder, SearchFor() As Byte, ReplaceWith() As Byte)
    Dim i As Int = bb.IndexOf(SearchFor)
    If i > -1 Then
        bb.Remove(i, i + SearchFor.Length)
        bb.Insert(i, ReplaceWith)
        End If
    End Sub
 

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Arrays are mutable while Strings are not. When dealing with a single replace as you are doing, there is no advantage of one form the other as time difference is neglible.

if you begin doing several replacements (say 1000) you will begin noticing a slower performance on the string, this is because the string being inmutable has to create a new object on each replacement. While arrays dont, they just modify the pointer of the value based on index.

This is based on Arrays of bytes vs strings, i wouldnt know if B4xBytesBuilder makes it different. for what i can see on the SingleReplace, it first translates the string to bytes, then it would be the same.
 
Upvote 0

amorosik

Expert
Licensed User
What is the file content? ASCII string? If not then both routines are wrong.

Also both routines are not memory efficient (only relevant if you are trying to replace in very large files).

The file content can be any character
Why do you say both are wrong?
Should routine1, working with bytes, not work correctly with any contents of the file?
And if these are wrong, can you suggest the correct solution for files with any content?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
ASCII encoding cannot represent all characters. Non-ascii characters will be lost. Change it to UTF8.

Note that the code is more complex than it needs to be.
Just use File.ReadString.

With that said, if the content is not a valid string then the code in routine 2 is completely wrong.
 
Upvote 0

amorosik

Expert
Licensed User

Like this?

B4X:
' ROUTINE 1
Dim bb As B4XBytesBuilder
bb.Initialize
bb.Append(File.ReadString("", nome_file_completo))
SingleReplace(bb, data_da_cercare.GetBytes("UTF8"), data_da_impostare_stringa.GetBytes("UTF8"))
File.WriteBytes("", nome_file_destinazione, bb.ToArray)

Private Sub SingleReplace(bb As B4XBytesBuilder, SearchFor() As Byte, ReplaceWith() As Byte)
    Dim i As Int = bb.IndexOf(SearchFor)
    If i > -1 Then
        bb.Remove(i, i + SearchFor.Length)
        bb.Insert(i, ReplaceWith)
        End If
    End Sub
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…