Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[fanfix.git] / src / jexer / bits / StringUtils.java
index f5e2d47362a62e9c76bc0f731141a7cf43810d62..fffce206875cf663480d2041aac121f88b58d01a 100644 (file)
@@ -449,7 +449,8 @@ public class StringUtils {
                 || ((ch >= 0xffe0) && (ch <= 0xffe6))
                 || ((ch >= 0x20000) && (ch <= 0x2fffd))
                 || ((ch >= 0x30000) && (ch <= 0x3fffd))
-                // TODO: emoji / twemoji
+                // emoji
+                || ((ch >= 0x1f004) && (ch <= 0x1fffd))
             )
         ) {
             return 2;
@@ -466,10 +467,32 @@ public class StringUtils {
      */
     public static int width(final String str) {
         int n = 0;
-        for (int i = 0; i < str.length(); i++) {
-            n += width(str.charAt(i));
+        for (int i = 0; i < str.length();) {
+            int ch = str.codePointAt(i);
+            n += width(ch);
+            i += Character.charCount(ch);
         }
         return n;
     }
 
+    /**
+     * Check if character is in the CJK range.
+     *
+     * @param ch character to check
+     * @return true if this character is in the CJK range
+     */
+    public static boolean isCjk(final int ch) {
+        return ((ch >= 0x2e80) && (ch <= 0x9fff));
+    }
+
+    /**
+     * Check if character is in the emoji range.
+     *
+     * @param ch character to check
+     * @return true if this character is in the emoji range
+     */
+    public static boolean isEmoji(final int ch) {
+        return ((ch >= 0x1f004) && (ch <= 0x1fffd));
+    }
+
 }