qr_code 2.0.0

QR code encoder in Rust, support structured append (data in multiple qrcodes)
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
//! Find the optimal data mode sequence to encode a piece of data.
use crate::types::{Mode, Version};
use std::borrow::Borrow;
use std::slice::Iter;

//------------------------------------------------------------------------------
//{{{ Segment

/// A segment of data committed to an encoding mode.
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
pub struct Segment {
    /// The encoding mode of the segment of data.
    pub mode: Mode,

    /// The start index of the segment.
    pub begin: usize,

    /// The end index (exclusive) of the segment.
    pub end: usize,
}

impl Segment {
    /// Compute the number of bits (including the size of the mode indicator and
    /// length bits) when this segment is encoded.
    pub fn encoded_len(&self, version: Version) -> usize {
        let byte_size = self.end - self.begin;
        let chars_count = if self.mode == Mode::Kanji {
            byte_size / 2
        } else {
            byte_size
        };

        let mode_bits_count = version.mode_bits_count();
        let length_bits_count = self.mode.length_bits_count(version);
        let data_bits_count = self.mode.data_bits_count(chars_count);

        mode_bits_count + length_bits_count + data_bits_count
    }

    /// Panics if `&self` is not a valid segment.
    fn assert_invariants(&self) {
        // TODO: It would be great if it would be impossible to construct an invalid `Segment` -
        // either by 1) making the fields private and only allowing construction via a public,
        // preconditions-checking API, or 2) keeping the fields public but replacing `end: usize`
        // with `len: NonZeroUsize`.
        assert!(self.begin < self.end);
    }
}

//}}}
//------------------------------------------------------------------------------
//{{{ Parser

/// This iterator is basically equivalent to
///
/// ```ignore
/// data.map(|c| ExclCharSet::from_u8(*c))
///     .chain(Some(ExclCharSet::End).move_iter())
///     .enumerate()
/// ```
///
/// But the type is too hard to write, thus the new type.
///
struct EcsIter<I> {
    base: I,
    index: usize,
    ended: bool,
}

impl<'a, I: Iterator<Item = &'a u8>> Iterator for EcsIter<I> {
    type Item = (usize, ExclCharSet);

    fn next(&mut self) -> Option<(usize, ExclCharSet)> {
        if self.ended {
            return None;
        }

        match self.base.next() {
            None => {
                self.ended = true;
                Some((self.index, ExclCharSet::End))
            }
            Some(c) => {
                let old_index = self.index;
                self.index += 1;
                Some((old_index, ExclCharSet::from_u8(*c)))
            }
        }
    }
}

/// QR code data parser to classify the input into distinct segments.
pub struct Parser<'a> {
    ecs_iter: EcsIter<Iter<'a, u8>>,
    state: State,
    begin: usize,
    pending_single_byte: bool,
}

impl<'a> Parser<'a> {
    /// Creates a new iterator which parse the data into segments that only
    /// contains their exclusive subsets. No optimization is done at this point.
    ///
    ///     use qr_code::optimize::{Parser, Segment};
    ///     use qr_code::types::Mode::{Alphanumeric, Numeric, Byte};
    ///
    ///     let parse_res = Parser::new(b"ABC123abcd").collect::<Vec<Segment>>();
    ///     assert_eq!(parse_res, vec![Segment { mode: Alphanumeric, begin: 0, end: 3 },
    ///                                Segment { mode: Numeric, begin: 3, end: 6 },
    ///                                Segment { mode: Byte, begin: 6, end: 10 }]);
    ///
    pub fn new(data: &[u8]) -> Parser {
        Parser {
            ecs_iter: EcsIter {
                base: data.iter(),
                index: 0,
                ended: false,
            },
            state: State::Init,
            begin: 0,
            pending_single_byte: false,
        }
    }
}

impl<'a> Iterator for Parser<'a> {
    type Item = Segment;

    fn next(&mut self) -> Option<Segment> {
        if self.pending_single_byte {
            self.pending_single_byte = false;
            self.begin += 1;
            return Some(Segment {
                mode: Mode::Byte,
                begin: self.begin - 1,
                end: self.begin,
            });
        }

        loop {
            let (i, ecs) = match self.ecs_iter.next() {
                None => return None,
                Some(a) => a,
            };
            let (next_state, action) = STATE_TRANSITION[self.state as usize + ecs as usize];
            self.state = next_state;

            let old_begin = self.begin;
            let push_mode = match action {
                Action::Idle => continue,
                Action::Numeric => Mode::Numeric,
                Action::Alpha => Mode::Alphanumeric,
                Action::Byte => Mode::Byte,
                Action::Kanji => Mode::Kanji,
                Action::KanjiAndSingleByte => {
                    let next_begin = i - 1;
                    if self.begin == next_begin {
                        Mode::Byte
                    } else {
                        self.pending_single_byte = true;
                        self.begin = next_begin;
                        return Some(Segment {
                            mode: Mode::Kanji,
                            begin: old_begin,
                            end: next_begin,
                        });
                    }
                }
            };

            self.begin = i;
            return Some(Segment {
                mode: push_mode,
                begin: old_begin,
                end: i,
            });
        }
    }
}

