Hi there! Is there really no way to iterate in Kot...
# android
a
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
a
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
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
Just unit test from Android studio.
Copy code
import java.text.BreakIterator;
m
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
Really?! πŸ™‚
So which one
java.text
Β orΒ 
android.icu.text
should I import?
m
that did not matter for me, I guess both uses the same implementation, its the runtime that matters, Androids Art vs Javas JVM
a
Ok, I will test it. Thanks you a lot!
@Mikael Alfredsson +1
No karma points here?
m
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
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
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
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
You also can test with unit tests, but you probably need newer version of Java