Benoit Marty
2 weeks ago
committed by
GitHub
11 changed files with 112 additions and 7 deletions
@ -0,0 +1,44 @@
@@ -0,0 +1,44 @@
|
||||
/* |
||||
* Copyright (c) 2024 New Vector Ltd |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* https://www.apache.org/licenses/LICENSE-2.0 |
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package io.element.android.features.messages.impl.utils |
||||
|
||||
import com.sigpwned.emoji4j.core.Grapheme.Type.EMOJI |
||||
import com.sigpwned.emoji4j.core.Grapheme.Type.PICTOGRAPHIC |
||||
import com.sigpwned.emoji4j.core.GraphemeMatchResult |
||||
import com.sigpwned.emoji4j.core.GraphemeMatcher |
||||
|
||||
/** |
||||
* Returns true if the string consists exclusively of "emoji or pictographic graphemes". |
||||
*/ |
||||
fun String.containsOnlyEmojis(): Boolean { |
||||
if (isEmpty()) return false |
||||
|
||||
val matcher = GraphemeMatcher(this) |
||||
var m: GraphemeMatchResult? = null |
||||
var contiguous = true |
||||
var previous = 0 |
||||
while (contiguous && matcher.find()) { |
||||
m = matcher.toMatchResult() |
||||
// Many non-"emoji" characters are pictographics. We only want to identify this specific range |
||||
// https://en.wikipedia.org/wiki/Miscellaneous_Symbols_and_Pictographs |
||||
val isEmoji = m!!.grapheme().type == EMOJI || m.grapheme().type == PICTOGRAPHIC && m.group() in "π".."πΊ" |
||||
contiguous = isEmoji and (m.start() == previous) |
||||
previous = m.end() |
||||
} |
||||
|
||||
return contiguous and (m?.end() == length) |
||||
} |
@ -1,3 +1,3 @@
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1 |
||||
oid sha256:9490990c863318390d0f088ff82ad01a688c81cada63d749872b22b633fa12fe |
||||
size 23040 |
||||
oid sha256:65eb29b745fabe2587019c169a48a862b129a3ece30cf9bc82aff09c67878b35 |
||||
size 23180 |
||||
|
@ -1,3 +1,3 @@
@@ -1,3 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1 |
||||
oid sha256:730c2c5ab52209366611546dbdbbed62214f6f5e13328be66cce5f08d8f4f0e5 |
||||
size 23125 |
||||
oid sha256:c1cf0091d377d8018d14804433aaa67d1b0b510778b6f8a4e8a73faff2b4c782 |
||||
size 23280 |
||||
|
Loadingβ¦
Reference in new issue