#[cfg(test)]
mod parse_tests {
    use crate::optimize::{Parser, Segment};
    use crate::types::Mode;

    fn parse(data: &[u8]) -> Vec<Segment> {
        Parser::new(data).collect()
    }

    #[test]
    fn test_parse_1() {
        let segs = parse(b"01049123451234591597033130128%10ABC123");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 29
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 29,
                    end: 30
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 30,
                    end: 32
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 32,
                    end: 35
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 35,
                    end: 38
                },
            ]
        );
    }

    #[test]
    fn test_parse_shift_jis_example_1() {
        let segs = parse(b"\x82\xa0\x81\x41\x41\xb1\x81\xf0"); // "あ、AアÅ"
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 4
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 4,
                    end: 5
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 5,
                    end: 6
                },
                Segment {
                    mode: Mode::Kanji,
                    begin: 6,
                    end: 8
                },
            ]
        );
    }

    #[test]
    fn test_parse_utf_8() {
        // Mojibake?
        let segs = parse(b"\xe3\x81\x82\xe3\x80\x81A\xef\xbd\xb1\xe2\x84\xab");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 4
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 4,
                    end: 5
                },
                Segment {
                    mode: Mode::Kanji,
                    begin: 5,
                    end: 7
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 7,
                    end: 10
                },
                Segment {
                    mode: Mode::Kanji,
                    begin: 10,
                    end: 12
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 12,
                    end: 13
                },
            ]
        );
    }

    #[test]
    fn test_not_kanji_1() {
        let segs = parse(b"\x81\x30");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 1
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 1,
                    end: 2
                },
            ]
        );
    }

    #[test]
    fn test_not_kanji_2() {
        // Note that it's implementation detail that the byte seq is split into
        // two. Perhaps adjust the test to check for this.
        let segs = parse(b"\xeb\xc0");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 1
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 1,
                    end: 2
                },
            ]
        );
    }

    #[test]
    fn test_not_kanji_3() {
        let segs = parse(b"\x81\x7f");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 1
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 1,
                    end: 2
                },
            ]
        );
    }

    #[test]
    fn test_not_kanji_4() {
        let segs = parse(b"\x81\x40\x81");
        assert_eq!(
            segs,
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 2
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 2,
                    end: 3
                },
            ]
        );
    }
}

/// Implementation of a shortest path algorithm in a graph where:
///
/// * Graph nodes represent an encoding that covers an initial slice of segments and ends with a
///   given `Mode`.  For an input of N segments, the graph will contain at most 4 * N nodes (maybe
///   less, because some reencodings are not possible - e.g. it is not possible to reencode a
///   `Byte` segment as `Numeric`).
/// * Graph edges connect encodings that cover N segments with encodings
///   that cover 1 additional segment.  Because of possible `Mode` transitions
///   nodes can have up to 4 incoming edges and up to 4 outgoing edges.
///
/// The algorithm follows the relaxation approach of the
/// https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm, but considers the nodes and edges in a
/// fixed order (rather than using a priority queue).  This is possible because of the constrained,
/// structured shape of the graph we are working with.
mod shortest_path {
    use super::Segment;
    use crate::types::{Mode, Version};
    use std::cmp::{Ordering, PartialOrd};

    #[derive(Clone)]
    struct Predecessor {
        index_of_segment: usize,
        mode: Mode,
    }

    #[derive(Clone)]
    struct ShortestPath {
        length: usize,
        predecessor: Option<Predecessor>,
    }

    pub struct AlgorithmState<'a> {
        /// Stores shortest paths - see `get_shortest_path` and `get_path_index` for more details.
        paths: Vec<Option<ShortestPath>>,

        /// Initial segmentation that we are trying to improve.
        segments: &'a [Segment],

