Checking if string is Encodeable In Gsm0338

Viewed 1144

I am working on an SMS application and I send out Unicode characters (Amharic/G'eez). I am using this example. The method on line 240, isEncodeableInGsm0338(), is used to check if I should use another encoding or the default encoding.

Here is the catch. The string "የእንግሊዝ ፕሪምየር ሊግ ነህሴ 6 ይጀምራል።", which is clearly Unicode, is returning from that method. My hypothesis being half of each letter. But I can't support that theory. If I change the text to "1. የእንግሊዝ ፕሪምየር ሊግ ነህሴ 6 ይጀምራል።", it detects correctly.

What is happening here?

2 Answers

Here is a version of mine. It's 20 times faster on 16 char strings than the nested loop, also faster than regexp, HashSet and tableswitch (by some reason).

public class Gsm0338 {

private static final char[] BASIC_CHARS = {
        // Basic Character Set
        '@', '£', '$', '¥', 'è', 'é', 'ù', 'ì', 'ò', 'Ç', '\n', 'Ø', 'ø', '\r', 'Å', 'å',
        'Δ', '_', 'Φ', 'Γ', 'Λ', 'Ω', 'Π', 'Ψ', 'Σ', 'Θ', 'Ξ', 'Æ', 'æ', 'ß', 'É',
        ' ', '!', '"', '#', '¤', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
        '¡', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'Ä', 'Ö', 'Ñ', 'Ü', '§',
        '¿', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ä', 'ö', 'ñ', 'ü', 'à',
        // Basic Character Set Extension
        '\f', '^', '{', '}', '\\', '[', '~', ']', '|', '€'
};

private static boolean[] ENCODEABLE_BY_ORD_UP_TO_253 = new boolean[254];
private static Set<Character> ENCODEABLE_REST = new HashSet<>();
static {
    for (int i = 0; i < BASIC_CHARS.length; i++) {
        char ch = BASIC_CHARS[i];
        if (ch <= 253) {
            ENCODEABLE_BY_ORD_UP_TO_253[ch] = true;
        } else {
            ENCODEABLE_REST.add(ch);
        }
    }
}

public static boolean isEncodeable(String javaString) {
    final int length = javaString.length();
    for (int i = 0; i < length; ++i) {
        char ch = javaString.charAt(i);
        if (ch <= 253) {
            if (!ENCODEABLE_BY_ORD_UP_TO_253[ch]) {
                return false;
            }
        } else {
            if (!ENCODEABLE_REST.contains(ch)) {
                return false;
            }
        }
    }
    return true;
}
}
Related