Skip to content

Commit

Permalink
feat: 优化代码。
Browse files Browse the repository at this point in the history
  • Loading branch information
闫茂源 committed Jun 1, 2024
1 parent c6ba9ca commit 1bf7525
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 50 deletions.
28 changes: 16 additions & 12 deletions lib/src/main/java/io/github/jmecn/text/EmojiIterator.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.github.jmecn.text;

import java.util.concurrent.atomic.AtomicBoolean;

import static io.github.jmecn.text.EmojiCategory.*;
import static io.github.jmecn.text.EmojiPresentationScanner.*;

/**
* Implementation of EmojiIterator is based on Pango's pango-emoji.c
*
*
* @see <a href="https://gitlab.gnome.org/GNOME/pango/-/blob/6fab48ac3a2d613704301d692e278cdcef3c2bf4/pango/pango-emoji.c">pango/pango-emoji.c</a>
* @author yanmaoyuan
*/
public class EmojiIterator {
Expand Down Expand Up @@ -85,24 +87,26 @@ public boolean next() {

this.start = this.end;

int cursor = this.cursor;
int p = this.cursor;

EmojiIteratorResult sr = scanEmojiPresentation(this.types, cursor, this.nChars);
cursor = sr.end;
boolean isEmoji = sr.isEmoji;
int scanResult = scanEmojiPresentation(this.types, p, this.nChars);
// because java do not have a referenced boolean type, here I use a bit mask to indicate whether the scanResult is emoji.
// this also avoid instancing a new object.
p = scanResult & 0x7FFFFFFF;
boolean emoji = (scanResult & 0x80000000) != 0;

do {
this.cursor = cursor;
this.isEmoji = isEmoji;
this.cursor = p;
this.isEmoji = emoji;

if (cursor == this.nChars) {// end
if (p == this.nChars) {// end
break;
}

sr = scanEmojiPresentation(this.types, cursor, this.nChars);
cursor = sr.end;
isEmoji = sr.isEmoji;
} while (this.isEmoji == isEmoji);
scanResult = scanEmojiPresentation(this.types, p, this.nChars);
p = scanResult & 0x7FFFFFFF;
emoji = (scanResult & 0x80000000) != 0;
} while (this.isEmoji == emoji);

this.end = this.cursor;

Expand Down
22 changes: 0 additions & 22 deletions lib/src/main/java/io/github/jmecn/text/EmojiIteratorResult.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ private EmojiPresentationScanner() {}
1, 4, 0, 1, 17, 17, 17, 17, 18, 18, 17, 17
};

@SuppressWarnings("java:S3776")
public static EmojiIteratorResult scanEmojiPresentation(byte[] data, int p, final int pe) {
public static int scanEmojiPresentation(byte[] data, int p, final int pe) {
byte act = 0;
int cs = EMOJI_PRESENTATION_START;
int te = -1;
Expand Down Expand Up @@ -167,39 +166,39 @@ public static EmojiIteratorResult scanEmojiPresentation(byte[] data, int p, fina
break;
case 5: {
te = p + 1;
return new EmojiIteratorResult(false, te);
return te;// is_emoji = false
}
case 6: {
te = p + 1;
return new EmojiIteratorResult(true, te);
return te | 0x80000000;// is_emoji = true
}
case 7: {
te = p + 1;
return new EmojiIteratorResult(false, te);
return te;// is_emoji = false
}
case 8: {
te = p;
p--;
return new EmojiIteratorResult(true, te);
return te | 0x80000000;// is_emoji = true
}
case 9: {
te = p;
p--;
return new EmojiIteratorResult(false, te);
return te;// is_emoji = false
}
case 10: {
p = ((te)) - 1;
return new EmojiIteratorResult(true, te);
return te | 0x80000000;// is_emoji = true
}
case 11: {
switch (act) {
case 2: {
p = ((te)) - 1;
return new EmojiIteratorResult(true, te);
return te | 0x80000000;// is_emoji = true
}
case 3: {
p = ((te)) - 1;
return new EmojiIteratorResult(false, te);
return te;// is_emoji = false
}
}
}
Expand Down Expand Up @@ -235,6 +234,6 @@ public static EmojiIteratorResult scanEmojiPresentation(byte[] data, int p, fina
}

/* Should not be reached. */
return new EmojiIteratorResult(false, pe);
return pe; // is_emoji = false
}
}
10 changes: 5 additions & 5 deletions tools/emoji_presentation_scanner_java.rl
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ text_run = any;
text_and_emoji_run := |*
# In order to give the the VS15 sequences higher priority than detecting
# emoji sequences they are listed first as scanner token here.
text_presentation_emoji => { return new EmojiIteratorResult(false, te);};
emoji_run => { return new EmojiIteratorResult(true, te);};
text_run => { return new EmojiIteratorResult(false, te);};
text_presentation_emoji => { return te;};
emoji_run => { return te | 0x80000000;};
text_run => { return te;};
*|;

}%%

public static EmojiIteratorResult scan_emoji_presentation(byte[] data, int p, final int pe) {
public static int scan_emoji_presentation(byte[] data, int p, final int pe) {
int ts, te;
final int eof = pe;

Expand All @@ -101,5 +101,5 @@ public static EmojiIteratorResult scan_emoji_presentation(byte[] data, int p, fi
}%%

/* Should not be reached. */
return new EmojiIteratorResult(false, pe);
return pe;
}

0 comments on commit 1bf7525

Please sign in to comment.