        /// QR code version that determines how many bits are needed to encode a given `Segment`.
        version: Version,
    }

    /// Finds the index into `AlgorithmState::paths` for a path that:
    /// * Includes all the `0..=index_of_segment` segments from `AlgorithmState::segments`
    ///   (this range is inclusive on both ends).
    /// * Encodes the last segment (i.e. the one at `index_of_segment`) using `mode`
    fn get_path_index(index_of_segment: usize, mode: Mode) -> usize {
        index_of_segment * Mode::ALL_MODES.len() + (mode as usize)
    }

    impl<'a> AlgorithmState<'a> {
        /// Gets the shortest path that has been computed earlier.
        ///
        /// * For more details about `ending_mode` and `index_of_segment` see the documentation
        ///   of `get_path_index`
        /// * For more details about the return value see the documentation of
        ///   `compute_shortest_path`.
        fn get_shortest_path(
            &self,
            index_of_segment: usize,
            ending_mode: Mode,
        ) -> &Option<ShortestPath> {
            &self.paths[get_path_index(index_of_segment, ending_mode)]
        }

        fn get_end_of_predecessor(&self, predecessor: &Option<Predecessor>) -> usize {
            match predecessor {
                None => 0,
                Some(predecessor) => self.segments[predecessor.index_of_segment].end,
            }
        }

        /// Computes the shortest path that
        /// * Includes all the segments in `self.segments[0..=index_of_segment]`
        /// * Encodes the last segment (i.e. the one at `index_of_segment`) using `desired_mode`
        ///
        /// Assumes that shortest paths for all earlier segments (i.e. ones before
        /// `index_of_segment) have already been computed and memoized in `self.paths`.
        ///
        /// Returns `None` if the segment at `index_of_segment` can't be encoded using
        /// `desired_mode` (e.g. contents of `Mode::Byte` segment can't be reencoded using
        /// `Mode::Numeric` - see also the documentation of `PartialOrd` impl for `Mode`).
        fn compute_shortest_path(
            &self,
            index_of_segment: usize,
            desired_mode: Mode,
        ) -> Option<ShortestPath> {
            let curr_segment = self.segments[index_of_segment];

            // Check if `curr_segment` can be reencoded using `desired_mode`.
            match curr_segment.mode.partial_cmp(&desired_mode) {
                None | Some(Ordering::Greater) => return None,
                Some(Ordering::Less) | Some(Ordering::Equal) => (),
            }

            let length_of_reencoding_curr_segment_in_desired_mode = {
                let reencoded_segment = Segment {
                    begin: curr_segment.begin,
                    end: curr_segment.end,
                    mode: desired_mode,
                };
                reencoded_segment.encoded_len(self.version)
            };

            // Handle the case when there are no predecessors.
            if index_of_segment == 0 {
                return Some(ShortestPath {
                    length: length_of_reencoding_curr_segment_in_desired_mode,
                    predecessor: None,
                });
            }

            // Find the predecessor with the best mode / compute the shortest path.
            let prev_index = index_of_segment - 1;
            Mode::ALL_MODES
                .iter()
                .filter_map(|&prev_mode| {
                    self.get_shortest_path(prev_index, prev_mode)
                        .as_ref()
                        .map(|prev_path| (prev_mode, prev_path))
                })
                .map(|(prev_mode, prev_path)| {
                    if prev_mode == desired_mode {
                        let merged_length = {
                            let merged_segment = Segment {
                                begin: self.get_end_of_predecessor(&prev_path.predecessor),
                                end: curr_segment.end,
                                mode: desired_mode,
                            };
                            merged_segment.encoded_len(self.version)
                        };
                        let length_up_to_merged_segment = match prev_path.predecessor.as_ref() {
                            None => 0,
                            Some(predecessor) => {
                                self.get_shortest_path(
                                    predecessor.index_of_segment,
                                    predecessor.mode,
                                )
                                .as_ref()
                                .expect("Predecessors should point at a valid path")
                                .length
                            }
                        };
                        ShortestPath {
                            length: length_up_to_merged_segment + merged_length,
                            predecessor: prev_path.predecessor.clone(),
                        }
                    } else {
                        ShortestPath {
                            length: prev_path.length
                                + length_of_reencoding_curr_segment_in_desired_mode,
                            predecessor: Some(Predecessor {
                                index_of_segment: prev_index,
                                mode: prev_mode,
                            }),
                        }
                    }
                })
                .min_by_key(|path| path.length)
        }

        /// Runs the shortest-path algorithm, fully populating `Self::paths`.
        pub fn find_shortest_paths(segments: &'a [Segment], version: Version) -> Self {
            let mut this = AlgorithmState::<'a> {
                segments,
                paths: vec![None; segments.len() * Mode::ALL_MODES.len()],
                version,
            };
            for index_of_segment in 0..segments.len() {
                for &mode in Mode::ALL_MODES {
                    this.paths[get_path_index(index_of_segment, mode)] =
                        this.compute_shortest_path(index_of_segment, mode);
                }
            }
            this
        }

        /// Constructs the best segmentation from prepopulated `self.paths`.
        pub fn construct_best_segmentation(&self) -> Vec<Segment> {
            let mut result = Vec::new();

            // Start at the last segment (since we want the best encoding
            // that covers all of the input segments) and find the mode that
            // results in the shortest path for the last segment.
            let mut curr_index_of_segment = self.segments.len() - 1;
            let (mut best_mode, mut shortest_path) = Mode::ALL_MODES
                .iter()
                .filter_map(|&mode| {
                    self.get_shortest_path(curr_index_of_segment, mode)
                        .as_ref()
                        .map(|shortest_path| (mode, shortest_path))
                })
                .min_by_key(|(_mode, path)| path.length)
                .expect("At least one path should always exist");

            // Work backwards to construct the shortest path based on the predecessor information.
            loop {
                result.push(Segment {
                    begin: self.get_end_of_predecessor(&shortest_path.predecessor),
                    end: self.segments[curr_index_of_segment].end,
                    mode: best_mode,
                });
                match shortest_path.predecessor.as_ref() {
                    None => {
                        result.reverse();
                        return result;
                    }
                    Some(predecessor) => {
                        curr_index_of_segment = predecessor.index_of_segment;
                        best_mode = predecessor.mode;
                        shortest_path = self
                            .get_shortest_path(curr_index_of_segment, best_mode)
                            .as_ref()
                            .expect("Predecessors should point at valid paths");
                    }
                };
            }
        }
    }
}

