MCPcopy Create free account
hub / github.com/bttv-android/bttv / tokenize

Method tokenize

mod/app/src/main/java/bttv/Tokenizer.java:32–95  ·  view source on GitHub ↗
(List<MessageToken> orig)

Source from the content-addressed store, hash-verified

30public class Tokenizer {
31
32 public static Pair<List<MessageToken>, Boolean> tokenize(List<MessageToken> orig) {
33 Context ctx = Data.ctx;
34 int channel = Data.currentBroadcasterId;
35
36 // Don't add Emotes, when we don't have the channel's emotes (yet)
37 // and we don't need to Highlight any messages
38 if (!Emotes.channelHasEmotes(ctx, channel) && Highlight.getInstance().isEmpty()) {
39 return new Pair<>(orig, false);
40 }
41
42 ArrayList<MessageToken> newTokens = new ArrayList<>(orig.size() + 5);
43 boolean shouldHighlight = false;
44
45 for (MessageToken token : orig) {
46 // possible issue: emotes won't work in e.g. MentionToken or BitsToken
47 if (!(token instanceof TextToken)) {
48 if (token instanceof EmoticonToken && !newTokens.isEmpty()) {
49 if (newTokens.get(newTokens.size() - 1) instanceof EmoticonToken) {
50 newTokens.add(new TextToken(" ", new AutoModMessageFlags()));
51 }
52 }
53 newTokens.add(token);
54 continue;
55 }
56
57 TextToken text = (TextToken) token;
58
59 if (text.getText().equals(" ")) {
60 // " ".split(" ") will produce an empty array
61 // this is why we need to handle this edge-case
62 newTokens.add(token);
63 continue;
64 }
65 String[] tokens = text.getText().split(" ");
66
67 StringBuilder currentText = new StringBuilder();
68 for (String word : tokens) {
69 Emote emote = Emotes.getEmote(ctx, word, channel);
70 if (Highlight.shouldHighlight(word)) {
71 shouldHighlight = true;
72 }
73 if (emote == null) {
74 currentText.append(word).append(" ");
75 continue;
76 }
77 // emote found
78 String before = currentText.toString();
79 if (!before.isEmpty()) {
80 newTokens.add(new TextToken(currentText.toString(), text.getFlags())); // add everything before Emote as TextToken
81 }
82 newTokens.add(new EmoticonToken(word, "BTTV-" + emote.id)); // add Emote
83
84 // prepare next TextToken
85 currentText.setLength(0);
86 currentText.append(' ');
87 }
88 String before = currentText.toString();
89 if (!before.trim().isEmpty()) {

Callers 2

testMethod · 0.95
getTokensMethod · 0.95

Calls 10

channelHasEmotesMethod · 0.95
getInstanceMethod · 0.95
getTextMethod · 0.95
getEmoteMethod · 0.95
shouldHighlightMethod · 0.95
getFlagsMethod · 0.95
isEmptyMethod · 0.80
addMethod · 0.80
getMethod · 0.65
toStringMethod · 0.45

Tested by 1

testMethod · 0.76