Two identical strings not identical?

Hi there,
I’m trying to test if a filename is what I think it is. The filename is Franais.txt. However, when I test f.name against the string “Franais.txt”, they don’t match. I’m guessing it’s the cedilla, but I’m blowed if I can work out why.

This code demonstrates:

Dim count As Integer = SpecialFolder.Desktop.Count For i As Integer = 1 to count Dim f As FolderItem = SpecialFolder.Desktop.Item(i) If f <> Nil Then if left(f.Name, 4)="Fran" then if f.Name = "Franais.txt" then break else break end if end if End If Next

To reproduce this, make a text file on the desktop whose name is Franais.txt - you’ll see it breaks on the second break, whereas I’d expect it to break on the first.

I’ve tried doing a ConvertEncoding on both to UTF-8, but that makes no difference.

if you were to look at the hexadecimal representation of those two strings in the debugger, you’d see that the one coming from f.name looks like this:

4672 616E 63CC A761 6973 2E74 7874

Whereas the one from the string literal looks like this:

4672 616E C3A7 6169 732E 7478 74

Have a kook at this

https://forum.xojo.com/20173-bug-in-replaceall/0

It works here. ConvertEncoding is not necessary.

Maybe you forgot that you are in a loop? Remove the Break statement after the Else statement.

OK, thanks for this - so, they’re different. But they’re the same! What’s going on?!

Decomposed vs precomposed - see link

[quote=219072:@Greg O’Lone]if you were to look at the hexadecimal representation of those two strings in the debugger, you’d see that the one coming from f.name looks like this:

4672 616E 63CC A761 6973 2E74 7874

Whereas the one from the string literal looks like this:

4672 616E C3A7 6169 732E 7478 74

Not here. They are both identical (the first).

I can reproduce too. This works however, as expected:

  dim target as text = "Franais.txt"
  
  Dim count As Integer  = SpecialFolder.Desktop.Count
  For i As Integer = 1 to count
    Dim f As FolderItem =  SpecialFolder.Desktop.Item(i)
    If f <> Nil Then
      if left(f.Name, 4)="Fran" then
        dim nameAsText as text = f.Name.ToText
        if nameAsText = target  then
          break
        else
          break
        end if
      end if
    End If
  Next

This is a perfect illustration as to why Text is a better conceptual representation of human-readable text. It represents the actual series of characters rather than the encoded bytes that represent those characters.

Is this maybe an OS thing? Because here on OS X the code of the original post works (provided there is no other file having a name starting with “Fran”).

I’m using OS X 10.10.5

This is a good occasion to use Text instead of String. Text is able to compare graphemes, so it does not mind the number of bytes :

Dim f as FolderItem = SpecialFolder.Desktop.child("a?e?e?c?a?u?a?e?i?o?u?.txt") dim filename as Text = f.name.ToText dim compareto as text = "a?e?e?c?a?u?a?e?i?o?u?.txt" if filename = compareto then msgbox "yeah !"

I’m a bit puzzled. Using string should not work on the Mac then? Because I get equal on my Mac:

Dim f as FolderItem = SpecialFolder.Desktop.Child(".txt") dim Filename as String = f.Name dim CompareTo as String = ".txt" if Filename = CompareTo then MsgBox "Equal" else MsgBox "Not equal" end if

So it works for some and not for others???

[quote=219144:@Markus Winter]I’m a bit puzzled. Using string should not work on the Mac then? Because I get equal on my Mac:

Dim f as FolderItem = SpecialFolder.Desktop.Child("äéèçàùâêîôû.txt") dim Filename as String = f.Name dim CompareTo as String = "äéèçàùâêîôû.txt" if Filename = CompareTo then MsgBox "Equal" else MsgBox "Not equal" end if

So it works for some and not for others???[/quote]

I created the file back a while ago to debug a problem of the same nature between precomposited and decomposed
https://forum.xojo.com/20139-mail-app-attachment-as-symbol-or-within-mail/19

At the time I did verify quite clearly the issue with String. I have no idea why it varies from system to system. However, I am positive Text is the best way to avoid possible issues. If it does nothing, it does not hurt. Whereas String seems unreliable enough not to risk issues with customers, IMO.

@Kem Tekinay : you said you can reproduce the problem.

Could you try with the String code? And what system do you use?

Works here for both names “.txt” and “Franais.txt” in 2015r2.4 on OS X 10.9.5

But whats not stated is whether this is on OS X, Linux, Windows

[quote=219148:@Markus Winter]@Kem Tekinay : you said you can reproduce the problem.

Could you try with the String code? And what system do you use?[/quote]

I’m not sure what “the String code” means. I tried with the OP’s code on MacOS X 10.10.5 and got the results that Greg outlined.

Oh and I created the file with BBEdit and used copy and paste from the OP to name it.

new folder - type in name
the code doesnt care what kind of file it is :stuck_out_tongue:
the op’s code fails here as well but markus code works
gawd i love composed vs decomposed utf-16 vs utf-8 etc

I just tried the OP code on both Mac and PC, the strings are equal, if I type from my French keyboard or from the Emoji and Symbols palette. I suspect the name of the file may have been created by copy/paste from another source which uses precomposed.

(keyboard) looks identical to c + &u0327 (c plus non advancing cedilla). This does not find equal :

Dim count As Integer = SpecialFolder.Desktop.Count For i As Integer = 1 to count Dim f As FolderItem = SpecialFolder.Desktop.Item(i) If f <> Nil Then if left(f.Name, 4)="Fran" then dim nom as string = "Franc"+&u0327+"ais.txt" system.debuglog nom if f.Name = nom then break else break end if end if End If Next

The most annoying is that even using text, it does not find equal…