-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathRangeSplitter.java
67 lines (56 loc) · 2.17 KB
/
RangeSplitter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.expensify.livemarkdown;
import androidx.annotation.NonNull;
import com.facebook.systrace.Systrace;
import java.util.ArrayList;
import java.util.List;
public class RangeSplitter {
public static ArrayList<MarkdownRange> splitRangesOnEmojis(@NonNull List<MarkdownRange> markdownRanges, @NonNull String type) {
ArrayList<MarkdownRange> emojiRanges = new ArrayList<>();
ArrayList<MarkdownRange> oldRanges = new ArrayList<>(markdownRanges);
ArrayList<MarkdownRange> newRanges = new ArrayList<>();
try {
Systrace.beginSection(0, "splitRangesOnEmojis");
for (MarkdownRange range : oldRanges) {
if (range.getType().equals("emoji")) {
emojiRanges.add(range);
}
}
int i = 0;
int j = 0;
while (i < oldRanges.size()) {
MarkdownRange currentRange = oldRanges.get(i);
if (!currentRange.getType().equals(type)) {
newRanges.add(currentRange);
i += 1;
continue;
}
// Iterate through all emoji ranges before the end of the current range, splitting the current range at each intersection.
while (j < emojiRanges.size()) {
MarkdownRange emojiRange = emojiRanges.get(j);
if (emojiRange.getStart() > currentRange.getEnd()) {
break;
}
int currentStart = currentRange.getStart();
int currentEnd = currentRange.getEnd();
int emojiStart = emojiRange.getStart();
int emojiEnd = emojiRange.getEnd();
if (emojiStart >= currentStart && emojiEnd <= currentEnd) { // Intersection
MarkdownRange newRange = new MarkdownRange(currentRange.getType(), currentStart, emojiStart - currentStart, currentRange.getDepth());
currentRange = new MarkdownRange(currentRange.getType(), emojiEnd, currentEnd - emojiEnd, currentRange.getDepth());
if (newRange.getLength() > 0) {
newRanges.add(newRange);
}
}
j += 1;
}
if (currentRange.getLength() > 0) {
newRanges.add(currentRange);
}
i += 1;
}
} finally {
Systrace.endSection(0);
}
return newRanges;
}
}