/// Panics if `segments` are not consecutive (i.e. if there are gaps, or overlaps, or if the
/// segments are not ordered by their `begin` field).
fn assert_valid_segmentation(segments: &[Segment]) {
    if segments.is_empty() {
        return;
    }

    for segment in segments.iter() {
        segment.assert_invariants();
    }

    let consecutive_pairs = segments[..(segments.len() - 1)]
        .iter()
        .zip(segments[1..].iter());
    for (prev, next) in consecutive_pairs {
        assert_eq!(prev.end, next.begin, "Non-adjacent segments");
    }
}

/// Optimize the segments by combining segments when possible.
///
/// Optimization considers all possible segmentations where each of the input `segments`
/// may have been reencoded into a different mode (and where adjacent, same-mode segments are
/// merged).  The shortest of the considered segmentations is returned.  The implementation uses a
/// shortest-path algorithm that runs in O(N) time and uses additional O(N) memory.
///
/// This function may panic if `segments` do not represent a valid segmentation
/// (e.g. if there are gaps between segments, if the segments overlap, etc.).
pub fn optimize_segmentation(segments: &[Segment], version: Version) -> Vec<Segment> {
    assert_valid_segmentation(segments);
    if segments.is_empty() {
        return Vec::new();
    }

    let optimized_segments = shortest_path::AlgorithmState::find_shortest_paths(segments, version)
        .construct_best_segmentation();

    #[cfg(fuzzing)]
    optimize_test_helpers::assert_valid_optimization(&segments, &*optimized_segments, version);

    optimized_segments
}

/// Computes the total encoded length of all segments.
pub fn total_encoded_len<I>(segments: I, version: Version) -> usize
where
    I: IntoIterator,
    I::Item: Borrow<Segment>,
{
    segments
        .into_iter()
        .map(|seg| seg.borrow().encoded_len(version))
        .sum()
}

#[cfg(any(fuzzing, test))]
mod optimize_test_helpers {
    use super::{assert_valid_segmentation, total_encoded_len, Segment};
    use crate::types::{Mode, Version};
    use std::cmp::Ordering;

    /// Panics if there exists an input that can be represented by `given` segments,
    /// but that cannot be represented by `opt` segments.
    pub fn assert_segmentation_equivalence(given: &[Segment], opt: &[Segment]) {
        if given.is_empty() {
            assert!(opt.is_empty());
            return;
        }
        let begin = given.first().unwrap().begin;
        let end = given.last().unwrap().end;

        // Verify that `opt` covers the same range as `given`.
        // (This assumes that contiguous coverage has already been verified by
        // `assert_valid_segmentation`.)
        assert!(!opt.is_empty());
        assert_eq!(begin, opt.first().unwrap().begin);
        assert_eq!(end, opt.last().unwrap().end);

        // Verify that for each character, `opt` can represent all the characters that may be
        // present in `given`.
        for i in begin..end {
            fn find_mode(segments: &[Segment], i: usize) {
                segments
                    .iter()
                    .filter(|s| (s.begin <= i) && (i < s.end))
                    .next()
                    .expect("Expecting exactly one segment")
                    .mode;
            }
            let given_mode = find_mode(&*given, i);
            let opt_mode = find_mode(&*given, i);
            match given_mode.partial_cmp(&opt_mode) {
                Some(Ordering::Less) | Some(Ordering::Equal) => (),
                _ => panic!(
                    "Character #{} is {:?}, which {:?} may not represent",
                    i, given_mode, opt_mode,
                ),
            }
        }
    }

    /// Panics if `optimized` is not an improved representation of `input`.
    pub fn assert_valid_optimization(input: &[Segment], optimized: &[Segment], version: Version) {
        assert_valid_segmentation(input);
        assert_valid_segmentation(optimized);
        assert_segmentation_equivalence(input, optimized);

        let input_len = total_encoded_len(input, version);
        let optimized_len = total_encoded_len(optimized, version);
        assert!(optimized_len <= input_len);

        let single_bytes_segment_len = if input.is_empty() {
            0
        } else {
            Segment {
                begin: input.first().unwrap().begin,
                end: input.last().unwrap().end,
                mode: Mode::Byte,
            }
            .encoded_len(version)
        };
        assert!(optimized_len <= single_bytes_segment_len);
    }
}

