-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIranSystemUtil.java
More file actions
787 lines (736 loc) · 26.1 KB
/
IranSystemUtil.java
File metadata and controls
787 lines (736 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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;
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));
}
}