Debian Codegraph @

src/encode.rs

raw symbols SCIP
1 //! Contains all functions required to encode any string as a `QRCode`
2
3 #![deny(unsafe_code)]
4 #![warn(missing_docs)]
5
6 use craterust-analyzer cargo fast_qr 0.13.1 crate/::compactrust-analyzer cargo fast_qr 0.13.1 compact/::CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#;
7 use craterust-analyzer cargo fast_qr 0.13.1 crate/::eclrust-analyzer cargo fast_qr 0.13.1 ecl/::ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#;
8 use craterust-analyzer cargo fast_qr 0.13.1 crate/::hardcoderust-analyzer cargo fast_qr 0.13.1 hardcode/;
9 use craterust-analyzer cargo fast_qr 0.13.1 crate/::versionrust-analyzer cargo fast_qr 0.13.1 version/::Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#;
10
11 /// Enum for the 3 encoding mode
12 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)]
13 pub enum Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode# {
14 /// Numeric mode (0-9 only)
15 Numericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Numeric#,
16 /// Alphanumeric mode (0-9, A-Z, $%*./:+-?.= [space])
17 Alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Alphanumeric#,
18 /// Byte mode (any)
19 Byterust-analyzer cargo fast_qr 0.13.1 encode/Mode#Byte#,
20 }
21
22 /// Encodes the string according the mode and version
23 pub fn encoderust-analyzer cargo fast_qr 0.13.1 encode/encode().(input: &[u8], ecl: ECLrust-analyzer cargo fast_qr 0.13.1 ecl/ECL#, mode: Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#, version: Versionrust-analyzer cargo fast_qr 0.13.1 version/Version#) -> CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR# {
24 let cci_bits = hardcoderust-analyzer cargo fast_qr 0.13.1 hardcode/::cci_bitsrust-analyzer cargo fast_qr 0.13.1 hardcode/cci_bits().(version, mode);
25
26 let mut compact = CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#::from_versionrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]from_version().(version);
27
28 match mode {
29 Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Numericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Numeric# => encode_numericrust-analyzer cargo fast_qr 0.13.1 encode/encode_numeric().(&mut compact, input, cci_bits),
30 Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Alphanumeric# => encode_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/encode_alphanumeric().(&mut compact, input, cci_bits),
31 Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Byterust-analyzer cargo fast_qr 0.13.1 encode/Mode#Byte# => encode_byterust-analyzer cargo fast_qr 0.13.1 encode/encode_byte().(&mut compact, input, cci_bits),
32 };
33
34 let data_bits = hardcoderust-analyzer cargo fast_qr 0.13.1 hardcode/::data_bitsrust-analyzer cargo fast_qr 0.13.1 hardcode/data_bits().(version, ecl);
35
36 add_terminatorrust-analyzer cargo fast_qr 0.13.1 encode/add_terminator().(&mut compact, data_bits);
37 pad_to_8rust-analyzer cargo fast_qr 0.13.1 encode/pad_to_8().(&mut compact);
38 compact.fillrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]fill().();
39
40 compact
41 }
42
43 /// Find the best encoding (Numeric -> Alnum -> Byte)
44 pub fn best_encodingrust-analyzer cargo fast_qr 0.13.1 encode/best_encoding().(input: &[u8]) -> Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode# {
45 fn try_encode_numericrust-analyzer cargo fast_qr 0.13.1 encode/try_encode_numeric().(input: &[u8], i: usize) -> Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode# {
46 for &c in input.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(i) {
47 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().c.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_digit().() {
48 return try_encode_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/try_encode_alphanumeric().(input, i);
49 }
50 }
51 Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Numericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Numeric#
52 }
53
54 fn try_encode_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/try_encode_alphanumeric().(input: &[u8], i: usize) -> Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode# {
55 for &c in input.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(i) {
56 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().is_qr_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/is_qr_alphanumeric().(c) {
57 return Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Byterust-analyzer cargo fast_qr 0.13.1 encode/Mode#Byte#;
58 }
59 }
60 Moderust-analyzer cargo fast_qr 0.13.1 encode/Mode#::Alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/Mode#Alphanumeric#
61 }
62
63 try_encode_numericrust-analyzer cargo fast_qr 0.13.1 encode/try_encode_numeric().(input, 0)
64 }
65
66 /// Encodes numeric strings (i.e. "123456789"), referring to 8.4.2 of the spec.
67 pub(craterust-analyzer cargo fast_qr 0.13.1 crate/) fn encode_numericrust-analyzer cargo fast_qr 0.13.1 encode/encode_numeric().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, input: &[u8], cci_bits: usize) {
68 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:)]
69 enum NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding# {
70 Singlerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Single#,
71 Doublerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Double#,
72 Triplerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Triple#,
73 }
74
75 fn encode_numberrust-analyzer cargo fast_qr 0.13.1 encode/encode_number().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, number: usize, encoding: NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#) {
76 match encoding {
77 NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Singlerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Single# => compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(number, 4),
78 NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Doublerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Double# => compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(number, 7),
79 NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Triplerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Triple# => compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(number, 10),
80 }
81 }
82
83 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(0b0001, 4);
84 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), cci_bits);
85
86 let mut i = 0;
87 let len = input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().3;
88
89 while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().len {
90 let number = ascii_to_digitrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_digit().(input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().100
91 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().ascii_to_digitrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_digit().(input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().10
92 +rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().ascii_to_digitrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_digit().(input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
93
94 encode_numberrust-analyzer cargo fast_qr 0.13.1 encode/encode_number().(compact, number, NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Triplerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Triple#);
95 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().3;
96 }
97
98 // If the length is a multiple of 3, we are done
99 if len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
100 return;
101 }
102
103 let mut number = 0;
104 while i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]lt().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
105 number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`MulAssign<Self>`]mul_assign().*=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`MulAssign<Self>`]mul_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`MulAssign<Self>`]mul_assign().10;
106 number rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().ascii_to_digitrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_digit().(input[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().i]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
107 i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
108 }
109
110 let encoding = match i rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().3 {
111 1 => NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Singlerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Single#,
112 2 => NumericEncodingrust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#::Doublerust-analyzer cargo fast_qr 0.13.1 encode/NumericEncoding#Double#,
113 _ => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!("i % 3 can only be 1 or 2"),
114 };
115
116 encode_numberrust-analyzer cargo fast_qr 0.13.1 encode/encode_number().(compact, number, encoding);
117 }
118
119 /// Encodes alphanumeric strings (i.e. "FAST-QR123"), referring to 8.4.3 of the spec.
120 pub(craterust-analyzer cargo fast_qr 0.13.1 crate/) fn encode_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/encode_alphanumeric().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, input: &[u8], cci_bits: usize) {
121 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(0b0010, 4);
122 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), cci_bits);
123
124 let even_size = input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().2;
125 for chunk in input.chunks_exactrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]chunks_exact().(2) {
126 let a = ascii_to_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_alphanumeric().(chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
127 let b = ascii_to_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_alphanumeric().(chunk[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().1]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().);
128 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(a rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().45 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().b, 11);
129 }
130 if even_size rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
131 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(ascii_to_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_alphanumeric().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().input.lastrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]last().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().()), 6);
132 }
133 }
134
135 /// Encodes any string (i.e. "<https://fast-qr.com/🚀>"), referring to 8.4.4 of the spec.
136 pub(craterust-analyzer cargo fast_qr 0.13.1 crate/) fn encode_byterust-analyzer cargo fast_qr 0.13.1 encode/encode_byte().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, input: &[u8], cci_bits: usize) {
137 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(0b0100, 4);
138 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(input.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), cci_bits);
139 compact.push_u8_slicerust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_u8_slice().(input);
140 }
141
142 /// Adds needed terminator padding, terminating the data `BitString`, referring to 8.4.8 of the spec.
143 fn add_terminatorrust-analyzer cargo fast_qr 0.13.1 encode/add_terminator().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#, data_bits: usize) {
144 let len = data_bits rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().compact.lenrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]len().();
145 let len = corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(len, 4);
146
147 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(0, len);
148 }
149
150 /// Adds the padding to make the length of the `BitString` a multiple of 8, referring to 8.4.9 of the spec.
151 fn pad_to_8rust-analyzer cargo fast_qr 0.13.1 encode/pad_to_8().(compact: &mut CompactQRrust-analyzer cargo fast_qr 0.13.1 compact/CompactQR#) {
152 let len = (8 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().compact.lenrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().8) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().%rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Rem<Self>`]rem().8;
153 compact.push_bitsrust-analyzer cargo fast_qr 0.13.1 compact/impl#[CompactQR]push_bits().(0, len);
154 }
155
156 /// Converts ascii number to it's value in usize \
157 /// "5" -> 5
158 fn ascii_to_digitrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_digit().(c: u8) -> usize {
159 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
160 c.is_ascii_digitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]is_ascii_digit().(),
161 "Unexpected character '{}' in Numeric mode",
162 c as char
163 );
164 (c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().b'0') as usize
165 }
166
167 /// Converts ascii alnum to it's numeric value, characters included in `AlphaNumeric` are: \
168 /// 0-9, A-Z, $%*./:+-?.= [space] \
169 /// referring to 7.1 of the spec.
170 pub(craterust-analyzer cargo fast_qr 0.13.1 crate/) fn ascii_to_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/ascii_to_alphanumeric().(c: u8) -> usize {
171 match c {
172 b'0'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'9' => (c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().b'0') as usize,
173 b'A'..=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeInclusive#b'Z' => (c rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u8][`Sub<Self>`]sub().b'A') as usize rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().10,
174 b' ' => 36,
175 b'$' => 37,
176 b'%' => 38,
177 b'*' => 39,
178 b'+' => 40,
179 b'-' => 41,
180 b'.' => 42,
181 b'/' => 43,
182 b':' => 44,
183 _ => panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("Unexpected character '{}' in Alphanumeric mode", c as char),
184 }
185 }
186
187 /// Checks if character c is alphanumeric: 0-9, A-Z, $%*./:+-?.= [space] \
188 /// referring to 7.1 of the spec.
189 const fn is_qr_alphanumericrust-analyzer cargo fast_qr 0.13.1 encode/is_qr_alphanumeric().(c: u8) -> bool {
190 matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/matches!!(c,
191 b'A'..=b'Z'
192 | b'0'..=b'9'
193 | b' '
194 | b'$'
195 | b'%'
196 | b'*'
197 | b'+'
198 | b'-'
199 | b'.'
200 | b'/'
201 | b':')
202 }