#[cfg(test)]
mod optimize_tests {
    use super::optimize_test_helpers::*;
    use super::{assert_valid_segmentation, optimize_segmentation, total_encoded_len, Segment};
    use crate::types::{Mode, Version};
    use std::cmp::Ordering;

    fn test_optimization_result(given: Vec<Segment>, expected: Vec<Segment>, version: Version) {
        // Verify that the test input is valid.
        assert_valid_segmentation(&given);

        // Verify that the test expectations are compatible with the test input.
        assert_valid_segmentation(&expected);
        assert_segmentation_equivalence(&given, &expected);

        let opt_segs = optimize_segmentation(given.as_slice(), version);
        assert_valid_optimization(&given, &opt_segs, version);

        // Verify that optimization produces result that is as short as `expected`.
        if opt_segs != expected {
            let actual_len = total_encoded_len(&opt_segs, version);
            let expected_len = total_encoded_len(&expected, version);
            let msg = match actual_len.cmp(&expected_len) {
                Ordering::Less => "Optimization gave something better than expected",
                Ordering::Equal => "Optimization gave something different, but just as short",
                Ordering::Greater => "Optimization gave something worse than expected",
            };
            panic!(
                "{}: expected_len={}; actual_len={}; opt_segs=({:?})",
                msg, expected_len, actual_len, opt_segs
            );
        }
    }

    #[test]
    fn test_example_1() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 0,
                    end: 3,
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 3,
                    end: 6,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 6,
                    end: 10,
                },
            ],
            vec![
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 0,
                    end: 6,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 6,
                    end: 10,
                },
            ],
            Version::Normal(1),
        );
    }

    #[test]
    fn test_example_2() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 29,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 29,
                    end: 30,
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 30,
                    end: 32,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 32,
                    end: 35,
                },
                Segment {
                    mode: Mode::Numeric,
                    begin: 35,
                    end: 38,
                },
            ],
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 29,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 29,
                    end: 38,
                },
            ],
            Version::Normal(9),
        );
    }

    #[test]
    fn test_example_3() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 4,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 4,
                    end: 5,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 5,
                    end: 6,
                },
                Segment {
                    mode: Mode::Kanji,
                    begin: 6,
                    end: 8,
                },
            ],
            vec![Segment {
                mode: Mode::Byte,
                begin: 0,
                end: 8,
            }],
            Version::Normal(1),
        );
    }

    #[test]
    fn test_example_4() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 10,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 10,
                    end: 11,
                },
            ],
            vec![
                Segment {
                    mode: Mode::Kanji,
                    begin: 0,
                    end: 10,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 10,
                    end: 11,
                },
            ],
            Version::Normal(1),
        );
    }

    #[test]
    fn test_empty() {
        test_optimization_result(vec![], vec![], Version::Normal(1));
    }

    /// This example shows that greedy merging strategy (used by `qr_code` v1.1.0) can give
    /// suboptimal results for the following input: `"bytesbytesA1B2C3D4E5"`.  The greedy strategy
    /// will always consider it (locally) beneficial to merge the initial `Mode::Byte` segment with
    /// the subsequent single-char segment - this will result in representing the whole input
    /// in a single `Mode::Byte` segment.  Better segmentation can be done by first merging all the
    /// `Mode::Alphanumeric` and `Mode::Numeric` segments.
    #[test]
    fn test_example_where_greedy_merging_is_suboptimal() {
        let mut input = vec![Segment {
            mode: Mode::Byte,
            begin: 0,
            end: 10,
        }];
        for _ in 0..5 {
            for &mode in [Mode::Alphanumeric, Mode::Numeric].iter() {
                let begin = input.last().unwrap().end;
                let end = begin + 1;
                input.push(Segment { mode, begin, end });
            }
        }
        test_optimization_result(
            input,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 10,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 10,
                    end: 20,
                },
            ],
            Version::Normal(40),
        );
    }

    /// In this example merging 2 consecutive segments is always harmful, but merging many segments
    /// may be beneficial.  This is because merging more than 2 segments saves additional header
    /// bytes.
    #[test]
    fn test_example_where_merging_two_consecutive_segments_is_always_harmful() {
        let mut input = vec![];
        for _ in 0..5 {
            for &mode in [Mode::Byte, Mode::Alphanumeric].iter() {
                let begin = input.last().map(|s: &Segment| s.end).unwrap_or_default();
                let end = begin + 10;
                input.push(Segment { mode, begin, end });
            }
        }
        test_optimization_result(
            input,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 90,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 90,
                    end: 100,
                },
            ],
            Version::Normal(40),
        );
    }

    #[test]
    fn test_optimize_base64() {
        let input: &[u8] = include_bytes!("../test_data/large_base64.in");
        let input: Vec<Segment> = super::Parser::new(input).collect();
        test_optimization_result(
            input,
            vec![
                Segment {
                    mode: Mode::Byte,
                    begin: 0,
                    end: 334,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 334,
                    end: 349,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 349,
                    end: 387,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 387,
                    end: 402,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 402,
                    end: 850,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 850,
                    end: 866,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 866,
                    end: 1146,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 1146,
                    end: 1162,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 1162,
                    end: 2474,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 2474,
                    end: 2489,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 2489,
                    end: 2618,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 2618,
                    end: 2641,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 2641,
                    end: 2707,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 2707,
                    end: 2722,
                },
                Segment {
                    mode: Mode::Byte,
                    begin: 2722,
                    end: 2880,
                },
            ],
            Version::Normal(40),
        );
    }

    #[test]
    fn test_annex_j_guideline_1a() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 3,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 3,
                    end: 4,
                },
            ],
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 3,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 3,
                    end: 4,
                },
            ],
            Version::Micro(2),
        );
    }

    #[test]
    fn test_annex_j_guideline_1b() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 2,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 2,
                    end: 4,
                },
            ],
            vec![Segment {
                mode: Mode::Alphanumeric,
                begin: 0,
                end: 4,
            }],
            Version::Micro(2),
        );
    }

    #[test]
    fn test_annex_j_guideline_1c() {
        test_optimization_result(
            vec![
                Segment {
                    mode: Mode::Numeric,
                    begin: 0,
                    end: 3,
                },
                Segment {
                    mode: Mode::Alphanumeric,
                    begin: 3,
                    end: 4,
                },
            ],
            vec![Segment {
                mode: Mode::Alphanumeric,
                begin: 0,
                end: 4,
            }],
            Version::Micro(3),
        );
    }
}

