https://kotlinlang.org logo
#android
Title
# android
a

Ariel Bogdziewicz

09/21/2020, 10:37 AM
Hi there! Is there really no way to iterate in Kotlin/Java over graphenes/runes like👨‍👩‍👧‍👧”?
Copy code
A: count = 1, utf16 = 1, unicode scalars = 1 -> ['A']
€: count = 1, utf16 = 1, unicode scalars = 1 -> ['€']
➜: count = 1, utf16 = 1, unicode scalars = 1 -> ['➜']
⛳: count = 1, utf16 = 1, unicode scalars = 1 -> ['⛳']
💩: count = 1, utf16 = 2, unicode scalars = 1 -> ['💩']
❤️: count = 1, utf16 = 2, unicode scalars = 2 -> ['❤', '️']
👨‍👩‍👧‍👦: count = 1, utf16 = 11, unicode scalars = 7 -> ['👨', '‍', '👩', '‍', '👧', '‍', '👦']
👩‍❤️‍💋‍👩: count = 1, utf16 = 11, unicode scalars = 8 -> ['👩', '‍', '❤', '️', '‍', '💋', '‍', '👩']
In swift it’s just
text.count
and it returns number of characters like “👨‍👩‍👧‍👧”. I tried
Copy code
@Test
    public void printBreak() {
        BreakIterator iterator = BreakIterator.getCharacterInstance();
        String[] tests = {
                "\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC67", // family icon 👨‍👩‍👧‍👧
                "💩",
                "€"
        };
        for (String test: tests) {
            iterator.setText(test);
            int index = iterator.first();
            System.out.println("!!!! Start " + test);
            while (index != BreakIterator.DONE) {
                System.out.println(index);
                index = iterator.next();
            }
            System.out.println("!!!! End " + test);
        }
    }
as article https://engineering.linecorp.com/en/blog/the-7-ways-of-counting-characters/ suggested but it also iterates over unicode scalars like
Copy code
!!!! Start 👨‍👩‍👧‍👧
0
2
3
5
6
8
9
11
!!!! End 👨‍👩‍👧‍👧
Is it really so rocket science to iterate over multiple-characters in Android?
g

gildor

09/21/2020, 11:35 AM
a

Ariel Bogdziewicz

09/21/2020, 11:42 AM
Code point is unicode scalar so family icon 👨‍👩‍👧‍👧 consists of 8 code points. I would need something which will return
Copy code
"👨‍👩‍👧‍👧".count = 1
CodePoint iterator returns
Copy code
!!!! Start 👨‍👩‍👧‍👧
128104
8205
128105
8205
128103
8205
128103
!!!! End 👨‍👩‍👧‍👧
m

Mikael Alfredsson

09/21/2020, 12:57 PM
when you test with
BreakIterator
, do you use the one from
java.text
or
android.icu.text
, and do you test on a device, or just a local unit test on your machine?
sorry,
java.text
vs
android.icu.text
doesn’t seem to matter, but running it on a device or locally might.
a

Ariel Bogdziewicz

09/21/2020, 1:23 PM
Just unit test from Android studio.
Copy code
import java.text.BreakIterator;
m

Mikael Alfredsson

09/21/2020, 1:25 PM
When I run it as a unit test, i also get all scalars printed out (and if I copy the family icon from here and past it into AS, i get 4 separate icons) which for me means that Java can’t handle the family icon. If i run it as an instrumentation test on a android 11 device, i also get the count of 1 (as you get on ios)
a

Ariel Bogdziewicz

09/21/2020, 1:26 PM
Really?! 🙂
So which one
java.text
 or 
android.icu.text
should I import?
m

Mikael Alfredsson

09/21/2020, 1:27 PM
that did not matter for me, I guess both uses the same implementation, its the runtime that matters, Androids Art vs Javas JVM
a

Ariel Bogdziewicz

09/21/2020, 1:27 PM
Ok, I will test it. Thanks you a lot!
@Mikael Alfredsson +1
No karma points here?
m

Mikael Alfredsson

09/21/2020, 1:28 PM
but on older devices where the family emoji doesn’t exists, you should get the 4 separated emojis and a count of 4 or 8 depending on if the separator inbetween is counted.
so do not rely on getting the count of 1 on all devices.
a

Ariel Bogdziewicz

09/21/2020, 1:29 PM
Yes. I know… especially BreatIterator >= API 24 and we support >= 21
Thanks a lot once again 🎉
It works on Nexus 6 API 29
Copy code
I/System.out: !!!!!!! BreakIterator
I/System.out: !!!! Start 👨‍👩‍👧‍👧
    0
    11
    !!!! End 👨‍👩‍👧‍👧
How to write unit tests for this? 🤦🏼‍♂️ If it works differently depending on host machine.
m

Mikael Alfredsson

09/21/2020, 1:48 PM
it requires an instrumentation test (running on a device ) and if you can see any difference between different API versions, you should check that as well in the test
a

Ariel Bogdziewicz

09/21/2020, 2:06 PM
I have 10 years with iOS behind me but it’s my first month with Android 😕 I have no idea how to set up tests like you said.
Google “instrumentation test” and we will see 🙂
g

gildor

09/21/2020, 2:18 PM
You also can test with unit tests, but you probably need newer version of Java
38 Views