-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils-encoding.r
2068 lines (1910 loc) · 51.4 KB
/
utils-encoding.r
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
REBOL [
; -- Core Header attributes --
title: "Files encoding management"
file: %utils-encoding.r
version: 1.3.0
date: 2018-11-23
author: "Peter W A Wood"
purpose: {A set of string utilities created to help with text encoding in 8-bit
character strings}
source-encoding: "Windows-1252"
note: {slim Library Manager is Required to use this module.}
; -- slim - Library Manager --
slim-name: 'utils-encoding
slim-version: 1.2.1
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/utils-blocks.r
;- / license
license: 'mit
;- \ license
;- / history
history: {
v1.2.2 - 2018-11-23
-Semantic API version
v1.3.0 - 2018-11-23 <SMC>
-Slimified the library
-Fixed strip-bom that was expecting BOM to be an object instead of a block
}
;- \ history
;- / documentation
documentation: ""
;- \ documentation
]
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'utils-encoding
;
;--------------------------------------
slim/register [
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- GLOBAL
;
;-----------------------------------------------------------------------------------------------------------
BOM: [
"utf-32be" "^(00)^(00)^(FE)^(FF)"
"utf-32le" "^(FF)^(FE)^(00)^(00)"
"utf-16be" "^(FE)^(FF)"
"utf-16le" "^(FF)^(FE)"
"utf-8" "^(EF)^(BB)^(BF)"
]
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- PARSING RULES
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- constants
;
;--------------------------
replacement-char: #"?"
;--------------------------
;- standard bitsets
;
;--------------------------
ascii: charset [#"^(00)" - #"^(7F)"]
non-ascii: charset [#"^(80)" - #"^(FF)"]
characters: charset [#"^(00)" - #"^(FF)"]
ch128-159: charset [#"^(80)" - #"^(9F)"]
ch160-255: charset [#"^(A0)" - #"^(FF)"]
ch128-255: charset [#"^(80)" - #"^(FF)"]
alpha: charset [#"a" - #"z" #"A" - #"Z"]
digit: charset [#"0" - #"9"]
alphanumeric: union alpha digit
letter-hyphen: union alphanumeric charset ["-"]
byte: charset [#"^(00)" - #"^(FF)"]
;--------------------------
;- UTF-8 bitsets
;
;--------------------------
first-2-byte: charset [#"^(C2)" - #"^(DF)"]
first-3-byte: charset [#"^(E0)" - #"^(EF)"]
first-4-byte: charset [#"^(F0)" - #"^(F4)"]
subsequent-byte: charset [#"^(80)" - #"^(BF)"]
not-subsequent-byte: complement subsequent-byte
invalid: charset [#"^(C0)" - #"^(C1)" #"^(F5)" - #"^(FF)"]
;--------------------------
;- 8-bit bitsets
;
;--------------------------
x80-xBF: charset [#"^(80)" - #"^(BF)"]
xA0-xBF: charset [#"^(A0)" - #"^(BF)"]
xC0-xFF: charset [#"^(C0)" - #"^(FF)"]
;--------------------------
;- reduced bitsets
;
;--------------------------
ascii-less-ampltgt: charset [
#"^(00)" - #"^(25)"
#"^(27)" - #"^(3B)"
#"^(3D)"
#"^(3F)" - #"^(7F)"
]
ascii-less-cr-lf: charset [
#"^(00)" - #"^(09)" #"^(0B)" - #"^(0C)" #"^(0E)" - #"^(7F)"
]
characters-less-gt: charset [
#"^(00)" - #"^(3D)"
#"^(3F)" - #"^(7F)"
]
;--------------------------
;- standard patterns
;
;--------------------------
a-tag: ["<" some characters-less-gt ">"]
a-utf-8-two-byte: [first-2-byte subsequent-byte]
a-utf-8-three-byte: [first-3-byte 2 subsequent-byte]
a-utf-8-four-byte: [first-4-byte 3 subsequent-byte]
invalid-utf-8-two-byte: [first-2-byte not-subsequent-byte]
invalid-utf-8-three-byte: [
first-3-byte [
subsequent-byte not-subsequent-byte
|
not-subsequent-byte subsequent-byte
|
2 not-subsequent-byte
]
]
invalid-utf-8-four-byte: [
first-4-byte [
subsequent-byte not-subsequent-byte subsequent-byte
|
subsequent-byte not-subsequent-byte not-subsequent-byte
|
subsequent-byte subsequent-byte not-subsequent-byte
|
not-subsequent-byte not-subsequent-byte subsequent-byte
|
not-subsequent-byte subsequent-byte not-subsequent-byte
|
not-subsequent-byte subsequent-byte subsequent-byte
|
3 not-subsequent-byte
]
]
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- FUNCTIONS
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- bom?()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
bom?: make function! [
{Checks a string to see if it starts with a Unicode Byte Order Mark (BOM).
Returns one of the following:
"utf-32be"
"utf-32le"
"utf-16be"
"utf-16le"
"utf-8"
#[none]
}
str [string!]
][
foreach [encoding bom] BOM [
if find/part str bom length? bom [
return encoding
]
]
#[none]
]
;--------------------------
;- encoding?()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
encoding?: make function! [
{Ascertains the character encoding of a string by applying a few rules of
thumb.
Returns the following:
"us-ascii"
"utf-8"
"iso-8859-1"
"macintosh"
"windows-1252"
One of the following may possibly be returned but only if there is a
Unicode Byte Order Mark at the beginning of the string:
"utf-32be"
"utf-32le"
"utf-16be"
"utf-16le"
}
str [string!]
/local
count-chars {object to hold parse rules and reuslts to count the
different types of characters in a string.}
bom {temporary variable to hold the type of BOM}
][
count-chars: make object! [
;--------------------------
;- local
;
;--------------------------
;--------------------------
;- accumulators
;
;--------------------------
number-of: make object! [
ascii: 0
crs: 0
crlfs: 0
lfs: 0
macroman: 0
upper-80-9f: 0
upper-a0-ff: 0
utf-8-2: 0
utf-8-3: 0
utf-8-4: 0
invalid-utf-8: 0
utf-8-valid-patterns: copy []
utf-8-invalid-patterns: copy []
]
;--------------------------
;- bitsets
;
;--------------------------
macroman: charset [#"^(81)" #"^(8D)" #"^(90)" #"^(9D)"]
;--------------------------
;- character sequences
;
;--------------------------
ascii-chars: [some ascii-less-cr-lf]
;--------------------------
;- rules
;
;--------------------------
ascii-rule: [
copy substr ascii-chars (
number-of/ascii: number-of/ascii + length? substr
)
]
byte-rule: [
byte
parse-input: (parse-input: back parse-input) :parse-input
]
cr-rule: [
cr (
number-of/crs: number-of/crs + 1
number-of/ascii: number-of/ascii + 1
)
]
crlf-rule: [
crlf (
number-of/crlfs: number-of/crlfs + 1
number-of/crs: number-of/crs + 1
number-of/lfs: number-of/lfs + 1
number-of/ascii: number-of/ascii + 2
)
]
lf-rule: [
lf (
number-of/lfs: number-of/lfs + 1
number-of/ascii: number-of/ascii + 1
)
]
macroman-rule: [
macroman (
number-of/macroman: number-of/macroman + 1
number-of/upper-80-9f: number-of/upper-80-9f + 1
)
]
upper-80-9f-rule: [
ch128-159 (
number-of/upper-80-9f: number-of/upper-80-9f + 1
)
]
upper-a0-ff-rule: [
ch160-255 (
number-of/upper-a0-ff: number-of/upper-a0-ff + 1
)
]
;--------------------------
;- invalid-utf-8-rule
;
;--------------------------
invalid-utf-8-rule: [
.here:
copy bytes invalid (
append number-of/utf-8-invalid-patterns index? .here
append number-of/utf-8-invalid-patterns bytes
number-of/invalid-utf-8: number-of/invalid-utf-8 + 1
)
;parse-input: (parse-input: back parse-input) :parse-input
.here:
]
;--------------------------
;- invalid-utf-8-2-rule
;
;--------------------------
invalid-utf-8-2-rule: [
.here:
copy bytes invalid-utf-8-two-byte (
append number-of/utf-8-invalid-patterns index? .here
append number-of/utf-8-invalid-patterns bytes
number-of/invalid-utf-8: number-of/invalid-utf-8 + 1
)
;parse-input: (parse-input: back back parse-input) :parse-input
.here:
]
;--------------------------
;- invalid-utf-8-3-rule
;
;--------------------------
invalid-utf-8-3-rule: [
.here:
copy bytes invalid-utf-8-three-byte (
append number-of/utf-8-invalid-patterns index? .here
append number-of/utf-8-invalid-patterns bytes
number-of/invalid-utf-8: number-of/invalid-utf-8 + 1
)
;parse-input: (parse-input: back back back parse-input) :parse-input
.here:
]
;--------------------------
;- invalid-utf-8-4-rule
;
;--------------------------
invalid-utf-8-4-rule: [
.here:
copy bytes invalid-utf-8-four-byte (
append number-of/utf-8-invalid-patterns index? .here
append number-of/utf-8-invalid-patterns bytes
number-of/invalid-utf-8: number-of/invalid-utf-8 + 1
)
;parse-input: (parse-input: back back back back parse-input) :parse-input
.here:
]
;--------------------------
;- utf-8-2-rule:
;
;--------------------------
utf-8-2-rule: [
.here:
copy bytes a-utf-8-two-byte (
append number-of/utf-8-valid-patterns index? .here
append number-of/utf-8-valid-patterns bytes
number-of/utf-8-2: number-of/utf-8-2 + 1
)
parse-input: (parse-input: back back parse-input) :parse-input
]
;--------------------------
;- utf-8-3-rule:
;
;--------------------------
utf-8-3-rule: [
.here:
copy bytes a-utf-8-three-byte (
append number-of/utf-8-valid-patterns index? .here
append number-of/utf-8-valid-patterns bytes
number-of/utf-8-3: number-of/utf-8-3 + 1
)
parse-input: (parse-input: back back back parse-input) :parse-input
]
;--------------------------
;- utf-8-4-rule:
;
;--------------------------
utf-8-4-rule: [
.here:
copy bytes a-utf-8-four-byte (
append number-of/utf-8-valid-patterns index? .here
append number-of/utf-8-valid-patterns bytes
number-of/utf-8-4: number-of/utf-8-4 + 1
)
parse-input: (parse-input: back back back back parse-input) :parse-input
]
;--------------------------
;- rules:
;
;--------------------------
rules: [
any [
crlf-rule
|
[cr-rule | lf-rule]
|
[
utf-8-2-rule
|
utf-8-3-rule
|
utf-8-4-rule
|
invalid-utf-8-rule
|
invalid-utf-8-2-rule
|
invalid-utf-8-3-rule
|
invalid-utf-8-4-rule
|
byte-rule
]
[
ascii-rule
|
upper-a0-ff-rule
|
macroman-rule
|
upper-80-9f-rule
]
]
]
]
;; check for a BOM
if bom: bom? str [return bom]
;; count the types of characters in the input string
parse/all str count-chars/rules
;print "====================================="
;print " Decoding input text"
;print "====================================="
;probe mold/all count-chars/number-of
;; apply rules of thumb
if count-chars/number-of/ascii = length? str [
return "us-ascii"
]
if count-chars/number-of/invalid-utf-8 <
( count-chars/number-of/utf-8-2 +
count-chars/number-of/utf-8-3 +
count-chars/number-of/utf-8-4
)
[
return "utf-8"
]
if all [
count-chars/number-of/upper-a0-ff > 0
count-chars/number-of/upper-80-9f = 0
][
return "iso-8859-1"
]
if any [
count-chars/number-of/macroman > 0
all [
count-chars/number-of/crs > 0
count-chars/number-of/lfs = 0
]
][
return "macintosh"
]
return "windows-1252"
]
;--------------------------
;- macroman-to-utf-8()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
macroman-to-utf-8: make function! [
{
Converts a MacRoman encoded string to UTF-8.
Invalid characters are replaced
}
input-string [string!]
/local
extra-rules
trans-table
][
;; translation table
trans-table: [
"^(80)" "^(C3)^(84)"
"^(81)" "^(C3)^(85)"
"^(82)" "^(C3)^(87)"
"^(83)" "^(C3)^(89)"
"^(84)" "^(C3)^(91)"
"^(85)" "^(C3)^(96)"
"^(86)" "^(C3)^(9C)"
"^(87)" "^(C3)^(A1)"
"^(88)" "^(C3)^(A0)"
"^(89)" "^(C3)^(A2)"
"^(8A)" "^(C3)^(A4)"
"^(8B)" "^(C3)^(A3)"
"^(8C)" "^(C3)^(A5)"
"^(8D)" "^(C3)^(A7)"
"^(8E)" "^(C3)^(A9)"
"^(8F)" "^(C3)^(A8)"
"^(90)" "^(C3)^(AA)"
"^(91)" "^(C3)^(AB)"
"^(92)" "^(C3)^(AD)"
"^(93)" "^(C3)^(AC)"
"^(94)" "^(C3)^(AE)"
"^(95)" "^(C3)^(AF)"
"^(96)" "^(C3)^(B1)"
"^(97)" "^(C3)^(B3)"
"^(98)" "^(C3)^(B2)"
"^(99)" "^(C3)^(B4)"
"^(9A)" "^(C3)^(B6)"
"^(9B)" "^(C3)^(B5)"
"^(9C)" "^(C3)^(BA)"
"^(9D)" "^(C3)^(B9)"
"^(9E)" "^(C3)^(BB)"
"^(9F)" "^(C3)^(BC)"
"^(A0)" "^(E2)^(80)^(A0)"
"^(A1)" "^(C2)^(B0)"
"^(A2)" "^(C2)^(A2)"
"^(A3)" "^(C2)^(A3)"
"^(A4)" "^(C2)^(A7)"
"^(A5)" "^(E2)^(80)^(A2)"
"^(A6)" "^(C2)^(B6)"
"^(A7)" "^(C3)^(9F)"
"^(A8)" "^(C2)^(AE)"
"^(A9)" "^(C2)^(A9)"
"^(AA)" "^(E2)^(84)^(A2)"
"^(AB)" "^(C2)^(B4)"
"^(AC)" "^(C2)^(A8)"
"^(AD)" "^(E2)^(89)^(A0)"
"^(AE)" "^(C3)^(86)"
"^(AF)" "^(C3)^(98)"
"^(B0)" "^(E2)^(88)^(9E)"
"^(B1)" "^(C2)^(B1)"
"^(B2)" "^(E2)^(89)^(A4)"
"^(B3)" "^(E2)^(89)^(A5)"
"^(B4)" "^(C2)^(A5)"
"^(B5)" "^(C2)^(B5)"
"^(B6)" "^(E2)^(88)^(82)"
"^(B7)" "^(E2)^(88)^(91)"
"^(B8)" "^(E2)^(88)^(8F)"
"^(B9)" "^(CF)^(80)"
"^(BA)" "^(E2)^(88)^(AB)"
"^(BB)" "^(C2)^(AA)"
"^(BC)" "^(C2)^(BA)"
"^(BD)" "^(CE)^(A9)"
"^(BE)" "^(C3)^(A6)"
"^(BF)" "^(C3)^(B8)"
"^(C0)" "^(C2)^(BF)"
"^(C1)" "^(C2)^(A1)"
"^(C2)" "^(C2)^(AC)"
"^(C3)" "^(E2)^(88)^(9A)"
"^(C4)" "^(C6)^(92)"
"^(C5)" "^(E2)^(89)^(88)"
"^(C6)" "^(E2)^(88)^(86)"
"^(C7)" "^(C2)^(AB)"
"^(C8)" "^(C2)^(BB)"
"^(C9)" "^(E2)^(80)^(A6)"
"^(CA)" "^(C2)^(A0)"
"^(CB)" "^(C3)^(80)"
"^(CC)" "^(C3)^(83)"
"^(CD)" "^(C3)^(95)"
"^(CE)" "^(C5)^(92)"
"^(CF)" "^(C5)^(93)"
"^(D0)" "^(E2)^(80)^(93)"
"^(D1)" "^(E2)^(80)^(94)"
"^(D2)" "^(E2)^(80)^(9C)"
"^(D3)" "^(E2)^(80)^(9D)"
"^(D4)" "^(E2)^(80)^(98)"
"^(D5)" "^(E2)^(80)^(99)"
"^(D6)" "^(C3)^(B7)"
"^(D7)" "^(E2)^(97)^(8A)"
"^(D8)" "^(C3)^(BF)"
"^(D9)" "^(C5)^(B8)"
"^(DA)" "^(E2)^(81)^(84)"
"^(DB)" "^(E2)^(82)^(AC)"
"^(DC)" "^(E2)^(80)^(B9)"
"^(DD)" "^(E2)^(80)^(BA)"
"^(DE)" "^(EF)^(AC)^(81)"
"^(DF)" "^(EF)^(AC)^(82)"
"^(E0)" "^(E2)^(80)^(A1)"
"^(E1)" "^(C2)^(B7)"
"^(E2)" "^(E2)^(80)^(9A)"
"^(E3)" "^(E2)^(80)^(9E)"
"^(E4)" "^(E2)^(80)^(B0)"
"^(E5)" "^(C3)^(82)"
"^(E6)" "^(C3)^(8A)"
"^(E7)" "^(C3)^(81)"
"^(E8)" "^(C3)^(8B)"
"^(E9)" "^(C3)^(88)"
"^(EA)" "^(C3)^(8D)"
"^(EB)" "^(C3)^(8E)"
"^(EC)" "^(C3)^(8F)"
"^(ED)" "^(C3)^(8C)"
"^(EE)" "^(C3)^(93)"
"^(EF)" "^(C3)^(94)"
"^(F0)" "^(EF)^(A3)^(BF)"
"^(F1)" "^(C3)^(92)"
"^(F2)" "^(C3)^(9A)"
"^(F3)" "^(C3)^(9B)"
"^(F4)" "^(C3)^(99)"
"^(F5)" "^(C4)^(B1)"
"^(F6)" "^(CB)^(86)"
"^(F7)" "^(CB)^(9C)"
"^(F8)" "^(C2)^(AF)"
"^(F9)" "^(CB)^(98)"
"^(FA)" "^(CB)^(99)"
"^(FB)" "^(CB)^(9A)"
"^(FC)" "^(C2)^(B8)"
"^(FD)" "^(CB)^(9D)"
"^(FE)" "^(CB)^(9B)"
"^(FF)" "^(CB)^(87)"
]
;; Define the additional rules to be applied before the default rules
extra-rules: [
copy transfer ch128-255 (
insert tail output-string select/case trans-table transfer
)
]
iso-8859-to-utf-8/addl-rules input-string extra-rules
]
;--------------------------
;- mail-encoding?()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
mail-encoding?: make function! [
{Returns the charset of the first Content-type entry in a mail message}
mail-str [string!]
/local
cset ;; character set
][
either parse/all mail-str [
to "Content-type" thru "charset=" copy cset some letter-hyphen to end end
][
lowercase cset
][
#[none]
]
]
;--------------------------
;- iso-8859-1-to-html()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
iso-8859-1-to-html: make function! [
{Converts an ISO-8859-1 encoded string to pure ASCII with characters 128
and above converted to html escape sequences}
input-string [string!]
/esc-lt-gt-amp {Escape <, > and &}
/keep-tags {leave < ....> alone}
/local
output-string
rule
transfer
escape
no-refinement-rule
esc-lt-gt-amp-rule
keep-tags-rule
standard-rule
variable-rule
][
no-refinement-rule: [
copy transfer [some ascii] (
insert tail output-string transfer
)
]
esc-lt-gt-amp-rule: [
"<" (insert tail output-string "<")
|
">" (insert tail output-string ">")
|
"&" (insert tail output-string "&")
|
copy transfer [some ascii-less-ampltgt] (
;print "here"
insert tail output-string transfer
)
]
keep-tags-rule: [
copy transfer a-tag (
insert tail output-string transfer
)
]
;; rule to deal with characters above 127
standard-rule: [
some ch128-159 ;; ignore characters in this range
|
copy escape ch160-255 (
insert tail output-string join "&#" [to integer! first escape ";"]
)
|
skip
]
;; assemble the parse rule according to the refinements
either esc-lt-gt-amp [
either keep-tags [
rule: [
any [
keep-tags-rule
|
esc-lt-gt-amp-rule
|
standard-rule
]
]
][
rule: [
any [
esc-lt-gt-amp-rule
|
standard-rule
]
]
]
][
rule: [
any [
no-refinement-rule
|
standard-rule
]
]
]
output-string: copy ""
parse/all input-string rule
head output-string
]
;--------------------------
;- iso-8859-to-utf-8()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
iso-8859-to-utf-8: make function! [
{
Converts an ISO-8859 encoded string to UTF-8.
The default processing assumes the input is ISO-8859-1
The /addl-rules refinement allows rules to be supplied for other ecodings
}
input-string [string!]
/addl-rules
extra-rules [block!]
/local
output-string
rule
ascii-rule
rule-80-BF
C0-FF-rule
transfer
][
;; temporary variables and constants
output-string: copy ""
transfer: none
;; sub-rules
ascii-rule: [
copy transfer [some ascii] (
insert tail output-string transfer
)
]
rule-80-BF: [
;; characters in the range 80-BF relate to C280-C2BF
copy transfer x80-xBF (
insert tail output-string compose [#"^(C2)" (transfer)]
)
]
C0-FF-rule: [
;; characters in the range C0-FF relate to C380-C3BF
copy transfer xC0-xFF (
insert tail output-string compose [
#"^(C3)" (#"^(40)" xor to char! transfer)
]
)
]
rule: [
any [
ascii-rule
|
rule-80-BF
|
C0-FF-rule
]
]
;; add the extra rules to the rule
if addl-rules [
bind extra-rules 'output-string
insert find/tail second rule 'ascii-rule [| extra-rules]
]
parse/all/case input-string rule
head output-string
]
;--------------------------
;- iso-8859-1-to-utf-8()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
iso-8859-1-to-utf-8: make function! [
{
Converts an ISO-8859-1 encoded string to UTF-8.
}
input-string [string!]
][
iso-8859-to-utf-8 input-string
]
;--------------------------
;- iso-8859-2-to-utf-8()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; to do:
;
; tests:
;--------------------------
iso-8859-2-to-utf-8: make function! [
{
Converts an ISO-8859-2 encoded string to UTF-8.
Invalid characters are replaced
}
input-string [string!]
/local
extra-rules
trans-table
][
;; translation table
trans-table: [
"^(A0)" "^(C2)^(A0)"
"^(A1)" "^(C4)^(84)"
"^(A2)" "^(CB)^(98)"
"^(A3)" "^(C5)^(81)"
"^(A4)" "^(C2)^(A4)"
"^(A5)" "^(C4)^(BD)"
"^(A6)" "^(C5)^(9A)"
"^(A7)" "^(C2)^(A7)"
"^(A8)" "^(C2)^(A8)"
"^(A9)" "^(C5)^(A0)"
"^(AA)" "^(C5)^(9E)"
"^(AB)" "^(C5)^(A4)"
"^(AC)" "^(C5)^(B9)"
"^(AD)" "^(C2)^(AD)"
"^(AE)" "^(C5)^(BD)"
"^(AF)" "^(C5)^(BB)"
"^(B0)" "^(C2)^(B0)"
"^(B1)" "^(C4)^(85)"
"^(B2)" "^(CB)^(9B)"
"^(B3)" "^(C5)^(82)"
"^(B4)" "^(C2)^(B4)"
"^(B5)" "^(C4)^(BE)"
"^(B6)" "^(C5)^(9B)"
"^(B7)" "^(CB)^(87)"
"^(B8)" "^(C2)^(B8)"
"^(B9)" "^(C5)^(A1)"
"^(BA)" "^(C5)^(9F)"