#[cfg(bench)]
mod bench {
    use super::{optimize_segmentation, total_encoded_len, Parser, Segment};
    use crate::Version;

    fn bench_optimize(data: &[u8], version: Version, bencher: &mut test::Bencher) {
        bencher.iter(|| {
            let segments: Vec<Segment> = Parser::new(data).collect();
            let segments = optimize_segmentation(segments.as_slice(), version);
            test::black_box(total_encoded_len(segments, version))
        });
    }

    #[bench]
    fn bench_optimize_example1(bencher: &mut test::Bencher) {
        let data = b"QR\x83R\x81[\x83h\x81i\x83L\x83\x85\x81[\x83A\x81[\x83\x8b\x83R\x81[\x83h\x81j\
                     \x82\xc6\x82\xcd\x81A1994\x94N\x82\xc9\x83f\x83\x93\x83\\\x81[\x82\xcc\x8aJ\
                     \x94\xad\x95\x94\x96\xe5\x81i\x8c\xbb\x8d\xdd\x82\xcd\x95\xaa\x97\xa3\x82\xb5\x83f\
                     \x83\x93\x83\\\x81[\x83E\x83F\x81[\x83u\x81j\x82\xaa\x8aJ\x94\xad\x82\xb5\x82\xbd\
                     \x83}\x83g\x83\x8a\x83b\x83N\x83X\x8c^\x93\xf1\x8e\x9f\x8c\xb3\x83R\x81[\x83h\
                     \x82\xc5\x82\xa0\x82\xe9\x81B\x82\xc8\x82\xa8\x81AQR\x83R\x81[\x83h\x82\xc6\
                     \x82\xa2\x82\xa4\x96\xbc\x8f\xcc\x81i\x82\xa8\x82\xe6\x82\xd1\x92P\x8c\xea\x81j\
                     \x82\xcd\x83f\x83\x93\x83\\\x81[\x83E\x83F\x81[\x83u\x82\xcc\x93o\x98^\x8f\xa4\
                     \x95W\x81i\x91\xe64075066\x8d\x86\x81j\x82\xc5\x82\xa0\x82\xe9\x81BQR\x82\xcd\
                     Quick Response\x82\xc9\x97R\x97\x88\x82\xb5\x81A\x8d\x82\x91\xac\x93\xc7\x82\xdd\
                     \x8e\xe6\x82\xe8\x82\xaa\x82\xc5\x82\xab\x82\xe9\x82\xe6\x82\xa4\x82\xc9\x8aJ\
                     \x94\xad\x82\xb3\x82\xea\x82\xbd\x81B\x93\x96\x8f\x89\x82\xcd\x8e\xa9\x93\xae\
                     \x8e\xd4\x95\x94\x95i\x8dH\x8f\xea\x82\xe2\x94z\x91\x97\x83Z\x83\x93\x83^\x81[\
                     \x82\xc8\x82\xc7\x82\xc5\x82\xcc\x8eg\x97p\x82\xf0\x94O\x93\xaa\x82\xc9\x8aJ\
                     \x94\xad\x82\xb3\x82\xea\x82\xbd\x82\xaa\x81A\x8c\xbb\x8d\xdd\x82\xc5\x82\xcd\x83X\
                     \x83}\x81[\x83g\x83t\x83H\x83\x93\x82\xcc\x95\x81\x8by\x82\xc8\x82\xc7\x82\xc9\
                     \x82\xe6\x82\xe8\x93\xfa\x96{\x82\xc9\x8c\xc0\x82\xe7\x82\xb8\x90\xa2\x8aE\x93I\
                     \x82\xc9\x95\x81\x8by\x82\xb5\x82\xc4\x82\xa2\x82\xe9\x81B";
        bench_optimize(data, Version::Normal(15), bencher);
    }

