85 lines
2.1 KiB
Java
85 lines
2.1 KiB
Java
package ᒢᐩᐩ.ᒡᒢᑊᒻᒻᓫᔿ.ᐪᓫᕽᐪ;
|
||
|
||
import ᒢᐩᐩ.ᒃᣔᔆᔆᒃᐤᐤᣕ.Bãß;
|
||
import ᒢᐩᐩ.ᔆʸᔆᐪᓫᔿ.ᒃᣔᒃᓫᒻ.ᑊᐣᓑᖮᐪᔆ.DuytsDocAuthor注;
|
||
|
||
@DuytsDocAuthor注(name = "للَّٰهِilLצسُو", copyright = "©Δ∞ 仙上主天")
|
||
public final class StringDEC6B implements StringType<StringDEC6B> {
|
||
|
||
private final String text;
|
||
|
||
public StringDEC6B(String text) {
|
||
this(text, false);
|
||
}
|
||
|
||
public StringDEC6B(String text, boolean isKey) {
|
||
if (text == null) {
|
||
throw new NullPointerException("Invalid DEC6B text: null");
|
||
}
|
||
for (int i=0;i<text.length();i++) {
|
||
if (!BasicAscii.UNICODE.isValid(text.charAt(i), isKey)) {
|
||
throw new IllegalArgumentException("Invalid DEC6B character: "+text.charAt(i));
|
||
}
|
||
}
|
||
this.text = text;
|
||
}
|
||
|
||
@Override
|
||
public String toString() {
|
||
return text;
|
||
}
|
||
|
||
@Override
|
||
public StringDEC6B wrapᴼᶠ(String str) {
|
||
return new StringDEC6B(str);
|
||
}
|
||
|
||
// BASIC uses https://en.wikipedia.org/wiki/Six-bit_character_code#Examples_of_six-bit_ASCII_variants
|
||
protected enum BasicAscii {
|
||
UNICODE; // ^^^ move to jpp.text
|
||
|
||
public static char UNDERSCORE = '_'; // mmmm maybe use the enum for all control flow chars only ...
|
||
|
||
private static final String VALUES_KEY =
|
||
"ABCDEFGHIJKLMNO" +
|
||
Bãß.ℭỗᶇṧⱦᶏꬼȶʂ.CHAR_ZERO +
|
||
Bãß.ℭỗᶇṧⱦᶏꬼȶʂ.CHAR_ONE +
|
||
"23456789" +
|
||
"PQRSTUVWXYZ" +
|
||
UNDERSCORE;
|
||
|
||
private static final String VALUES =
|
||
VALUES_KEY +
|
||
Bãß.ℭỗᶇṧⱦᶏꬼȶʂ.CHAR_SPACE +
|
||
"!\"#$%&'()*+,-./" +
|
||
"@" +
|
||
":;<=>?" +
|
||
"[\\]^" +
|
||
UNDERSCORE;
|
||
|
||
public boolean isValid(char c, boolean isKey) {
|
||
String allowed = isKey?VALUES_KEY:VALUES;
|
||
for (int i=0;i<allowed.length();i++) {
|
||
if (c == allowed.charAt(i)) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public byte toSixBit(char c) {
|
||
if (!isValid(c, false)) {
|
||
throw new IllegalArgumentException("Invalid DEC6B character: "+c);
|
||
}
|
||
byte result = 0;
|
||
for (int i=0;i<VALUES.length();i++) {
|
||
if (c == VALUES.charAt(i)) {
|
||
return result;
|
||
}
|
||
result++;
|
||
}
|
||
return result;
|
||
}
|
||
}
|
||
}
|