iOS Question b4a map does not work in b4i

fastlingo

Member
Licensed User
Longtime User
I have some maps saved from b4a containing translations of strings
They start like this

#Wed Mar 05 14:47:17 GMT+00:00 2025
You\ have\ nothing\ to\ fear\ as\ long\ as\ I'm\ here.=Non hai niente da temere finch\u00E9 sono qui.

If I try to retrieve the value for the key "You have nothing to fear as long as I'm here", it works fine in b4a but it does not work in b4i. It only works if I look for "You\ have\..." with backslashes.
Is there a way to make this work in b4i?
 

fastlingo

Member
Licensed User
Longtime User
Is there a way to change those strings, from that format, to a format that can be displayed? (special characters etc). I cannot change the map files as there are hundreds of them, each with about 10,000 lines. They are already in use with my b4a apps, I am trying to make b4i versions.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This will read the saved map:
B4X:
Private Sub ReadMap(Dir As String, FileName As String) As Map
    Dim res As Map
    res.Initialize
    For Each Line As String In File.ReadList(Dir, FileName)
        Dim m As Matcher = Regex.Matcher("(?<!\\)=", Line)
        Log(Line)
        If m.Find Then
            Dim key As String = Line.SubString2(0, m.GetEnd(0) - 1)
            key = key.Replace("\=", "=").Replace("\ ", " ")
            Dim value As String = Line.SubString2(m.GetStart(0) + 1, Line.Length)
            value = value.Replace("\=", "=")
            res.Put(UnescapeUnicode(key), UnescapeUnicode(value))
        End If
    Next
    Return res
End Sub

Sub UnescapeUnicode(s As String) As String
    Dim sb As StringBuilder
    sb.Initialize
    Dim i As Int
    Do While i < s.Length
        Dim c As Char = s.CharAt(i)
        If c = "\" And i < s.Length - 1 And s.CharAt(i + 1) = "u" Then
            Dim unicode As StringBuilder
            unicode.Initialize
            i = i + 2
            Do While i < s.Length
                Dim cc As String = s.CharAt(i)
                Dim n As Int = Asc(cc.ToLowerCase)
                If (n >= Asc("0") And n <= Asc("9")) Or (n >= Asc("a") And n <= Asc("f")) Then
                    unicode.Append(s.CharAt(i))
                Else
                    i = i - 1
                    Exit
                End If
                i = i + 1
            Loop
            sb.Append(Chr(Bit.ParseInt(unicode.ToString, 16)))
        Else
            sb.Append(c)
        End If
        i = i + 1
    Loop
    Return sb.ToString
End Sub
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Is there a way to change those strings, from that format, to a format that can be displayed? (special characters etc). I cannot change the map files as there are hundreds of them, each with about 10,000 lines. They are already in use with my b4a apps, I am trying to make b4i versions.

In order to avoid a lot of effort use this:


with this class provided by @Erel :

 
Upvote 0

fastlingo

Member
Licensed User
Longtime User
Thanks Erel. The sub UnescapeUnicode works but if what immediately follows after an unicode character is a letter in the range "a...f", it will add it to the number describing the unicode character. I modified the sub so that it always takes into account only four digits after \u, now it shows the characters correctly

B4X:
Sub UnescapeUnicode(s As String) As String
    Dim sb As StringBuilder
    sb.Initialize
    Dim i As Int
    Do While i < s.Length
        Dim c As Char = s.CharAt(i)
        If c = "\" And i < s.Length - 1 And s.CharAt(i + 1) = "u" Then
            Dim unicode As String
            i = i + 2
            unicode=s.SubString2(i,i+4)
            i=i+3
            sb.Append(Chr(Bit.ParseInt(unicode, 16)))
        Else
            sb.Append(c)
        End If
        i = i + 1
    Loop
    Return sb.ToString
End Sub
 
Upvote 0
Top