    #[bench]
    fn bench_optimize_base64(bencher: &mut test::Bencher) {
        let data = include_bytes!("../test_data/large_base64.in");
        bench_optimize(data, Version::Normal(40), bencher);
    }
}

//}}}
//------------------------------------------------------------------------------
//{{{ Internal types and data for parsing

/// All values of `u8` can be split into 9 different character sets when
/// determining which encoding to use. This enum represents these groupings for
/// parsing purpose.
#[derive(Copy, Clone)]
enum ExclCharSet {
    /// The end of string.
    End = 0,

    /// All symbols supported by the Alphanumeric encoding, i.e. space, `$`, `%`,
    /// `*`, `+`, `-`, `.`, `/` and `:`.
    Symbol = 1,

    /// All numbers (0–9).
    Numeric = 2,

    /// All uppercase letters (A–Z). These characters may also appear in the
    /// second byte of a Shift JIS 2-byte encoding.
    Alpha = 3,

    /// The first byte of a Shift JIS 2-byte encoding, in the range 0x81–0x9f.
    KanjiHi1 = 4,

    /// The first byte of a Shift JIS 2-byte encoding, in the range 0xe0–0xea.
    KanjiHi2 = 5,

    /// The first byte of a Shift JIS 2-byte encoding, of value 0xeb. This is
    /// different from the other two range that the second byte has a smaller
    /// range.
    KanjiHi3 = 6,

    /// The second byte of a Shift JIS 2-byte encoding, in the range 0x40–0xbf,
    /// excluding letters (covered by `Alpha`), 0x81–0x9f (covered by `KanjiHi1`),
    /// and the invalid byte 0x7f.
    KanjiLo1 = 7,

    /// The second byte of a Shift JIS 2-byte encoding, in the range 0xc0–0xfc,
    /// excluding the range 0xe0–0xeb (covered by `KanjiHi2` and `KanjiHi3`).
    /// This half of byte-pair cannot appear as the second byte leaded by
    /// `KanjiHi3`.
    KanjiLo2 = 8,

    /// Any other values not covered by the above character sets.
    Byte = 9,
}

impl ExclCharSet {
    /// Determines which character set a byte is in.
    fn from_u8(c: u8) -> Self {
        match c {
            0x20 | 0x24 | 0x25 | 0x2a | 0x2b | 0x2d..=0x2f | 0x3a => ExclCharSet::Symbol,
            0x30..=0x39 => ExclCharSet::Numeric,
            0x41..=0x5a => ExclCharSet::Alpha,
            0x81..=0x9f => ExclCharSet::KanjiHi1,
            0xe0..=0xea => ExclCharSet::KanjiHi2,
            0xeb => ExclCharSet::KanjiHi3,
            0x40 | 0x5b..=0x7e | 0x80 | 0xa0..=0xbf => ExclCharSet::KanjiLo1,
            0xc0..=0xdf | 0xec..=0xfc => ExclCharSet::KanjiLo2,
            _ => ExclCharSet::Byte,
        }
    }
}

/// The current parsing state.
#[derive(Copy, Clone)]
enum State {
    /// Just initialized.
    Init = 0,

    /// Inside a string that can be exclusively encoded as Numeric.
    Numeric = 10,

    /// Inside a string that can be exclusively encoded as Alphanumeric.
    Alpha = 20,

    /// Inside a string that can be exclusively encoded as 8-Bit Byte.
    Byte = 30,

    /// Just encountered the first byte of a Shift JIS 2-byte sequence of the
    /// set `KanjiHi1` or `KanjiHi2`.
    KanjiHi12 = 40,

    /// Just encountered the first byte of a Shift JIS 2-byte sequence of the
    /// set `KanjiHi3`.
    KanjiHi3 = 50,

    /// Inside a string that can be exclusively encoded as Kanji.
    Kanji = 60,
}

/// What should the parser do after a state transition.
#[derive(Copy, Clone)]
enum Action {
    /// The parser should do nothing.
    Idle,

    /// Push the current segment as a Numeric string, and reset the marks.
    Numeric,

    /// Push the current segment as an Alphanumeric string, and reset the marks.
    Alpha,

    /// Push the current segment as a 8-Bit Byte string, and reset the marks.
    Byte,

    /// Push the current segment as a Kanji string, and reset the marks.
    Kanji,

    /// Push the current segment excluding the last byte as a Kanji string, then
    /// push the remaining single byte as a Byte string, and reset the marks.
    KanjiAndSingleByte,
}

