Skip to content

Commit

Permalink
Optimize getting code points of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
freya022 committed Nov 23, 2023
1 parent 6daf76e commit 605cdbb
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions lib/src/main/java/net/fellbaum/jemoji/EmojiManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static net.fellbaum.jemoji.EmojiUtils.addColonToAlias;
import static net.fellbaum.jemoji.EmojiUtils.findEmojiByEitherAlias;
import static net.fellbaum.jemoji.EmojiUtils.isStringNullOrEmpty;
import static net.fellbaum.jemoji.EmojiUtils.removeColonFromAlias;
import static net.fellbaum.jemoji.EmojiUtils.*;

@SuppressWarnings("unused")
public final class EmojiManager {
Expand Down Expand Up @@ -53,7 +50,7 @@ public final class EmojiManager {

private static Collector<Emoji, ?, LinkedHashMap<Integer, List<Emoji>>> getEmojiLinkedHashMapCollector() {
return Collectors.groupingBy(
emoji -> emoji.getEmoji().codePoints().toArray()[0],
emoji -> emoji.getEmoji().codePointAt(0),
LinkedHashMap::new,
Collectors.collectingAndThen(
Collectors.toList(),
Expand Down Expand Up @@ -237,14 +234,14 @@ public static boolean containsEmoji(final String text) {

final List<Emoji> emojis = new ArrayList<>();

final int[] textCodePointsArray = text.codePoints().toArray();
final int[] textCodePointsArray = stringToCodePoints(text);
final long textCodePointsLength = textCodePointsArray.length;

for (int textIndex = 0; textIndex < textCodePointsLength; textIndex++) {
final List<Emoji> emojisByCodePoint = EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING.get(textCodePointsArray[textIndex]);
if (emojisByCodePoint == null) continue;
for (final Emoji emoji : emojisByCodePoint) {
final int[] emojiCodePointsArray = emoji.getEmoji().codePoints().toArray();
final int[] emojiCodePointsArray = stringToCodePoints(emoji.getEmoji());
final int emojiCodePointsLength = emojiCodePointsArray.length;
// Emoji code points are in bounds of the text code points
if (!((textIndex + emojiCodePointsLength) <= textCodePointsLength)) {
Expand Down Expand Up @@ -275,7 +272,7 @@ public static List<Emoji> extractEmojisInOrder(final String text) {

final List<Emoji> emojis = new ArrayList<>();

final int[] textCodePointsArray = text.codePoints().toArray();
final int[] textCodePointsArray = stringToCodePoints(text);
final long textCodePointsLength = textCodePointsArray.length;

// JDK 21 Characters.isEmoji
Expand All @@ -286,7 +283,7 @@ public static List<Emoji> extractEmojisInOrder(final String text) {
final List<Emoji> emojisByCodePoint = EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING.get(currentCodepoint);
if (emojisByCodePoint == null) continue;
for (final Emoji emoji : emojisByCodePoint) {
final int[] emojiCodePointsArray = emoji.getEmoji().codePoints().toArray();
final int[] emojiCodePointsArray = stringToCodePoints(emoji.getEmoji());
final int emojiCodePointsLength = emojiCodePointsArray.length;
// Emoji code points are in bounds of the text code points
if (!((textIndex + emojiCodePointsLength) <= textCodePointsLength)) {
Expand Down Expand Up @@ -372,7 +369,7 @@ public static String removeAllEmojisExcept(final String text, final Emoji... emo
*/
public static String removeAllEmojisExcept(final String text, final Collection<Emoji> emojisToKeep) {
if (isStringNullOrEmpty(text)) return "";
final int[] textCodePointsArray = text.codePoints().toArray();
final int[] textCodePointsArray = stringToCodePoints(text);
final long textCodePointsLength = textCodePointsArray.length;

final StringBuilder sb = new StringBuilder();
Expand All @@ -385,7 +382,7 @@ public static String removeAllEmojisExcept(final String text, final Collection<E
final List<Emoji> emojisByCodePoint = EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING.get(currentCodepoint);
if (emojisByCodePoint == null) continue;
for (final Emoji emoji : emojisByCodePoint) {
final int[] emojiCodePointsArray = emoji.getEmoji().codePoints().toArray();
final int[] emojiCodePointsArray = stringToCodePoints(emoji.getEmoji());
final int emojiCodePointsLength = emojiCodePointsArray.length;
// Check if Emoji code points are in bounds of the text code points
if (!((textIndex + emojiCodePointsLength) <= textCodePointsLength)) {
Expand Down Expand Up @@ -472,7 +469,7 @@ public static String replaceEmojis(final String text, final String replacementSt
public static String replaceEmojis(final String text, Function<Emoji, String> replacementFunction, final Collection<Emoji> emojisToReplace) {
if (isStringNullOrEmpty(text)) return "";

final int[] textCodePointsArray = text.codePoints().toArray();
final int[] textCodePointsArray = stringToCodePoints(text);
final long textCodePointsLength = textCodePointsArray.length;

final StringBuilder sb = new StringBuilder();
Expand All @@ -485,7 +482,7 @@ public static String replaceEmojis(final String text, Function<Emoji, String> re
final List<Emoji> emojisByCodePoint = EMOJI_FIRST_CODEPOINT_TO_EMOJIS_ORDER_CODEPOINT_LENGTH_DESCENDING.get(currentCodepoint);
if (emojisByCodePoint == null) continue;
for (final Emoji emoji : emojisByCodePoint) {
final int[] emojiCodePointsArray = emoji.getEmoji().codePoints().toArray();
final int[] emojiCodePointsArray = stringToCodePoints(emoji.getEmoji());
final int emojiCodePointsLength = emojiCodePointsArray.length;
// Check if Emoji code points are in bounds of the text code points
if (!((textIndex + emojiCodePointsLength) <= textCodePointsLength)) {
Expand Down Expand Up @@ -529,6 +526,17 @@ public static String replaceEmojis(final String text, Function<Emoji, String> re
return replaceEmojis(text, replacementFunction, Arrays.asList(emojisToReplace));
}

private static int[] stringToCodePoints(String text) {
int[] codePoints = new int[getCodePointCount(text)];
int j = 0;
for (int i = 0; i < text.length();) {
final int codePoint = text.codePointAt(i);
codePoints[j++] = codePoint;
i += Character.charCount(codePoint);
}
return codePoints;
}

/*public static List<Emoji> testEmojiPattern(final String text) {
if (isStringNullOrEmpty(text)) return Collections.emptyList();
Expand Down

0 comments on commit 605cdbb

Please sign in to comment.