Android Question BBCodeView Printout the Text Backword

Addo

Well-Known Member
Licensed User
following this thread


I am using String builder along with regex to create a string and replace some regex matches with new string
i know that i can simply use string.replace but that is not the case that i aim for

simply i have a string just looks like that

" hello -) how is everything -) "
i have set the makepattern function to use string builder to remove each -) character and create an image string and replace the targeted character with it

i have used substring to remove the matched character in the regex pattern and then i have added the new image string inside the stringbuilder

but the output comes backword "-) everything is how -) hello"
what i am doing wrong ?

B4X:
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As String
    Dim sb As StringBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    sb.Initialize
   
    Do While m.Find
        Dim currentStart As Int = m.GetStart(GroupNumber)
        Dim SmileCode As String
        Dim SmileCodeToimage As String
        SmileCode = m.Group(GroupNumber)
        SmileCodeToimage = CreateSmileyImage(0, 0, DipToCurrent(30), DipToCurrent(30), SmileCode&".gif")
        sb.Append(Input.SubString2(lastMatchEnd, currentStart) & SmileCodeToimage)
        lastMatchEnd = m.GetEnd(GroupNumber)

    Loop
    If lastMatchEnd < Input.Length Then
        sb.Append(Input.SubString(lastMatchEnd))
    End If
    Return sb.ToString
End Sub
 
Last edited:

emexes

Expert
Licensed User
simply i have a string just looks like that

" hello -) how is everything -) "

but the output comes backword "-) everything is how -)"

I don't have time to look at this right now, but at first glance:

what happened to the "hello" in the source string?

It doesn't look like the output is backwards, it looks more like it has just lopped off the leading "hello".
 
Upvote 0

Addo

Well-Known Member
Licensed User
I don't have time to look at this right now, but at first glance:

what happened to the "hello" in the source string?

It doesn't look like the output is backwards, it looks more like it has just lopped off the leading "hello".
It was a mistyped since i wrote the results forget to include it, the thing is the whole string printed backwards
 
Upvote 0

emexes

Expert
Licensed User
What value does GroupNumber have? And is it always the same value for each iteration of the Do While m.find loop?

1662177280732.png


i know that i can simply use string.replace but that is not the case that i aim for

Why not? It feels like it would do the job here, if CreateSmilyImage is indeed returning the image as a String. 🤔
 
Upvote 0

Addo

Well-Known Member
Licensed User
The full function were mentioned here


And yes the createsmile function should return a string that will be added later in BBCodeView

Like mentioned here


The string builder should remove the smile operator and place the generated SMM String but i have no idea why the final results printed out backwards
 
Upvote 0

emexes

Expert
Licensed User
The string builder should remove the smile operator and place the generated SMM String but i have no idea why the final results printed out backwards

In the words that programmers love: it seems to be working ok here 🤔

Perhaps it's a northern-hemisphere vs southern-hemisphere thing, similar to: which way does water spin into drain hole.

Or... are you using a right-to-left language? Unicode does some interesting stuff in those cases, especially if the text also includes left-to-right numbers.