static STATE_TRANSITION: [(State, Action); 70] = [
    // STATE_TRANSITION[current_state + next_character] == (next_state, what_to_do)

    // Init state:
    (State::Init, Action::Idle),      // End
    (State::Alpha, Action::Idle),     // Symbol
    (State::Numeric, Action::Idle),   // Numeric
    (State::Alpha, Action::Idle),     // Alpha
    (State::KanjiHi12, Action::Idle), // KanjiHi1
    (State::KanjiHi12, Action::Idle), // KanjiHi2
    (State::KanjiHi3, Action::Idle),  // KanjiHi3
    (State::Byte, Action::Idle),      // KanjiLo1
    (State::Byte, Action::Idle),      // KanjiLo2
    (State::Byte, Action::Idle),      // Byte
    // Numeric state:
    (State::Init, Action::Numeric),      // End
    (State::Alpha, Action::Numeric),     // Symbol
    (State::Numeric, Action::Idle),      // Numeric
    (State::Alpha, Action::Numeric),     // Alpha
    (State::KanjiHi12, Action::Numeric), // KanjiHi1
    (State::KanjiHi12, Action::Numeric), // KanjiHi2
    (State::KanjiHi3, Action::Numeric),  // KanjiHi3
    (State::Byte, Action::Numeric),      // KanjiLo1
    (State::Byte, Action::Numeric),      // KanjiLo2
    (State::Byte, Action::Numeric),      // Byte
    // Alpha state:
    (State::Init, Action::Alpha),      // End
    (State::Alpha, Action::Idle),      // Symbol
    (State::Numeric, Action::Alpha),   // Numeric
    (State::Alpha, Action::Idle),      // Alpha
    (State::KanjiHi12, Action::Alpha), // KanjiHi1
    (State::KanjiHi12, Action::Alpha), // KanjiHi2
    (State::KanjiHi3, Action::Alpha),  // KanjiHi3
    (State::Byte, Action::Alpha),      // KanjiLo1
    (State::Byte, Action::Alpha),      // KanjiLo2
    (State::Byte, Action::Alpha),      // Byte
    // Byte state:
    (State::Init, Action::Byte),      // End
    (State::Alpha, Action::Byte),     // Symbol
    (State::Numeric, Action::Byte),   // Numeric
    (State::Alpha, Action::Byte),     // Alpha
    (State::KanjiHi12, Action::Byte), // KanjiHi1
    (State::KanjiHi12, Action::Byte), // KanjiHi2
    (State::KanjiHi3, Action::Byte),  // KanjiHi3
    (State::Byte, Action::Idle),      // KanjiLo1
    (State::Byte, Action::Idle),      // KanjiLo2
    (State::Byte, Action::Idle),      // Byte
    // KanjiHi12 state:
    (State::Init, Action::KanjiAndSingleByte),    // End
    (State::Alpha, Action::KanjiAndSingleByte),   // Symbol
    (State::Numeric, Action::KanjiAndSingleByte), // Numeric
    (State::Kanji, Action::Idle),                 // Alpha
    (State::Kanji, Action::Idle),                 // KanjiHi1
    (State::Kanji, Action::Idle),                 // KanjiHi2
    (State::Kanji, Action::Idle),                 // KanjiHi3
    (State::Kanji, Action::Idle),                 // KanjiLo1
    (State::Kanji, Action::Idle),                 // KanjiLo2
    (State::Byte, Action::KanjiAndSingleByte),    // Byte
    // KanjiHi3 state:
    (State::Init, Action::KanjiAndSingleByte),      // End
    (State::Alpha, Action::KanjiAndSingleByte),     // Symbol
    (State::Numeric, Action::KanjiAndSingleByte),   // Numeric
    (State::Kanji, Action::Idle),                   // Alpha
    (State::Kanji, Action::Idle),                   // KanjiHi1
    (State::KanjiHi12, Action::KanjiAndSingleByte), // KanjiHi2
    (State::KanjiHi3, Action::KanjiAndSingleByte),  // KanjiHi3
    (State::Kanji, Action::Idle),                   // KanjiLo1
    (State::Byte, Action::KanjiAndSingleByte),      // KanjiLo2
    (State::Byte, Action::KanjiAndSingleByte),      // Byte
    // Kanji state:
    (State::Init, Action::Kanji),     // End
    (State::Alpha, Action::Kanji),    // Symbol
    (State::Numeric, Action::Kanji),  // Numeric
    (State::Alpha, Action::Kanji),    // Alpha
    (State::KanjiHi12, Action::Idle), // KanjiHi1
    (State::KanjiHi12, Action::Idle), // KanjiHi2
    (State::KanjiHi3, Action::Idle),  // KanjiHi3
    (State::Byte, Action::Kanji),     // KanjiLo1
    (State::Byte, Action::Kanji),     // KanjiLo2
    (State::Byte, Action::Kanji),     // Byte
];

//}}}