Page 2 of 2

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 11:30 am
by #NULL
@mk-soft
Your ProcedureReturn values should be swapped?

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 11:41 am
by mk-soft
@idle
I have already prepared something for your modules

Code: Select all

;-TOP by mk-soft

Procedure IsUTF16String(String$)
  Protected *String.Unicode = @String$
  
  If *String
    While *String\u
      If *String\u > $D7FF And *String\u < $E000
        ProcedureReturn #True
      EndIf
      *String + 2
    Wend
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure LenUTF16(String$)
  Protected *Char.Unicode
  Protected cnt
  
  *Char.Unicode = @String$
  If *Char
    While *Char\u
      If *Char\u > $D7FF And *Char\u < $E000
        *Char + 4
      Else
        *Char + 2
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn cnt
EndProcedure

Procedure.s LeftUTF16(String$, Length)
  Protected *Char.Unicode
  Protected len, cnt
  
  If Length < 1
    ProcedureReturn ""
  EndIf
  *Char.Unicode = @String$
  If *Char
    While *Char\u
      If cnt >= Length
        Break
      EndIf
      If *Char\u > $D7FF And *Char\u < $E000
        *Char + 4
        len + 2
      Else
        *Char + 2
        len + 1
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn Left(String$, len)
EndProcedure

Procedure.s RightUTF16(String$, Length)
  Protected *Char.Unicode, *Char2.Unicode, *String.Unicode
  Protected len, cnt
  
  If Length < 1
    ProcedureReturn ""
  EndIf
  *String = @String$
  If *String
    *Char = *String + StringByteLength(String$) - 2
    While *Char\u
      If cnt >= Length Or *Char <= *String
        Break
      EndIf
      *Char2 = *Char - 2
      If *Char2 >= *String And (*Char\u > $D7FF And *Char\u < $E000)
        *Char - 4
        len + 2
      Else
        *Char - 2
        len + 1
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn Right(String$, len)
EndProcedure

; ****

Define s1.s

s1 = "πŸ…AπŸ…šKπŸ…"
Debug "Is '" + s1 + "' UFT16: " + IsUTF16String(s1)
Debug "StringByteLength = " + StringByteLength(s1)
Debug "Len = " + LenUTF16(s1)
Debug "Left = " + LeftUTF16(s1, 2)
Debug "Right = " + RightUTF16(s1, 2)


Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 11:50 am
by mk-soft
#NULL wrote: Sat May 06, 2023 11:30 am @mk-soft
Your ProcedureReturn values should be swapped?
I don't think so, as it is no longer a UCS-2 string

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 12:12 pm
by idle
mk-soft wrote: Sat May 06, 2023 11:41 am @idle
I have already prepared something for your modules

Code: Select all

;-TOP by mk-soft

Procedure IsUTF16String(String$)
  Protected *String.Unicode = @String$
  
  If *String
    While *String\u
      If *String\u > $D7FF And *String\u < $E000
        ProcedureReturn #True
      EndIf
      *String + 2
    Wend
  EndIf
  ProcedureReturn #False
EndProcedure

Procedure LenUTF16(String$)
  Protected *Char.Unicode
  Protected cnt
  
  *Char.Unicode = @String$
  If *Char
    While *Char\u
      If *Char\u > $D7FF And *Char\u < $E000
        *Char + 4
      Else
        *Char + 2
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn cnt
EndProcedure

Procedure.s LeftUTF16(String$, Length)
  Protected *Char.Unicode
  Protected len, cnt
  
  If Length < 1
    ProcedureReturn ""
  EndIf
  *Char.Unicode = @String$
  If *Char
    While *Char\u
      If cnt >= Length
        Break
      EndIf
      If *Char\u > $D7FF And *Char\u < $E000
        *Char + 4
        len + 2
      Else
        *Char + 2
        len + 1
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn Left(String$, len)
EndProcedure

Procedure.s RightUTF16(String$, Length)
  Protected *Char.Unicode, *Char2.Unicode, *String.Unicode
  Protected len, cnt
  
  If Length < 1
    ProcedureReturn ""
  EndIf
  *String = @String$
  If *String
    *Char = *String + StringByteLength(String$) - 2
    While *Char\u
      If cnt >= Length Or *Char <= *String
        Break
      EndIf
      *Char2 = *Char - 2
      If *Char2 >= *String And (*Char\u > $D7FF And *Char\u < $E000)
        *Char - 4
        len + 2
      Else
        *Char - 2
        len + 1
      EndIf
      cnt + 1
    Wend
  EndIf
  ProcedureReturn Right(String$, len)
EndProcedure

; ****

Define s1.s

s1 = "πŸ…AπŸ…šKπŸ…"
Debug "Is '" + s1 + "' UFT16: " + IsUTF16String(s1)
Debug "StringByteLength = " + StringByteLength(s1)
Debug "Len = " + LenUTF16(s1)
Debug "Left = " + LeftUTF16(s1, 2)
Debug "Right = " + RightUTF16(s1, 2)

Thanks looks good πŸ‘ I'll add it tomorrow

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 4:00 pm
by The8th
Thank you guys that you invest so much efforts in my problem.
But I do not want to include extra modules and procedures in my code. My goal is to simply test if there is one of these strange characters on the right end of my strings. My strings involved only contain one of these characters each, and I can use

Code: Select all

If Right(mystring$, 2) = "πŸ…"
this works in this case.
Also

Code: Select all

If FindString(mystring$, "πŸ…")
works. And even

Code: Select all

RemoveString(mystring$, "πŸ…š")
ReplaceString(mystring$, "πŸ…š", "nothing")
work.
Nevertheless it's strange and a pity that it does not work with all PB string functions.
Henry

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 6:30 pm
by Olli
UTF is too complex ! I stopped my read to Unicode 0xD83C. Now, I suppose the IDE does not support it...

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 7:41 pm
by STARGΓ…TE
I already posted some supplementary functions in the german board:
Characters above the Basic Multilingual Plane (BMP) plane

Re: How do I get the leftmost/righmost character in a string?

Posted: Sat May 06, 2023 9:48 pm
by Olli
Thank you ! (Die Zeit verbringt... :cry: )