B4X:
Sub AppStart (Args() As String)
 
    Dim SmileyPattern as string = "\-\)"  'is actually "-)" after escaping non-alphanumeric regex characters
 
    Dim TestInput As String = " hello -) how is everything -) "
    Dim TestOutput As String = MarkPattern(TestInput, SmileyPattern, 0)
 
    Log("Input string = """ & TestInput & """")
    Log("Output string = """ & TestOutput & """")

End Sub
 
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As String
    Dim sb As StringBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    sb.Initialize
 
    Do While m.Find
        Dim currentStart As Int = m.GetStart(GroupNumber)
        Dim SmileCode As String
        Dim SmileCodeToimage As String
        SmileCode = m.Group(GroupNumber)
        SmileCodeToimage = "[[SMILEY CODE]]"    '''CreateSmileyImage(0, 0, DipToCurrent(30), DipToCurrent(30), SmileCode&".gif")
        sb.Append(Input.SubString2(lastMatchEnd, currentStart) & SmileCodeToimage)
        lastMatchEnd = m.GetEnd(GroupNumber)
    Loop
    If lastMatchEnd < Input.Length Then
        sb.Append(Input.SubString(lastMatchEnd))
    End If
    Return sb.ToString
End Sub

Log file output:
Waiting for debugger to connect...
Program started.
Input string = " hello -) how is everything -) "
Output string = " hello [[SMILEY CODE]] how is everything [[SMILEY CODE]] "

ps congrats on your code retaining the leading and trailing spaces; many first-draft string de/re-construction algorithms would have lost one or both of them 👍
 
Last edited:
Upvote 0

Addo

Well-Known Member
Licensed User
In the words that programmers love: it seems to be working ok here 🤔

Perhaps it's a northern-hemisphere vs southern-hemisphere thing, similar to: which way does water spin into drain hole.

Or... are you using a right-to-left language? Unicode does some interesting stuff in those cases, especially if the text also includes left-to-right numbers.

B4X:
Sub AppStart (Args() As String)
 
    Dim SmileyPattern as string = "\-\)"  'is actually "-)" after escaping non-alphanumeric regex characters
 
    Dim TestInput As String = " hello -) how is everything -) "
    Dim TestOutput As String = MarkPattern(TestInput, SmileyPattern, 0)
 
    Log("Input string = """ & TestInput & """")
    Log("Output string = """ & TestOutput & """")

End Sub
 
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As String
    Dim sb As StringBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    sb.Initialize
 
    Do While m.Find
        Dim currentStart As Int = m.GetStart(GroupNumber)
        Dim SmileCode As String
        Dim SmileCodeToimage As String
        SmileCode = m.Group(GroupNumber)
        SmileCodeToimage = "[[SMILEY CODE]]"    '''CreateSmileyImage(0, 0, DipToCurrent(30), DipToCurrent(30), SmileCode&".gif")
        sb.Append(Input.SubString2(lastMatchEnd, currentStart) & SmileCodeToimage)
        lastMatchEnd = m.GetEnd(GroupNumber)
    Loop
    If lastMatchEnd < Input.Length Then
        sb.Append(Input.SubString(lastMatchEnd))
    End If
    Return sb.ToString
End Sub

Log file output:
Waiting for debugger to connect...
Program started.
Input string = " hello -) how is everything -) "
Output string = " hello [[SMILEY CODE]] how is everything [[SMILEY CODE]] "

ps congrats on your code retaining the leading and trailing spaces; many first-draft string de/re-construction algorithms would have lost one or both of them 👍
You are correct when BBCodeView Right to Left Property is set to true the text printed out backward thats very weird the behavior shouldn't happened.
but the more weird is when i dont add SmileCodeToimage into sb.Append(Input.SubString2(lastMatchEnd, currentStart))
the text printed out in BBcodeview Correctly .. i really don't understand why such behavior caused
 
Upvote 0

Addo

Well-Known Member
Licensed User
as example this printed out the output correctly even with RTL Property is set to true but it will only substring the smileCode from the input string and will not add the image string .
B4X:
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As String
    Dim sb As StringBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    sb.Initialize
    
    Do While m.Find
        Dim currentStart As Int = m.GetStart(GroupNumber)
        Dim SmileCode As String
        Dim SmileCodeToimage As String
        SmileCode = m.Group(GroupNumber)
        SmileCodeToimage = SetSmiley(0, 0, DipToCurrent(30), DipToCurrent(30),SmileCode&".gif")
        sb.Append(Input.SubString2(lastMatchEnd, currentStart)) ' this will shows normally in BBCodeview
        lastMatchEnd = m.GetEnd(GroupNumber)

    Loop
    If lastMatchEnd < Input.Length Then
        sb.Append(Input.SubString(lastMatchEnd))
    End If
    Return sb.ToString
End Sub


but this will add the image instead of the SmileCode but it will be showing backwarded in BBcodeView which i dont know why this has been caused and how to fix it

B4X:
Sub MarkPattern(Input As String, Pattern As String, GroupNumber As Int) As String
    Dim sb As StringBuilder
    Dim lastMatchEnd As Int = 0
    Dim m As Matcher = Regex.Matcher(Pattern, Input)
    sb.Initialize
    
    Do While m.Find
        Dim currentStart As Int = m.GetStart(GroupNumber)
        Dim SmileCode As String
        Dim SmileCodeToimage As String
        SmileCode = m.Group(GroupNumber)
        SmileCodeToimage = SetSmiley(0, 0, DipToCurrent(30), DipToCurrent(30),SmileCode&".gif")
        sb.Append(Input.SubString2(lastMatchEnd, currentStart) & SmileCodeToimage)  ' substring the smilecode and add SmileCodeToimage
        lastMatchEnd = m.GetEnd(GroupNumber)

    Loop
    If lastMatchEnd < Input.Length Then
        sb.Append(Input.SubString(lastMatchEnd))
    End If
    Return sb.ToString
End Sub
 
Upvote 0

emexes

Expert
Licensed User
simply i have a string just looks like that
" hello -) how is everything -) "

but the output comes backword "-) everything is how -) hello"
but this will add the image instead of the SmileCode but it will be showing backwarded in BBcodeView which i dont know why this has been caused and how to fix it

Lol I am so confused now, but perhaps I misunderstood the problem.

Which of these is backward? :
- the string returned by MarkPattern()
- the on-screen display of that string by BBcodeView
- the embedded smiley image(s) eg is the gif :-> but the BBcodeView display of that gif <-: ?

The log file output of post #6 indicates that MarkPattern() is not causing the backwardness (with two smileys, at least) :
Log file output:
Waiting for debugger to connect...
Program started.
Input string = " hello -) how is everything -) "
Output string = " hello [[SMILEY CODE]] how is everything [[SMILEY CODE]] "
 
Upvote 0

Addo

Well-Known Member
Licensed User
Lol I am so confused now, but perhaps I misunderstood the problem.

Which of these is backward? :
- the string returned by MarkPattern()
- the on-screen display of that string by BBcodeView

The log file output of post #6 indicates that MarkPattern() is not causing the backwardness (with two smileys, at least) :
Log file output:
Waiting for debugger to connect...
Program started.
Input string = " hello -) how is everything -) "
Output string = " hello [[SMILEY CODE]] how is everything [[SMILEY CODE]] "
The BBCodeView with the property of RTL enabled. In the beginning i was doubt that iam doing something wrong in the makepattern function. But at the end its BBCodeView related thing if RTL property enabled which something not related to the whole code
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
The BBCodeView with the property of RTL enabled

Interesting. Are you actually working with a RTL language eg Arabic, Hebrew, Kurdish ?

I remember some strange goings-on a couple of years back to do with Hebrew language and... I think it was zero-width-joiners being used to superimpose some characters to make up a currency symbol. Hang on a min while I test out the forum search feature. :cool:
 
Upvote 0

Addo

Well-Known Member
Licensed User
Interesting. Are you actually working with a RTL language eg Arabic, Hebrew, Kurdish ?

I remember some strange goings-on a couple of years back to do with Hebrew language and... I think it was zero-width-joiners being used to superimpose some characters to make up a currency symbol. Hang on a min while I test out the forum search feature. :cool:
I have searched about BBCodeView RTL and found only one post that asks for RTL feature. I will use BBCodeView to view text messages and for sure i had to enable RTL for multilingual support
 
Upvote 0

emexes

Expert
Licensed User
I remember some strange goings-on a couple of years back to do with Hebrew language and... I think it was zero-width-joiners being used to superimpose some characters to make up a currency symbol.

Well, I was in the ball-park: it was to do with Hebrew language, and the currency symbol being moved about to the "wrong" side of the number.

https://www.b4x.com/android/forum/threads/hebrew-currency-with-number-string.107612/#post-672849

The fix there was to use a Unicode control code Chr(0x200F) to (temporarily?) override the text direction. Whether that same trick will work re: embedded gif images, I don't know, but perhaps it's worth a try.

1662267557866.png
 
Upvote 0

emexes

Expert
Licensed User
Could you show a screen dump of
- the " hello -) how is everything -) " string being passed into MarkPattern(), and
- the BBCodeView displayed output for that string?

Also, I'm still not certain on: are you actually working with a RTL language eg Arabic, Hebrew, Kurdish ?

And if you've got BBCodeView in RTL mode per:
I will use BBCodeView to view text messages and for sure i had to enable RTL for multilingual support
then: what happens if you disable RTL?
 
Upvote 0

Addo

Well-Known Member
Licensed User
- the BBCodeView displayed output for that string?
the log output is

Hello [View=view0/] how is everything [View=view1/]

which is expected.

Could you show a screen dump of
- the " hello -) how is everything -) " string being passed into MarkPattern(), and
Screenshot attached.

Also, I'm still not certain on: are you actually working with a RTL language eg Arabic, Hebrew, Kurdish ?

And if you've got BBCodeView in RTL mode per:

then: what happens if you disable RTL?
if RTL property is set to False in BBcodeview the text printed out correctly , but this wont help since arabic,kurdish,hebrew language are supported in my app.
 

Attachments

  • Screenshot_20220904_073637_b4a.example.jpg
    Screenshot_20220904_073637_b4a.example.jpg
    72.5 KB · Views: 71
  • RTLDisabled.jpg
    RTLDisabled.jpg
    40.9 KB · Views: 70
Upvote 0

emexes

Expert
Licensed User
if RTL property is set to False in BBcodeview the text printed out correctly , but this wont help since arabic,kurdish,hebrew language are supported in my app.

I'm thinking your app set the BBCodeView RTL property to True only if your app (or the phone locale?) is using a RTL language.

With luck this is something that you need do only when a layout is loaded.
 
Upvote 0

Addo

Well-Known Member
Licensed User
I'm thinking your app set the BBCodeView RTL property to True only if your app (or the phone locale?) is using a RTL language.

With luck this is something that you need do only when a layout is loaded.
No dude RTL are always enabled during BBCodeView initialization, since those messages in the chat room should be showing correctly from each user. Its not based on user devices
 
Upvote 0

Addo

Well-Known Member
Licensed User
an ugly workaround can be something Like this

B4X:
' by default we will set RTL to /false
BBCodeView1.RTL = False

' this is an ugly way to detect if input string is english or arabic or hebrew if so set rtl to true
If TextInput.ToLowerCase = TextInput.ToUpperCase Then
BBCodeView1.RTL = True
End If

Since setting RTL to True cause English text gets backwarded in BBcodeView while adding views Like this
B4X:
Hello [View=view0/] how is everything [View=view1/]
and doesn't have a similar behavior on none English text so i make that ugly check that mentioned above to check if text is none English letters.
 
Upvote 0

Addo

Well-Known Member
Licensed User
to beautify things more we can use regex :D

B4X:
Sub CheckRTL(inputText As String) As Boolean

    Dim matcher1 As Matcher
    ' check code range for {hebrew,arabic,persain,Chinese}...
    matcher1 = Regex.Matcher("[\u0591-\u07FF-\u4E00-\u9FA5]", inputText)

    Dim strfound As Boolean = matcher1.Find

    Return strfound
   
End Sub

usage

B4X:
dim rtlstr as String = "זזه الا"
If CheckRTL(rtlstr)Then
BBCodeView1.RTL = True
End If
 
Upvote 0
Top