mirror of
https://github.com/openharmony/third_party_qrcodegen.git
synced 2026-07-19 20:23:35 -04:00
Changed Rust functions to take &str instead of &[char] because that type is more natural to use, removed intermediate step of making Vec<char>, updated demo program to fit.
This commit is contained in:
@@ -92,8 +92,8 @@ fn do_segment_demo() {
|
||||
print_qr(&qr);
|
||||
|
||||
let segs = vec![
|
||||
QrSegment::make_alphanumeric(&to_chars(silver0)),
|
||||
QrSegment::make_numeric(&to_chars(silver1)),
|
||||
QrSegment::make_alphanumeric(silver0),
|
||||
QrSegment::make_numeric(silver1),
|
||||
];
|
||||
let qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();
|
||||
print_qr(&qr);
|
||||
@@ -107,8 +107,8 @@ fn do_segment_demo() {
|
||||
|
||||
let segs = vec![
|
||||
QrSegment::make_bytes(golden0.as_bytes()),
|
||||
QrSegment::make_numeric(&to_chars(golden1)),
|
||||
QrSegment::make_alphanumeric(&to_chars(golden2)),
|
||||
QrSegment::make_numeric(golden1),
|
||||
QrSegment::make_alphanumeric(golden2),
|
||||
];
|
||||
let qr = QrCode::encode_segments(&segs, QrCodeEcc::Low).unwrap();
|
||||
print_qr(&qr);
|
||||
@@ -141,14 +141,14 @@ fn do_segment_demo() {
|
||||
// Creates QR Codes with the same size and contents but different mask patterns.
|
||||
fn do_mask_demo() {
|
||||
// Project Nayuki URL
|
||||
let segs = QrSegment::make_segments(&to_chars("https://www.nayuki.io/"));
|
||||
let segs = QrSegment::make_segments("https://www.nayuki.io/");
|
||||
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, None, true).unwrap(); // Automatic mask
|
||||
print_qr(&qr);
|
||||
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::High, Version::MIN, Version::MAX, Some(Mask::new(3)), true).unwrap(); // Force mask 3
|
||||
print_qr(&qr);
|
||||
|
||||
// Chinese text as UTF-8
|
||||
let segs = QrSegment::make_segments(&to_chars("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫"));
|
||||
let segs = QrSegment::make_segments("維基百科(Wikipedia,聆聽i/ˌwɪkᵻˈpiːdi.ə/)是一個自由內容、公開編輯且多語言的網路百科全書協作計畫");
|
||||
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(0)), true).unwrap(); // Force mask 0
|
||||
print_qr(&qr);
|
||||
let qr = QrCode::encode_segments_advanced(&segs, QrCodeEcc::Medium, Version::MIN, Version::MAX, Some(Mask::new(1)), true).unwrap(); // Force mask 1
|
||||
@@ -204,9 +204,3 @@ fn print_qr(qr: &QrCode) {
|
||||
}
|
||||
println!();
|
||||
}
|
||||
|
||||
|
||||
// Converts the given borrowed string slice to a new character vector.
|
||||
fn to_chars(text: &str) -> Vec<char> {
|
||||
text.chars().collect()
|
||||
}
|
||||
|
||||
+14
-16
@@ -152,8 +152,7 @@ impl QrCode {
|
||||
/// Returns a wrapped `QrCode` if successful, or `Err` if the
|
||||
/// data is too long to fit in any version at the given ECC level.
|
||||
pub fn encode_text(text: &str, ecl: QrCodeEcc) -> Result<Self,DataTooLong> {
|
||||
let chrs: Vec<char> = text.chars().collect();
|
||||
let segs: Vec<QrSegment> = QrSegment::make_segments(&chrs);
|
||||
let segs: Vec<QrSegment> = QrSegment::make_segments(text);
|
||||
QrCode::encode_segments(&segs, ecl)
|
||||
}
|
||||
|
||||
@@ -979,13 +978,13 @@ impl QrSegment {
|
||||
/// Returns a segment representing the given string of decimal digits encoded in numeric mode.
|
||||
///
|
||||
/// Panics if the string contains non-digit characters.
|
||||
pub fn make_numeric(text: &[char]) -> Self {
|
||||
pub fn make_numeric(text: &str) -> Self {
|
||||
let mut bb = BitBuffer(Vec::with_capacity(text.len() * 3 + (text.len() + 2) / 3));
|
||||
let mut accumdata: u32 = 0;
|
||||
let mut accumcount: u8 = 0;
|
||||
for &c in text {
|
||||
assert!('0' <= c && c <= '9', "String contains non-numeric characters");
|
||||
accumdata = accumdata * 10 + (u32::from(c) - u32::from('0'));
|
||||
for b in text.bytes() {
|
||||
assert!(b'0' <= b && b <= b'9', "String contains non-numeric characters");
|
||||
accumdata = accumdata * 10 + (u32::from(b) - u32::from(b'0'));
|
||||
accumcount += 1;
|
||||
if accumcount == 3 {
|
||||
bb.append_bits(accumdata, 10);
|
||||
@@ -1006,12 +1005,12 @@ impl QrSegment {
|
||||
/// dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||
///
|
||||
/// Panics if the string contains non-encodable characters.
|
||||
pub fn make_alphanumeric(text: &[char]) -> Self {
|
||||
pub fn make_alphanumeric(text: &str) -> Self {
|
||||
let mut bb = BitBuffer(Vec::with_capacity(text.len() * 5 + (text.len() + 1) / 2));
|
||||
let mut accumdata: u32 = 0;
|
||||
let mut accumcount: u32 = 0;
|
||||
for &c in text {
|
||||
let i: usize = ALPHANUMERIC_CHARSET.iter().position(|&x| x == c)
|
||||
for b in text.bytes() {
|
||||
let i: usize = ALPHANUMERIC_CHARSET.iter().position(|&x| x == char::from(b))
|
||||
.expect("String contains unencodable characters in alphanumeric mode");
|
||||
accumdata = accumdata * 45 + (i as u32);
|
||||
accumcount += 1;
|
||||
@@ -1032,7 +1031,7 @@ impl QrSegment {
|
||||
///
|
||||
/// The result may use various segment modes and switch
|
||||
/// modes to optimize the length of the bit stream.
|
||||
pub fn make_segments(text: &[char]) -> Vec<Self> {
|
||||
pub fn make_segments(text: &str) -> Vec<Self> {
|
||||
if text.is_empty() {
|
||||
vec![]
|
||||
} else {
|
||||
@@ -1042,8 +1041,7 @@ impl QrSegment {
|
||||
} else if QrSegment::is_alphanumeric(text) {
|
||||
QrSegment::make_alphanumeric(text)
|
||||
} else {
|
||||
let s: String = text.iter().cloned().collect();
|
||||
QrSegment::make_bytes(s.as_bytes())
|
||||
QrSegment::make_bytes(text.as_bytes())
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1125,8 +1123,8 @@ impl QrSegment {
|
||||
/// Tests whether the given string can be encoded as a segment in numeric mode.
|
||||
///
|
||||
/// A string is encodable iff each character is in the range 0 to 9.
|
||||
pub fn is_numeric(text: &[char]) -> bool {
|
||||
text.iter().all(|&c| '0' <= c && c <= '9')
|
||||
pub fn is_numeric(text: &str) -> bool {
|
||||
text.bytes().all(|b| b'0' <= b && b <= b'9')
|
||||
}
|
||||
|
||||
|
||||
@@ -1134,8 +1132,8 @@ impl QrSegment {
|
||||
///
|
||||
/// A string is encodable iff each character is in the following set: 0 to 9, A to Z
|
||||
/// (uppercase only), space, dollar, percent, asterisk, plus, hyphen, period, slash, colon.
|
||||
pub fn is_alphanumeric(text: &[char]) -> bool {
|
||||
text.iter().all(|c| ALPHANUMERIC_CHARSET.contains(c))
|
||||
pub fn is_alphanumeric(text: &str) -> bool {
|
||||
text.bytes().all(|b| ALPHANUMERIC_CHARSET.contains(&char::from(b)))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user