public final class IranSystemUtil {
private static final int HAMZE = 0;
private static final int AA = 1;
private static final int ALEF = 2;
private static final int BE = 3;
private static final int PE = 4;
private static final int TE = 5;
private static final int SE = 6;
private static final int JIM = 7;
private static final int CHE = 8;
private static final int HE = 9;
private static final int KHE = 10;
private static final int DAL = 11;
private static final int ZAL = 12;
private static final int RE = 13;
private static final int ZE = 14;
private static final int JE = 15;
private static final int SIN = 16;
private static final int SHIN = 17;
private static final int SAD = 18;
private static final int ZAD = 19;
private static final int TA = 20;
private static final int ZA = 21;
private static final int EIN = 22;
private static final int GHEIN = 23;
private static final int FA = 24;
private static final int GHAF = 25;
private static final int KAF = 26;
private static final int GAF = 27;
private static final int LAM = 28;
private static final int MIM = 29;
private static final int NOON = 30;
private static final int VAV = 31;
private static final int H = 32;
private static final int YE = 33;
private static final int ZERO = 34;
private static final int ONE = 35;
private static final int TWO = 36;
private static final int THREE = 37;
private static final int FOUR = 38;
private static final int FIVE = 39;
private static final int SIX = 40;
private static final int SEVEN = 41;
private static final int EIGHT = 42;
private static final int NINE = 43;
private static final int QM = 44;
private static final int SPACE = 45;
private static final int LA = 46;
private static final int VIRGOOL = 47;
code
Assistant
private static final int JLA = 0;
private static final int JLR = 1;
private static final int JLM = 2;
private static final int JLL = 3;
// Join bytes: {ALONE, RIGHT, MID, LEFT}
private static final byte[][] join_map = {
{(byte) 0x8f, (byte) 0x8f, (byte) 0x8e, (byte) 0x8e}, // HAMZE
{(byte) 0x8d, (byte) 0x8d, (byte) 0x8d, (byte) 0x8d}, // AA
{(byte) 0x90, (byte) 0x91, (byte) 0x90, (byte) 0x90}, // ALEF
{(byte) 0x92, (byte) 0x92, (byte) 0x93, (byte) 0x93}, // BE
{(byte) 0x94, (byte) 0x94, (byte) 0x95, (byte) 0x95}, // PE
{(byte) 0x96, (byte) 0x96, (byte) 0x97, (byte) 0x97}, // TE
{(byte) 0x98, (byte) 0x98, (byte) 0x99, (byte) 0x99}, // SE
{(byte) 0x9a, (byte) 0x9a, (byte) 0x9b, (byte) 0x9b}, // JIM
{(byte) 0x9c, (byte) 0x9c, (byte) 0x9d, (byte) 0x9d}, // CHE
{(byte) 0x9e, (byte) 0x9e, (byte) 0x9f, (byte) 0x9f}, // HE
{(byte) 0xa0, (byte) 0xa0, (byte) 0xa1, (byte) 0xa1}, // KHE
{(byte) 0xa2, (byte) 0xa2, (byte) 0xa2, (byte) 0xa2}, // DAL
{(byte) 0xa3, (byte) 0xa3, (byte) 0xa3, (byte) 0xa3}, // ZAL
{(byte) 0xa4, (byte) 0xa4, (byte) 0xa4, (byte) 0xa4}, // RE
{(byte) 0xa5, (byte) 0xa5, (byte) 0xa5, (byte) 0xa5}, // ZE
{(byte) 0xa6, (byte) 0xa6, (byte) 0xa6, (byte) 0xa6}, // JE
{(byte) 0xa7, (byte) 0xa7, (byte) 0xa8, (byte) 0xa8}, // SIN
{(byte) 0xa9, (byte) 0xa9, (byte) 0xaa, (byte) 0xaa}, // SHIN
{(byte) 0xab, (byte) 0xab, (byte) 0xac, (byte) 0xac}, // SAD
{(byte) 0xad, (byte) 0xad, (byte) 0xae, (byte) 0xae}, // ZAD
{(byte) 0xaf, (byte) 0xaf, (byte) 0xaf, (byte) 0xaf}, // TA
{(byte) 0xe0, (byte) 0xe0, (byte) 0xe0, (byte) 0xe0}, // ZA
{(byte) 0xe1, (byte) 0xe2, (byte) 0xe3, (byte) 0xe4}, // EIN
{(byte) 0xe5, (byte) 0xe6, (byte) 0xe7, (byte) 0xe8}, // GHEIN
{(byte) 0xe9, (byte) 0xe9, (byte) 0xea, (byte) 0xea}, // FA
{(byte) 0xeb, (byte) 0xeb, (byte) 0xec, (byte) 0xec}, // GHAF
{(byte) 0xed, (byte) 0xed, (byte) 0xee, (byte) 0xee}, // KAF
{(byte) 0xef, (byte) 0xef, (byte) 0xf0, (byte) 0xf0}, // GAF
{(byte) 0xf1, (byte) 0xf1, (byte) 0xf3, (byte) 0xf3}, // LAM
{(byte) 0xf4, (byte) 0xf4, (byte) 0xf5, (byte) 0xf5}, // MIM
{(byte) 0xf6, (byte) 0xf6, (byte) 0xf7, (byte) 0xf7}, // NOON
{(byte) 0xf8, (byte) 0xf8, (byte) 0xf8, (byte) 0xf8}, // VAV
{(byte) 0xf9, (byte) 0xf9, (byte) 0xfa, (byte) 0xfb}, // H
{(byte) 0xfd, (byte) 0xfc, (byte) 0xfe, (byte) 0xfe}, // YE
{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80}, // ZERO
{(byte) 0x81, (byte) 0x81, (byte) 0x81, (byte) 0x81}, // ONE
{(byte) 0x82, (byte) 0x82, (byte) 0x82, (byte) 0x82}, // TWO
{(byte) 0x83, (byte) 0x83, (byte) 0x83, (byte) 0x83}, // THREE
{(byte) 0x84, (byte) 0x84, (byte) 0x84, (byte) 0x84}, // FOUR
{(byte) 0x85, (byte) 0x85, (byte) 0x85, (byte) 0x85}, // FIVE
{(byte) 0x86, (byte) 0x86, (byte) 0x86, (byte) 0x86}, // SIX
{(byte) 0x87, (byte) 0x87, (byte) 0x87, (byte) 0x87}, // SEVEN
{(byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88}, // EIGHT
{(byte) 0x89, (byte) 0x89, (byte) 0x89, (byte) 0x89}, // NINE
{(byte) 0x8c, (byte) 0x8c, (byte) 0x8c, (byte) 0x8c}, // QM
{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}, // SPACE
{(byte) 0xf2, (byte) 0xf2, (byte) 0xf2, (byte) 0xf2}, // LA
{(byte) 0x8a, (byte) 0x8a, (byte) 0x8a, (byte) 0x8a}, // VIRGOOL
};
private static final int JMR = 0;
private static final int JMML = 1;
// Join modes: {RIGHT, MID_LEFT}
private static final boolean[][] join_modes = {
{false, true}, // HAMZE
{false, false}, // AA
{true, false}, // ALEF
{true, true}, // BE
{true, true}, // PE
{true, true}, // TE
{true, true}, // SE
{true, true}, // JIM
{true, true}, // CHE
{true, true}, // HE
{true, true}, // KHE
{true, false}, // DAL
{true, false}, // ZAL
{true, false}, // RE
{true, false}, // ZE
{true, false}, // JE
{true, true}, // SIN
{true, true}, // SHIN
{true, true}, // SAD
{true, true}, // ZAD
{true, true}, // TA
{true, true}, // ZA
{true, true}, // EIN
{true, true}, // GHEIN
{true, true}, // FA
{true, true}, // GHAF
{true, true}, // KAF
{true, true}, // GAF
{true, true}, // LAM
{true, true}, // MIM
{true, true}, // NOON
{true, false}, // VAV
{true, true}, // H
{true, true}, // YE
{false, false}, // ZERO
{false, false}, // ONE
{false, false}, // TWO
{false, false}, // THREE
{false, false}, // FOUR
{false, false}, // FIVE
{false, false}, // SIX
{false, false}, // SEVEN
{false, false}, // EIGHT
{false, false}, // NINE
{false, false}, // QM
{false, false}, // SPACE
{true, false}, // LA
{false, false}, // VIRGOOL
};
private static final char[] table = {
/* 0 */ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '،', 'ـ', '؟', 'آ', 'ئ', 'ء',
/* 16 */ 'ا', 'ا', 'ب', 'ب', 'پ', 'پ', 'ت', 'ت', 'ث', 'ث', 'ج', 'ج', 'چ', 'چ', 'ح', 'ح',
/* 32 */ 'خ', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'س', 'ش', 'ش', 'ص', 'ص', 'ض', 'ض', 'ط',
/* 48 */ 'ظ', 'ع', 'ع', 'ع', 'ع', 'غ', 'غ', 'غ', 'غ', 'ف', 'ف', 'ق', 'ق', 'ک', 'ک', 'گ',
/* 64 */ 'گ', 'ل', 'ل', 'ل', 'م', 'م', 'ن', 'ن', 'و', 'ه', 'ه', 'ه', 'ی', 'ی', 'ی', ' ',
/* 80 */
/* 0 */ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '،', 'ـ', '؟', 'آ', 'ﺋ', 'ء',
/* 16 */ 'ﺍ', 'ﺎ', 'ب', 'ﺑ', 'پ', 'ﭘ', 'ت', 'ﺗ', 'ث', 'ﺛ', 'ج', 'ﺟ', 'چ', 'ﭼ', 'ح', 'ﺣ',
/* 32 */ 'خ', 'ﺧ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ﺳ', 'ش', 'ﺷ', 'ص', 'ﺻ', 'ض', 'ﺿ', 'ط',
/* 48 */ 'ظ', 'ع', 'ﻊ', 'ﻌ', 'ﻋ', 'غ', 'ﻎ', 'ﻐ', 'ﻏ', 'ف', 'ﻓ', 'ق', 'ﻗ', 'ک', 'ﮐ', 'گ',
/* 64 */ 'ﮔ', 'ل', 'ﻟ', 'ﻟ', 'م', 'ﻣ', 'ن', 'ﻧ', 'و', 'ه', 'ﻬ', 'ﻫ', 'ﯽ', 'ی', 'ﯾ', ' ',
/* 80 */
};
private IranSystemUtil() {
// since this class is a utility class so it must be singleton due to memory consumption.
}
private static int unicode_internal_index(int i) {
if (i >= 0x660 && i <= 0x669)
return ZERO + i - 0x660;
if (i >= 0x6F0 && i <= 0x6F9)
return ZERO + i - 0x6F0;
if (i >= 0x30 && i <= 0x39)
return ZERO + i - 0x30;
switch (i) {
case 0x2c:
case 0x60C:
case 0x7F8:
return VIRGOOL;
case 0x20:
return SPACE;
case 0x61f:
case 0x3f:
return QM;
case 0x620:
case 0x626:
case 0x63d:
case 0x63e:
case 0x63f:
case 0x649:
case 0x64a:
case 0x678:
case 0x6cc:
case 0x6cd:
case 0x6ce:
case 0x6d0:
case 0x6d1:
case 0x6d2:
case 0x6d3:
case 0x775:
case 0x776:
case 0x777:
case 0x77A:
case 0x77B:
case 0x8A8:
case 0x8A9:
case 0x8BA:
return YE;
case 0x621:
case 0x6FD:
return HAMZE;
case 0x622:
return AA;
case 0x623:
case 0x625:
case 0x627:
case 0x773:
case 0x774:
return ALEF;
case 0x624:
case 0x648:
case 0x676:
case 0x677:
case 0x6c4:
case 0x6c5:
case 0x6c6:
case 0x6c7:
case 0x6c8:
case 0x6c9:
case 0x6ca:
case 0x6cb:
case 0x6cf:
case 0x778:
case 0x779:
return VAV;
case 0x628:
case 0x67b:
case 0x680:
case 0x750:
case 0x751:
case 0x752:
case 0x753:
case 0x754:
case 0x755:
case 0x756:
case 0x8A0:
case 0x8A1:
case 0x8B6:
return BE;
case 0x629:
case 0x647:
case 0x6be:
case 0x6d5:
case 0x6FF:
return H;
case 0x62a:
case 0x679:
case 0x67a:
case 0x67c:
case 0x67d:
case 0x67F:
case 0x8B8:
return TE;
case 0x62b:
return SE;
case 0x62c:
case 0x8A2:
return JIM;
case 0x6bf:
case 0x686:
case 0x687:
return CHE;
case 0x62d:
case 0x681:
case 0x682:
case 0x683:
case 0x684:
case 0x685:
case 0x6c0:
case 0x6c1:
case 0x6c2:
case 0x6c3:
case 0x757:
case 0x758:
case 0x76E:
case 0x76F:
case 0x772:
case 0x77C:
return HE;
case 0x62e:
return KHE;
case 0x62f:
case 0x688:
case 0x689:
case 0x68a:
case 0x68b:
case 0x68c:
case 0x68d:
case 0x68e:
case 0x68f:
case 0x690:
case 0x6ee:
case 0x759:
case 0x75a:
case 0x8AE:
return DAL;
case 0x630:
return ZAL;
case 0x631:
case 0x691:
case 0x692:
case 0x693:
case 0x694:
case 0x695:
case 0x696:
case 0x697:
case 0x699:
case 0x6EF:
case 0x75B:
case 0x76B:
case 0x76c:
case 0x771:
case 0x8B9:
return RE;
case 0x698:
return JE;
case 0x632:
case 0x8B2:
return ZE;
case 0x633:
case 0x69a:
case 0x69b:
case 0x69c:
case 0x75C:
case 0x76D:
case 0x770:
case 0x77D:
case 0x77E:
return SIN;
case 0x634:
case 0x6FA:
return SHIN;
case 0x635:
case 0x69d:
case 0x69e:
case 0x8AF:
return SAD;
case 0x636:
case 0x6FB:
return ZAD;
case 0x637:
case 0x69f:
case 0x8A3:
return TA;
case 0x638:
return ZA;
case 0x639:
case 0x6a0:
case 0x75D:
case 0x75e:
case 0x75f:
case 0x8B3:
return EIN;
case 0x63a:
case 0x6FC:
return GHEIN;
case 0x63b:
case 0x63c:
case 0x643:
case 0x6a9:
case 0x6aa:
case 0x6ab:
case 0x6ac:
case 0x6ad:
case 0x6ae:
case 0x762:
case 0x763:
case 0x764:
case 0x77F:
case 0x8B4:
return KAF;
case 0x641:
case 0x6A1:
case 0x6A2:
case 0x6A3:
case 0x6A4:
case 0x6A5:
case 0x6A6:
case 0x6A7:
case 0x760:
case 0x761:
case 0x8A4:
case 0x8BB:
return FA;
case 0x642:
case 0x6A8:
case 0x8A5:
case 0x8BC:
return GHAF;
case 0x8b0:
case 0x6b0:
case 0x6b1:
case 0x6b2:
case 0x6b3:
case 0x6b4:
case 0x6af:
return GAF;
case 0x644:
case 0x6b5:
case 0x6b6:
case 0x6b7:
case 0x6b8:
case 0x76A:
case 0x8A6:
return LAM;
case 0x645:
case 0x765:
case 0x766:
case 0x8A7:
return MIM;
case 0x646:
case 0x6b9:
case 0x6ba:
case 0x6bb:
case 0x6bc:
case 0x6bd:
case 0x767:
case 0x768:
case 0x769:
case 0x8BD:
return NOON;
case 0x67e:
case 0x8B7:
return PE;
default:
return -1;
}
}
private static int w1256_internal_index(int i) {
if (i >= 0x30 && i <= 0x39)
return ZERO + i - 0x30;
switch (i) {
case 0x2c:
case 0x82:
return VIRGOOL;
case 0x20:
return SPACE;
case 0x3f:
case 0xbf:
return QM;
case 0xc6:
case 0xec:
case 0xed:
return YE;
case 0xc1:
return HAMZE;
case 0xc2:
return AA;
case 0xc3:
case 0xc5:
case 0xc7:
return ALEF;
case 0xc4:
return VAV;
case 0xc8:
return BE;
case 0xc0:
case 0xc9:
return H;
case 0x8a:
case 0xca:
return TE;
case 0xcb:
return SE;
case 0xcc:
return JIM;
case 0x8d:
return CHE;
case 0xcd:
return HE;
case 0xce:
return KHE;
case 0x8f:
case 0xcf:
return DAL;
case 0xd0:
return ZAL;
case 0x9a:
case 0xd1:
return RE;
case 0x8e:
return JE;
case 0xd2:
return ZE;
case 0xd3:
return SIN;
case 0xd4:
return SHIN;
case 0xd5:
return SAD;
case 0xd6:
return ZAD;
case 0xd8:
return TA;
case 0xd9:
return ZA;
case 0xda:
return EIN;
case 0xdb:
return GHEIN;
case 0x98:
return KAF;
case 0xdd:
return FA;
case 0xde:
return GHAF;
case 0x90:
return GAF;
case 0xe1:
return LAM;
case 0xe3:
return MIM;
case 0xe4:
return NOON;
case 0x81:
return PE;
default:
return -1;
}
}
private boolean get_join_mode(int letter, int align) {
return (unicode_internal_index(letter) != -1) && join_modes[letter][align];
}
private static byte[] process_buffer(int[] letters, int offset, int length) {
byte[] res = new byte[length];
if (length == 1) {
res[0] = join_map[letters[offset]][JLA];
return res;
}
int previousLetter = letters[offset + length - 1];
int currentLetter;
int nextLetter;
boolean start = true;
for (int i = length - 2; i >= 1; --i) {
currentLetter = letters[i + offset];
nextLetter = letters[i + offset - 1];
if (start) {
if (join_modes[currentLetter][JMML] || join_modes[currentLetter][JMR]) {
res[i + 1] = join_map[previousLetter][JLL];
} else {
res[i + 1] = join_map[previousLetter][JLA];
}
start = false;
}
if (join_modes[previousLetter][JMML]) {
if (join_modes[nextLetter][JMR] || join_modes[nextLetter][JMML]) { // <-> ? <-
if (join_modes[currentLetter][JMML]) {
res[i] = join_map[currentLetter][JLM];
} else if (join_modes[currentLetter][JMR]) {
res[i] = join_map[currentLetter][JLR];
} else {
res[i] = join_map[currentLetter][JLA];
}
} else { // - ? <-
if (join_modes[currentLetter][JMR]) {
res[i] = join_map[currentLetter][JLR];
} else {
res[i] = join_map[currentLetter][JLA];
}
}
} else if (join_modes[nextLetter][JMML]) {
res[i] = join_map[currentLetter][JLL];
} else if (join_modes[nextLetter][JMR] && join_modes[currentLetter][JMML]) {
res[i] = join_map[currentLetter][JLL];
} else {
res[i] = join_map[currentLetter][JLA];
}
previousLetter = currentLetter;
}
if (start) { // two letters length
currentLetter = letters[1 + offset];
if (join_modes[currentLetter][JMML] || join_modes[currentLetter][JMR]) {
res[1] = join_map[previousLetter][JLL];
} else {
res[1] = join_map[previousLetter][JLA];
}
}
currentLetter = letters[offset];
if (join_modes[currentLetter][JMR]) {
if (join_modes[previousLetter][JMML]) {
res[0] = join_map[currentLetter][JLR];
} else {
res[0] = join_map[currentLetter][JLA];
}
} else {
res[0] = join_map[currentLetter][JLA];
}
return res;
}
public static String toString(byte[] data) {
return toString(data, 0, data.length);
}
public static String toString(byte[] data, int offset, int length) {
if (length == 0 || data.length < offset + length) {
return "";
}
int j = length;
for (int i = offset; i < offset + length; ++i) {
if (data[i] == (byte) 0xF2) {
j += 1;
}
}
char[] temp = new char[j];
byte t;
j = 0;
for (int i = offset; i < offset + length; ++i) {
if (data[i] == 0x20) {
temp[temp.length - j++ - 1] = table[79];
continue;
}
if (data[i] >= 0) {
temp[temp.length - j++ - 1] = (char) data[i];
} else {
t = (byte) (128 + data[i]);
if (t < 96 && t >= 48) {
temp[temp.length - j++ - 1] = table[79];
continue;
}
if (t >= 96) {
t -= 48;
}
if (t == 66) {
temp[temp.length - j++ - 1] = table[16];
}
temp[temp.length - j++ - 1] = table[t];
}
}
return new String(temp);
}
public static byte[] fromString(String data, boolean reverse) {
int[] letters = new int[data.length()];
int letters_length = 0;
int letter = -1;
boolean is_last_space = false;
for (int i = 0; i < data.length(); ++i) {
letter = unicode_internal_index(data.charAt(i));
if (letter == -1) {
continue;
}
if (letter == SPACE) {
if (!is_last_space) {
is_last_space = true;
if (letters_length != 0) {
letters[letters_length++] = letter;
}
}
continue;
} else {
is_last_space = false;
}
if ((letter == ALEF || letter == AA) && letters_length > 0 && letters[letters_length - 1] == LAM) {
letters[letters_length - 1] = LA;
} else {
letters[letters_length++] = letter;
}
}
if (letter == SPACE) {
--letters_length;
}
for (int i = 0, t, k = letters_length / 2; i < k; i++) {
t = letters[i];
letters[i] = letters[letters_length - i - 1];
letters[letters_length - i - 1] = t;
}
return (letters_length > 0) ?
(reverse ? reverse(process_buffer(letters, 0, letters_length)) :
process_buffer(letters, 0, letters_length)) : null;
}
public static byte[] fromStringMultiPart(String data, boolean reverse) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
int index = 0;
boolean persian = false;
for (int i = 0; i < data.length(); ++i) {
if (unicode_internal_index(data.charAt(i)) != -1 && data.charAt(i) != ' ') {
persian = true;
} else {
if (persian && i != 0) {
persian = false;
try {
out.write(fromString(data.substring(index, i), !reverse));
index = i;
} catch (IOException e) {
}
}
out.write(data.charAt(i));
}
}
if (index != data.length() && persian) {
try {
out.write(fromString(data.substring(index), !reverse));
} catch (IOException e) {
}
}
return reverse ? out.toByteArray() : reverse(out.toByteArray());
}
private static byte[] reverse(byte[] a) {
if (a == null) {
return null;
} else {
int p1 = 0;
int p2 = a.length;
byte[] result = new byte[p2];
while(true) {
--p2;
if (p2 < 0) {
return result;
}
result[p2] = a[p1++];
}
}
}
public static void main(String[] args) {
// String sample = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم از صنعت چاپ و با استفاده از طراحان گرافیک است. چاپگرها و متون بلکه روزنامه و مجله در ستون و سطرآنچنان که لازم است و برای شرایط فعلی تکنولوژی مورد نیاز و کاربردهای متنوع با هدف بهبود ابزارهای کاربردی می باشد. کتابهای زیادی در شصت و سه درصد گذشته، حال و آینده شناخت فراوان جامعه و متخصصان را می طلبد تا با نرم افزارها شناخت بیشتری را برای طراحان رایانه ای علی الخصوص طراحان خلاقی و فرهنگ پیشرو در زبان فارسی ایجاد کرد. در این صورت می توان امید داشت که تمام و دشواری موجود در ارائه راهکارها و شرایط سخت تایپ به پایان رسد وزمان مورد نیاز شامل حروفچینی دستاوردهای اصلی و جوابگوی سوالات پیوسته اهل دنیای موجود طراحی اساسا مورد استفاده قرار گیرد";
// byte[] res = fromStringMultiPart(sample, false);
// System.out.println(toString(res, 0, res.length));
}
}
Here's the converted Java class to C#. The logic and structure are kept as is, only adapted to C# syntax and idioms:
csharp
using System;
using System.IO;
using System.Text;
public static class IranSystemUtil
{
private const int HAMZE = 0;
private const int AA = 1;
private const int ALEF = 2;
private const int BE = 3;
private const int PE = 4;
private const int TE = 5;
private const int SE = 6;
private const int JIM = 7;
private const int CHE = 8;
private const int HE = 9;
private const int KHE = 10;
private const int DAL = 11;
private const int ZAL = 12;
private const int RE = 13;
private const int ZE = 14;
private const int JE = 15;
private const int SIN = 16;
private const int SHIN = 17;
private const int SAD = 18;
private const int ZAD = 19;
private const int TA = 20;
private const int ZA = 21;
private const int EIN = 22;
private const int GHEIN = 23;
private const int FA = 24;
private const int GHAF = 25;
private const int KAF = 26;
private const int GAF = 27;
private const int LAM = 28;
private const int MIM = 29;
private const int NOON = 30;
private const int VAV = 31;
private const int H = 32;
private const int YE = 33;
private const int ZERO = 34;
private const int ONE = 35;
private const int TWO = 36;
private const int THREE = 37;
private const int FOUR = 38;
private const int FIVE = 39;
private const int SIX = 40;
private const int SEVEN = 41;
private const int EIGHT = 42;
private const int NINE = 43;
private const int QM = 44;
private const int SPACE = 45;
private const int LA = 46;
private const int VIRGOOL = 47;
private const int JLA = 0;
private const int JLR = 1;
private const int JLM = 2;
private const int JLL = 3;
// Join bytes: {ALONE, RIGHT, MID, LEFT}
private static readonly byte[][] join_map = {
new byte[] { 0x8f, 0x8f, 0x8e, 0x8e }, // HAMZE
new byte[] { 0x8d, 0x8d, 0x8d, 0x8d }, // AA
new byte[] { 0x90, 0x91, 0x90, 0x90 }, // ALEF
new byte[] { 0x92, 0x92, 0x93, 0x93 }, // BE
new byte[] { 0x94, 0x94, 0x95, 0x95 }, // PE
new byte[] { 0x96, 0x96, 0x97, 0x97 }, // TE
new byte[] { 0x98, 0x98, 0x99, 0x99 }, // SE
new byte[] { 0x9a, 0x9a, 0x9b, 0x9b }, // JIM
new byte[] { 0x9c, 0x9c, 0x9d, 0x9d }, // CHE
new byte[] { 0x9e, 0x9e, 0x9f, 0x9f }, // HE
new byte[] { 0xa0, 0xa0, 0xa1, 0xa1 }, // KHE
new byte[] { 0xa2, 0xa2, 0xa2, 0xa2 }, // DAL
new byte[] { 0xa3, 0xa3, 0xa3, 0xa3 }, // ZAL
new byte[] { 0xa4, 0xa4, 0xa4, 0xa4 }, // RE
new byte[] { 0xa5, 0xa5, 0xa5, 0xa5 }, // ZE
new byte[] { 0xa6, 0xa6, 0xa6, 0xa6 }, // JE
new byte[] { 0xa7, 0xa7, 0xa8, 0xa8 }, // SIN
new byte[] { 0xa9, 0xa9, 0xaa, 0xaa }, // SHIN
new byte[] { 0xab, 0xab, 0xac, 0xac }, // SAD
new byte[] { 0xad, 0xad, 0xae, 0xae }, // ZAD
new byte[] { 0xaf, 0xaf, 0xaf, 0xaf }, // TA
new byte[] { 0xe0, 0xe0, 0xe0, 0xe0 }, // ZA
new byte[] { 0xe1, 0xe2, 0xe3, 0xe4 }, // EIN
new byte[] { 0xe5, 0xe6, 0xe7, 0xe8 }, // GHEIN
new byte[] { 0xe9, 0xe9, 0xea, 0xea }, // FA
new byte[] { 0xeb, 0xeb, 0xec, 0xec }, // GHAF
new byte[] { 0xed, 0xed, 0xee, 0xee }, // KAF
new byte[] { 0xef, 0xef, 0xf0, 0xf0 }, // GAF
new byte[] { 0xf1, 0xf1, 0xf3, 0xf3 }, // LAM
new byte[] { 0xf4, 0xf4, 0xf5, 0xf5 }, // MIM
new byte[] { 0xf6, 0xf6, 0xf7, 0xf7 }, // NOON
new byte[] { 0xf8, 0xf8, 0xf8, 0xf8 }, // VAV
new byte[] { 0xf9, 0xf9, 0xfa, 0xfb }, // H
new byte[] { 0xfd, 0xfc, 0xfe, 0xfe }, // YE
new byte[] { 0x80, 0x80, 0x80, 0x80 }, // ZERO
new byte[] { 0x81, 0x81, 0x81, 0x81 }, // ONE
new byte[] { 0x82, 0x82, 0x82, 0x82 }, // TWO
new byte[] { 0x83, 0x83, 0x83, 0x83 }, // THREE
new byte[] { 0x84, 0x84, 0x84, 0x84 }, // FOUR
new byte[] { 0x85, 0x85, 0x85, 0x85 }, // FIVE
new byte[] { 0x86, 0x86, 0x86, 0x86 }, // SIX
new byte[] { 0x87, 0x87, 0x87, 0x87 }, // SEVEN
new byte[] { 0x88, 0x88, 0x88, 0x88 }, // EIGHT
new byte[] { 0x89, 0x89, 0x89, 0x89 }, // NINE
new byte[] { 0x8c, 0x8c, 0x8c, 0x8c }, // QM
new byte[] { 0xff, 0xff, 0xff, 0xff }, // SPACE
new byte[] { 0xf2, 0xf2, 0xf2, 0xf2 }, // LA
new byte[] { 0x8a, 0x8a, 0x8a, 0x8a }, // VIRGOOL
};
private const int JMR = 0;
private const int JMML = 1;
// Join modes: {RIGHT, MID_LEFT}
private static readonly bool[][] join_modes = {
new bool[] { false, true }, // HAMZE
new bool[] { false, false }, // AA
new bool[] { true, false }, // ALEF
new bool[] { true, true }, // BE
new bool[] { true, true }, // PE
new bool[] { true, true }, // TE
new bool[] { true, true }, // SE
new bool[] { true, true }, // JIM
new bool[] { true, true }, // CHE
new bool[] { true, true }, // HE
new bool[] { true, true }, // KHE
new bool[] { true, false }, // DAL
new bool[] { true, false }, // ZAL
new bool[] { true, false }, // RE
new bool[] { true, false }, // ZE
new bool[] { true, false }, // JE
new bool[] { true, true }, // SIN
new bool[] { true, true }, // SHIN
new bool[] { true, true }, // SAD
new bool[] { true, true }, // ZAD
new bool[] { true, true }, // TA
new bool[] { true, true }, // ZA
new bool[] { true, true }, // EIN
new bool[] { true, true }, // GHEIN
new bool[] { true, true }, // FA
new bool[] { true, true }, // GHAF
new bool[] { true, true }, // KAF
new bool[] { true, true }, // GAF
new bool[] { true, true }, // LAM
new bool[] { true, true }, // MIM
new bool[] { true, true }, // NOON
new bool[] { true, false }, // VAV
new bool[] { true, true }, // H
new bool[] { true, true }, // YE
new bool[] { false, false }, // ZERO
new bool[] { false, false }, // ONE
new bool[] { false, false }, // TWO
new bool[] { false, false }, // THREE
new bool[] { false, false }, // FOUR
new bool[] { false, false }, // FIVE
new bool[] { false, false }, // SIX
new bool[] { false, false }, // SEVEN
new bool[] { false, false }, // EIGHT
new bool[] { false, false }, // NINE
new bool[] { false, false }, // QM
new bool[] { false, false }, // SPACE
new bool[] { true, false }, // LA
new bool[] { false, false }, // VIRGOOL
};
private static readonly char[] table = {
/* 0 */ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '،', 'ـ', '؟', 'آ', 'ئ', 'ء',
/* 16 */ 'ا', 'ا', 'ب', 'ب', 'پ', 'پ', 'ت', 'ت', 'ث', 'ث', 'ج', 'ج', 'چ', 'چ', 'ح', 'ح',
/* 32 */ 'خ', 'خ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'س', 'ش', 'ش', 'ص', 'ص', 'ض', 'ض', 'ط',
/* 48 */ 'ظ', 'ع', 'ع', 'ع', 'ع', 'غ', 'غ', 'غ', 'غ', 'ف', 'ف', 'ق', 'ق', 'ک', 'ک', 'گ',
/* 64 */ 'گ', 'ل', 'ل', 'ل', 'م', 'م', 'ن', 'ن', 'و', 'ه', 'ه', 'ه', 'ی', 'ی', 'ی', ' ',
/* 80 */
/* 0 */ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹', '،', 'ـ', '؟', 'آ', 'ﺋ', 'ء',
/* 16 */ 'ﺍ', 'ﺎ', 'ب', 'ﺑ', 'پ', 'ﭘ', 'ت', 'ﺗ', 'ث', 'ﺛ', 'ج', 'ﺟ', 'چ', 'ﭼ', 'ح', 'ﺣ',
/* 32 */ 'خ', 'ﺧ', 'د', 'ذ', 'ر', 'ز', 'ژ', 'س', 'ﺳ', 'ش', 'ﺷ', 'ص', 'ﺻ', 'ض', 'ﺿ', 'ط',
/* 48 */ 'ظ', 'ع', 'ﻊ', 'ﻌ', 'ﻋ', 'غ', 'ﻎ', 'ﻐ', 'ﻏ', 'ف', 'ﻓ', 'ق', 'ﻗ', 'ک', 'ﮐ', 'گ',
/* 64 */ 'ﮔ', 'ل', 'ﻟ', 'ﻟ', 'م', 'ﻣ', 'ن', 'ﻧ', 'و', 'ه', 'ﻬ', 'ﻫ', 'ﯽ', 'ی', 'ﯾ', ' ',
/* 80 */
};
private static int UnicodeInternalIndex(int i)
{
if (i >= 0x660 && i <= 0x669)
return ZERO + i - 0x660;
if (i >= 0x6F0 && i <= 0x6F9)
return ZERO + i - 0x6F0;
if (i >= 0x30 && i <= 0x39)
return ZERO + i - 0x30;
switch (i)
{
case 0x2c:
case 0x60C:
case 0x7F8:
return VIRGOOL;
case 0x20:
return SPACE;
case 0x61f:
case 0x3f:
return QM;
case 0x620:
case 0x626:
case 0x63d:
case 0x63e:
case 0x63f:
case 0x649:
case 0x64a:
case 0x678:
case 0x6cc:
case 0x6cd:
case 0x6ce:
case 0x6d0:
case 0x6d1:
case 0x6d2:
case 0x6d3:
case 0x775:
case 0x776:
case 0x777:
case 0x77A:
case 0x77B:
case 0x8A8:
case 0x8A9:
case 0x8BA:
return YE;
case 0x621:
case 0x6FD:
return HAMZE;
case 0x622:
return AA;
case 0x623:
case 0x625:
case 0x627:
case 0x773:
case 0x774:
return ALEF;
case 0x624:
case 0x648:
case 0x676:
case 0x677:
case 0x6c4:
case 0x6c5:
case 0x6c6:
case 0x6c7:
case 0x6c8:
case 0x6c9:
case 0x6ca:
case 0x6cb:
case 0x6cf:
case 0x778:
case 0x779:
return VAV;
case 0x628:
case 0x67b:
case 0x680:
case 0x750:
case 0x751:
case 0x752:
case 0x753:
case 0x754:
case 0x755:
case 0x756:
case 0x8A0:
case 0x8A1:
case 0x8B6:
return BE;
case 0x629:
case 0x647:
case 0x6be:
case 0x6d5:
case 0x6FF:
return H;
case 0x62a:
case 0x679:
case 0x67a:
case 0x67c:
case 0x67d:
case 0x67F:
case 0x8B8:
return TE;
case 0x62b:
return SE;
case 0x62c:
case 0x8A2:
return JIM;
case 0x6bf:
case 0x686:
case 0x687:
return CHE;
case 0x62d:
case 0x681:
case 0x682:
case 0x683:
case 0x684:
case 0x685:
case 0x6c0:
case 0x6c1:
case 0x6c2:
case 0x6c3:
case 0x757:
case 0x758:
case 0x76E:
case 0x76F:
case 0x772:
case 0x77C:
return HE;
case 0x62e:
return KHE;
case 0x62f:
case 0x688:
case 0x689:
case 0x68a:
case 0x68b:
case 0x68c:
case 0x68d:
case 0x68e:
case 0x68f:
case 0x690:
case 0x6ee:
case 0x759:
case 0x75a:
case 0x8AE:
return DAL;
case 0x630:
return ZAL;
case 0x631:
case 0x691:
case 0x692:
case 0x693:
case 0x694:
case 0x695:
case 0x696:
case 0x697:
case 0x699:
case 0x6EF:
case 0x75B:
case 0x76B:
case 0x76c:
case 0x771:
case 0x8B9:
return RE;
case 0x698:
return JE;
case 0x632:
case 0x8B2:
return ZE;
case 0x633:
case 0x69a:
case 0x69b:
case 0x69c:
case 0x75C:
case 0x76D:
case 0x770:
case 0x77D:
case 0x77E:
return SIN;
case 0x634:
case 0x6FA:
return SHIN;
case 0x635:
case 0x69d:
case 0x69e:
case 0x8AF:
return SAD;
case 0x636:
case 0x6FB:
return ZAD;
case 0x637:
case 0x69f:
case 0x8A3:
return TA;
case 0x638:
return ZA;
case 0x639:
case 0x6a0:
case 0x75D:
case 0x75e:
case 0x75f:
case 0x8B3:
return EIN;
case 0x63a:
case 0x6FC:
return GHEIN;
case 0x63b:
case 0x63c:
case 0x643:
case 0x6a9:
case 0x6aa:
case 0x6ab:
case 0x6ac:
case 0x6ad:
case 0x6ae:
case 0x762:
case 0x763:
case 0x764:
case 0x77F:
case 0x8B4:
return KAF;
case 0x641:
case 0x6A1:
case 0x6A2:
case 0x6A3:
case 0x6A4:
case 0x6A5:
case 0x6A6:
case 0x6A7:
case 0x760:
case 0x761:
case 0x8A4:
case 0x8BB:
return FA;
case 0x642:
case 0x6A8:
case 0x8A5:
case 0x8BC:
return GHAF;
case 0x8b0:
case 0x6b0:
case 0x6b1:
case 0x6b2:
case 0x6b3:
case 0x6b4:
case 0x6af:
return GAF;
case 0x644:
case 0x6b5:
case 0x6b6:
case 0x6b7:
case 0x6b8:
case 0x76A:
case 0x8A6:
return LAM;
case 0x645:
case 0x765:
case 0x766:
case 0x8A7:
return MIM;
case 0x646:
case 0x6b9:
case 0x6ba:
case 0x6bb:
case 0x6bc:
case 0x6bd:
case 0x767:
case 0x768:
case 0x769:
case 0x8BD:
return NOON;
case 0x67e:
case 0x8B7:
return PE;
default:
return -1;
}
}
private static int W1256InternalIndex(int i)
{
if (i >= 0x30 && i <= 0x39)
return ZERO + i - 0x30;
switch (i)
{
case 0x2c:
case 0x82:
return VIRGOOL;
case 0x20:
return SPACE;
case 0x3f:
case 0xbf:
return QM;
case 0xc6:
case 0xec:
case 0xed:
return YE;
case 0xc1:
return HAMZE;
case 0xc2:
return AA;
case 0xc3:
case 0xc5:
case 0xc7:
return ALEF;
case 0xc4:
return VAV;
case 0xc8:
return BE;
case 0xc0:
case 0xc9:
return H;
case 0x8a:
case 0xca:
return TE;
case 0xcb:
return SE;
case 0xcc:
return JIM;
case 0x8d:
return CHE;
case 0xcd:
return HE;
case 0xce:
return KHE;
case 0x8f:
case 0xcf:
return DAL;
case 0xd0:
return ZAL;
case 0x9a:
case 0xd1:
return RE;
case 0x8e:
return JE;
case 0xd2:
return ZE;
case 0xd3:
return SIN;
case 0xd4:
return SHIN;
case 0xd5:
return SAD;
case 0xd6:
return ZAD;
case 0xd8:
return TA;
case 0xd9:
return ZA;
case 0xda:
return EIN;
case 0xdb:
return GHEIN;
case 0x98:
return KAF;
case 0xdd:
return FA;
case 0xde:
return GHAF;
case 0x90:
return GAF;
case 0xe1:
return LAM;
case 0xe3:
return MIM;
case 0xe4:
return NOON;
case 0x81:
return PE;
default:
return -1;
}
}
private static bool GetJoinMode(int letter, int align)
{
return letter != -1 && join_modes[letter][align];
}
private static byte[] ProcessBuffer(int[] letters, int offset, int length)
{
byte[] res = new byte[length];
if (length == 1)
{
res[0] = join_map[letters[offset]][JLA];
return res;
}
int previousLetter = letters[offset + length - 1];
int currentLetter, nextLetter;
bool start = true;
for (int i = length - 2; i >= 1; --i)
{
currentLetter = letters[i + offset];
nextLetter = letters[i + offset - 1];
if (start)
{
if (join_modes[currentLetter][JMML] || join_modes[currentLetter][JMR])
{
res[i + 1] = join_map[previousLetter][JLL];
}
else
{
res[i + 1] = join_map[previousLetter][JLA];
}
start = false;
}
if (join_modes[previousLetter][JMML])
{
if (join_modes[nextLetter][JMR] || join_modes[nextLetter][JMML]) // <-> ? <-
{
if (join_modes[currentLetter][JMML])
{
res[i] = join_map[currentLetter][JLM];
}
else if (join_modes[currentLetter][JMR])
{
res[i] = join_map[currentLetter][JLR];
}
else
{
res[i] = join_map[currentLetter][JLA];
}
}
else // - ? <-
{
if (join_modes[currentLetter][JMR])
{
res[i] = join_map[currentLetter][JLR];
}
else
{
res[i] = join_map[currentLetter][JLA];
}
}
}
else if (join_modes[nextLetter][JMML])
{
res[i] = join_map[currentLetter][JLL];
}
else if (join_modes[nextLetter][JMR] && join_modes[currentLetter][JMML])
{
res[i] = join_map[currentLetter][JLL];
}
else
{
res[i] = join_map[currentLetter][JLA];
}
previousLetter = currentLetter;
}
if (start) // two letters length
{
currentLetter = letters[1 + offset];
if (join_modes[currentLetter][JMML] || join_modes[currentLetter][JMR])
{
res[1] = join_map[previousLetter][JLL];
}
else
{
res[1] = join_map[previousLetter][JLA];
}
}
currentLetter = letters[offset];
if (join_modes[currentLetter][JMR])
{
if (join_modes[previousLetter][JMML])
{
res[0] = join_map[currentLetter][JLR];
}
else
{
res[0] = join_map[currentLetter][JLA];
}
}
else
{
res[0] = join_map[currentLetter][JLA];
}
return res;
}
public static string ToString(byte[] data)
{
return ToString(data, 0, data.Length);
}
public static string ToString(byte[] data, int offset, int length)
{
if (length == 0 || data.Length < offset + length)
return string.Empty;
int j = length;
for (int i = offset; i < offset + length; ++i)
{
if (data[i] == 0xF2)
j += 1;
}
char[] temp = new char[j];
byte t;
j = 0;
for (int i = offset; i < offset + length; ++i)
{
if (data[i] == 0x20)
{
temp[temp.Length - j++ - 1] = table[79];
continue;
}
if (data[i] >= 0)
{
temp[temp.Length - j++ - 1] = (char)data[i];
}
else
{
t = (byte)(128 + data[i]);
if (t < 96 && t >= 48)
{
temp[temp.Length - j++ - 1] = table[79];
continue;
}
if (t >= 96)
t -= 48;
if (t == 66)
temp[temp.Length - j++ - 1] = table[16];
temp[temp.Length - j++ - 1] = table[t];
}
}
return new string(temp);
}
public static byte[] FromString(string data, bool reverse)
{
int[] letters = new int[data.Length];
int lettersLength = 0;
int letter = -1;
bool isLastSpace = false;
for (int i = 0; i < data.Length; ++i)
{
letter = UnicodeInternalIndex(data[i]);
if (letter == -1)
continue;
if (letter == SPACE)
{
if (!isLastSpace)
{
isLastSpace = true;
if (lettersLength != 0)
letters[lettersLength++] = letter;
}
continue;
}
else
{
isLastSpace = false;
}
if ((letter == ALEF || letter == AA) && lettersLength > 0 && letters[lettersLength - 1] == LAM)
letters[lettersLength - 1] = LA;
else
letters[lettersLength++] = letter;
}
if (letter == SPACE)
--lettersLength;
for (int i = 0, t, k = lettersLength / 2; i < k; i++)
{
t = letters[i];
letters[i] = letters[lettersLength - i - 1];
letters[lettersLength - i - 1] = t;
}
if (lettersLength > 0)
return reverse ? Reverse(ProcessBuffer(letters, 0, lettersLength)) : ProcessBuffer(letters, 0, lettersLength);
return null;
}
public static byte[] FromStringMultiPart(string data, bool reverse)
{
using (MemoryStream outStream = new MemoryStream())
{
int index = 0;
bool persian = false;
for (int i = 0; i < data.Length; ++i)
{
if (UnicodeInternalIndex(data[i]) != -1 && data[i] != ' ')
{
persian = true;
}
else
{
if (persian && i != 0)
{
persian = false;
var bytes = FromString(data.Substring(index, i - index), !reverse);
if (bytes != null)
outStream.Write(bytes, 0, bytes.Length);
index = i;
}
outStream.WriteByte((byte)data[i]);
}
}
if (index != data.Length && persian)
{
var bytes = FromString(data.Substring(index), !reverse);
if (bytes != null)
outStream.Write(bytes, 0, bytes.Length);
}
var array = outStream.ToArray();
return reverse ? array : Reverse(array);
}
}
private static byte[] Reverse(byte[] a)
{
if (a == null)
return null;
byte[] result = new byte[a.Length];
int p1 = 0;
int p2 = a.Length;
while (true)
{
p2--;
if (p2 < 0)
return result;
result[p2] = a[p1++];
}
}
// Example usage
/*
public static void Main()
{
string sample = "لورم ایپسوم متن ساختگی با تولید سادگی نامفهوم...";
byte[] res = IranSystemUtil.FromStringMultiPart(sample, false);
string output = IranSystemUtil.ToString(res, 0, res.Length);
Console.WriteLine(output);
}
*/
}
Notes:
I replaced ByteArrayOutputStream with MemoryStream.
Used byte[] arrays instead of Java byte[].
The reversed byte[] logic is faithfully converted.
I renamed method names to PascalCase as per C# conventions.
The commented Main method shows example usage; you can adapt it as needed.
Let me know if you want me to assist in any part or fix anything.