From e236fc1dd876561dd0f2e79d577d3473324068c5 Mon Sep 17 00:00:00 2001 From: Niklas Baudy Date: Wed, 11 May 2022 11:34:17 +0200 Subject: [PATCH] EmojiRange: Replace start & end with IntRange. (#779) --- .../vanniktech/emoji/ios/EmojiUtilsTest.kt | 20 ++++++++----------- .../com/vanniktech/emoji/EmojiManager.kt | 8 ++++---- .../kotlin/com/vanniktech/emoji/EmojiRange.kt | 3 +-- .../vanniktech/emoji/EmojiInformationTest.kt | 4 ++-- .../com/vanniktech/emoji/EmojiManagerTest.kt | 4 ++-- .../com/vanniktech/emoji/EmojiRangeTest.kt | 20 +++++++++---------- 6 files changed, 27 insertions(+), 32 deletions(-) diff --git a/emoji-ios/src/test/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt b/emoji-ios/src/test/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt index a250bb2f5e..f0e32131cc 100644 --- a/emoji-ios/src/test/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt +++ b/emoji-ios/src/test/kotlin/com/vanniktech/emoji/ios/EmojiUtilsTest.kt @@ -28,14 +28,12 @@ class EmojiUtilsTest { assertEquals(true, emojiInformation.isOnlyEmojis) assertEquals(2, emojiInformation.emojis.size) - assertEquals(0, emojiInformation.emojis[0].start) - assertEquals(5, emojiInformation.emojis[0].end) - assertEquals(mage1, text.substring(emojiInformation.emojis[0].start, emojiInformation.emojis[0].end)) + assertEquals(0..5, emojiInformation.emojis[0].range) + assertEquals(mage1, text.substring(emojiInformation.emojis[0].range.first, emojiInformation.emojis[0].range.last)) assertEquals(mage1, emojiInformation.emojis[0].emoji.unicode) - assertEquals(5, emojiInformation.emojis[1].start) - assertEquals(12, emojiInformation.emojis[1].end) - assertEquals(mage2, text.substring(emojiInformation.emojis[1].start, emojiInformation.emojis[1].end)) + assertEquals(5..12, emojiInformation.emojis[1].range) + assertEquals(mage2, text.substring(emojiInformation.emojis[1].range.first, emojiInformation.emojis[1].range.last)) assertEquals(mage2, emojiInformation.emojis[1].emoji.unicode) } @@ -47,14 +45,12 @@ class EmojiUtilsTest { assertEquals(false, emojiInformation.isOnlyEmojis) assertEquals(2, emojiInformation.emojis.size) - assertEquals(7, emojiInformation.emojis[0].start) - assertEquals(9, emojiInformation.emojis[0].end) - assertEquals(hamburger, text.substring(emojiInformation.emojis[0].start, emojiInformation.emojis[0].end)) + assertEquals(7..9, emojiInformation.emojis[0].range) + assertEquals(hamburger, text.substring(emojiInformation.emojis[0].range.first, emojiInformation.emojis[0].range.last)) assertEquals(hamburger, emojiInformation.emojis[0].emoji.unicode) - assertEquals(23, emojiInformation.emojis[1].start) - assertEquals(25, emojiInformation.emojis[1].end) - assertEquals(cheese, text.substring(emojiInformation.emojis[1].start, emojiInformation.emojis[1].end)) + assertEquals(23..25, emojiInformation.emojis[1].range) + assertEquals(cheese, text.substring(emojiInformation.emojis[1].range.first, emojiInformation.emojis[1].range.last)) assertEquals(cheese, emojiInformation.emojis[1].emoji.unicode) } diff --git a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiManager.kt b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiManager.kt index 722f8f4702..dde2c7dc03 100644 --- a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiManager.kt +++ b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiManager.kt @@ -52,7 +52,7 @@ object EmojiManager { val emoji = findEmoji(it.value) if (emoji != null) { - EmojiRange(it.range.first, it.range.last + 1, emoji) + EmojiRange(emoji, IntRange(it.range.first, it.range.last + 1)) } else { null } @@ -86,11 +86,11 @@ object EmojiManager { } val findAllEmojis = findAllEmojis(text) for (i in findAllEmojis.indices) { - val (start, end, emoji) = findAllEmojis[i] - if (!existingSpanPositions.contains(start)) { + val (emoji, range) = findAllEmojis[i] + if (!existingSpanPositions.contains(range.first)) { text.setSpan( EmojiSpan(context, emoji, emojiSize), - start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE + range.first, range.last, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE ) } } diff --git a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiRange.kt b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiRange.kt index 640d0fceb4..dc419602f7 100644 --- a/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiRange.kt +++ b/emoji/src/main/kotlin/com/vanniktech/emoji/EmojiRange.kt @@ -17,7 +17,6 @@ package com.vanniktech.emoji data class EmojiRange( - val start: Int, - val end: Int, val emoji: Emoji, + val range: IntRange, ) diff --git a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiInformationTest.kt b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiInformationTest.kt index 45a5633ea3..ce025789f3 100644 --- a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiInformationTest.kt +++ b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiInformationTest.kt @@ -32,8 +32,8 @@ class EmojiInformationTest { empty = EmojiInformation(false, emptyList) empty2 = EmojiInformation(false, emptyList) val emoji = TestEmoji(intArrayOf(0x1234), listOf("test"), false) - one = EmojiInformation(false, listOf(EmojiRange(0, 1, emoji))) - one2 = EmojiInformation(false, listOf(EmojiRange(0, 1, emoji))) + one = EmojiInformation(false, listOf(EmojiRange(emoji, 0..1))) + one2 = EmojiInformation(false, listOf(EmojiRange(emoji, 0..1))) } @Test fun equality() { diff --git a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt index 1f0a061df7..6c746f7f83 100644 --- a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt +++ b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt @@ -112,8 +112,8 @@ class EmojiManagerTest { "te" + String(intArrayOf(0x5678), 0, 1) + "st" + String(intArrayOf(0x1234), 0, 1) ) - val firstExpectedRange = EmojiRange(2, 3, TestEmoji(intArrayOf(0x5678), listOf("test"), false)) - val secondExpectedRange = EmojiRange(5, 6, TestEmoji(intArrayOf(0x1234), listOf("test"), false)) + val firstExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x5678), listOf("test"), false), 2..3) + val secondExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x1234), listOf("test"), false), 5..6) assertEquals( listOf( firstExpectedRange, diff --git a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiRangeTest.kt b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiRangeTest.kt index 1563d05cb8..302c1a884a 100644 --- a/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiRangeTest.kt +++ b/emoji/src/test/kotlin/com/vanniktech/emoji/EmojiRangeTest.kt @@ -31,18 +31,18 @@ class EmojiRangeTest { } @Test fun equality() { - assertEquals(EmojiRange(0, 1, emoji), EmojiRange(0, 1, emoji)) - assertEquals(EmojiRange(0, 1, emoji2), EmojiRange(0, 1, emoji2)) - assertNotEquals(EmojiRange(0, 1, emoji2), EmojiRange(0, 0, emoji2)) - assertNotEquals(EmojiRange(10, 1, emoji2), EmojiRange(10, 0, emoji2)) - assertNotEquals(EmojiRange(0, 1, emoji2), EmojiRange(0, 1, emoji)) + assertEquals(EmojiRange(emoji, 0..1), EmojiRange(emoji, 0..1)) + assertEquals(EmojiRange(emoji2, 0..1), EmojiRange(emoji2, 0..1)) + assertNotEquals(EmojiRange(emoji2, 0..1), EmojiRange(emoji2, 0..0)) + assertNotEquals(EmojiRange(emoji2, 1..10), EmojiRange(emoji2, 0..10)) + assertNotEquals(EmojiRange(emoji2, 0..1), EmojiRange(emoji, 0..1)) } @Test fun hashy() { - assertEquals(EmojiRange(0, 1, emoji).hashCode(), EmojiRange(0, 1, emoji).hashCode()) - assertEquals(EmojiRange(0, 1, emoji2).hashCode(), EmojiRange(0, 1, emoji2).hashCode()) - assertNotEquals(EmojiRange(0, 1, emoji2).hashCode(), EmojiRange(0, 0, emoji2).hashCode()) - assertNotEquals(EmojiRange(10, 1, emoji2).hashCode(), EmojiRange(10, 0, emoji2).hashCode()) - assertNotEquals(EmojiRange(0, 1, emoji2).hashCode(), EmojiRange(0, 1, emoji).hashCode()) + assertEquals(EmojiRange(emoji, 0..1).hashCode(), EmojiRange(emoji, 0..1).hashCode()) + assertEquals(EmojiRange(emoji2, 0..1).hashCode(), EmojiRange(emoji2, 0..1).hashCode()) + assertNotEquals(EmojiRange(emoji2, 0..1).hashCode(), EmojiRange(emoji2, 0..0).hashCode()) + assertNotEquals(EmojiRange(emoji2, 1..10).hashCode(), EmojiRange(emoji2, 0..10).hashCode()) + assertNotEquals(EmojiRange(emoji2, 0..1).hashCode(), EmojiRange(emoji, 0..1).hashCode()) } }