mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-27 21:02:35 +00:00
all address book tests pass
This commit is contained in:
@@ -10,6 +10,7 @@ exclude = [
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
regex = "1.6"
|
regex = "1.6"
|
||||||
|
fancy-regex = "0.10"
|
||||||
encoding = "0.2"
|
encoding = "0.2"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
urlencoding = "2.1.2"
|
urlencoding = "2.1.2"
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ use uriparse::URI;
|
|||||||
|
|
||||||
use super::{AddressBookParsedRXingResult, ParsedClientResult, ResultParser};
|
use super::{AddressBookParsedRXingResult, ParsedClientResult, ResultParser};
|
||||||
|
|
||||||
const BEGIN_VCARD: &'static str = "BEGIN:VCARD"; //, Pattern.CASE_INSENSITIVE);
|
const BEGIN_VCARD: &'static str = "(?i:BEGIN:VCARD)"; //, Pattern.CASE_INSENSITIVE);
|
||||||
const VCARD_LIKE_DATE: &'static str = "\\d{4}-?\\d{2}-?\\d{2}";
|
const VCARD_LIKE_DATE: &'static str = "\\d{4}-?\\d{2}-?\\d{2}";
|
||||||
const CR_LF_SPACE_TAB: &'static str = "\r\n[ \t]";
|
const CR_LF_SPACE_TAB: &'static str = "\r\n[ \t]";
|
||||||
const NEWLINE_ESCAPE: &'static str = "\\\\[nN]";
|
const NEWLINE_ESCAPE: &'static str = "\\\\[nN]";
|
||||||
@@ -70,12 +70,13 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
|
|||||||
if mtch.start() != 0 {
|
if mtch.start() != 0 {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let names: Vec<Vec<String>> =
|
let names: Vec<Vec<String>> =
|
||||||
if let Some(m) = matchVCardPrefixedField("FN", &rawText, true, false) {
|
if let Some(m) = matchVCardPrefixedField("FN", &rawText, true, false) {
|
||||||
m
|
m
|
||||||
} else {
|
} else {
|
||||||
// If no display names found, look for regular name fields and format them
|
// If no display names found, look for regular name fields and format them
|
||||||
let mut n = matchVCardPrefixedField("N", &rawText, true, false)?;
|
let mut n = matchVCardPrefixedField("N", &rawText, true, false).unwrap_or_default();
|
||||||
formatNames(&mut n);
|
formatNames(&mut n);
|
||||||
n
|
n
|
||||||
};
|
};
|
||||||
@@ -129,21 +130,21 @@ pub fn parse(result: &RXingResult) -> Option<ParsedClientResult> {
|
|||||||
// geo = null;
|
// geo = null;
|
||||||
// }
|
// }
|
||||||
if let Ok(adb) = AddressBookParsedRXingResult::with_details(
|
if let Ok(adb) = AddressBookParsedRXingResult::with_details(
|
||||||
toPrimaryValues(Some(names))?,
|
toPrimaryValues(Some(names)),
|
||||||
nicknames,
|
nicknames,
|
||||||
"".to_owned(),
|
"".to_owned(),
|
||||||
toPrimaryValues(phoneNumbers.clone())?,
|
toPrimaryValues(phoneNumbers.clone()),
|
||||||
toTypes(phoneNumbers)?,
|
toTypes(phoneNumbers),
|
||||||
toPrimaryValues(emails.clone())?,
|
toPrimaryValues(emails.clone()),
|
||||||
toTypes(emails)?,
|
toTypes(emails),
|
||||||
toPrimaryValue(instantMessenger),
|
toPrimaryValue(instantMessenger),
|
||||||
toPrimaryValue(note),
|
toPrimaryValue(note),
|
||||||
toPrimaryValues(addresses.clone())?,
|
toPrimaryValues(addresses.clone()),
|
||||||
toTypes(addresses)?,
|
toTypes(addresses),
|
||||||
toPrimaryValue(org),
|
toPrimaryValue(org),
|
||||||
toPrimaryValue(Some(birthday)),
|
toPrimaryValue(Some(birthday)),
|
||||||
toPrimaryValue(title),
|
toPrimaryValue(title),
|
||||||
toPrimaryValues(urls)?,
|
toPrimaryValues(urls),
|
||||||
geo,
|
geo,
|
||||||
) {
|
) {
|
||||||
Some(ParsedClientResult::AddressBookResult(adb))
|
Some(ParsedClientResult::AddressBookResult(adb))
|
||||||
@@ -163,15 +164,17 @@ fn matchVCardPrefixedField(
|
|||||||
let max = rawText.len();
|
let max = rawText.len();
|
||||||
|
|
||||||
let equals_regex = Regex::new(EQUALS).unwrap();
|
let equals_regex = Regex::new(EQUALS).unwrap();
|
||||||
let unescaped_semis = Regex::new(UNESCAPED_SEMICOLONS).unwrap();
|
let unescaped_semis = fancy_regex::Regex::new(UNESCAPED_SEMICOLONS).unwrap();
|
||||||
let cr_lf_space_tab = Regex::new(CR_LF_SPACE_TAB).unwrap();
|
let cr_lf_space_tab = Regex::new(CR_LF_SPACE_TAB).unwrap();
|
||||||
let newline_esc = Regex::new(NEWLINE_ESCAPE).unwrap();
|
let newline_esc = Regex::new(NEWLINE_ESCAPE).unwrap();
|
||||||
let vcard_esc = Regex::new(VCARD_ESCAPES).unwrap();
|
let vcard_esc = Regex::new(VCARD_ESCAPES).unwrap();
|
||||||
|
|
||||||
// At start or after newline, match prefix, followed by optional metadata
|
// At start or after newline, match prefix, followed by optional metadata
|
||||||
// (led by ;) ultimately ending in colon
|
// (led by ;) ultimately ending in colon
|
||||||
let matcher_primary = Regex::new(&format!("(?:^|\n){}(?:;([^:]*))?:", prefix)).unwrap();
|
let matcher_primary = Regex::new(&format!("(?:^|\\n)(?i:{})(?:;([^:]*))?:", prefix)).unwrap();
|
||||||
let lower_case_raw_text = rawText.to_lowercase();
|
// let matcher_primary = Regex::new(&format!("(?:^|\n){}(.*)", prefix)).unwrap();
|
||||||
|
|
||||||
|
//let lower_case_raw_text = rawText.to_lowercase();
|
||||||
|
|
||||||
while i < max {
|
while i < max {
|
||||||
//let rawText = rawText.to_lowercase();
|
//let rawText = rawText.to_lowercase();
|
||||||
@@ -179,13 +182,14 @@ fn matchVCardPrefixedField(
|
|||||||
if i > 0 {
|
if i > 0 {
|
||||||
i -= 1; // Find from i-1 not i since looking at the preceding character
|
i -= 1; // Find from i-1 not i since looking at the preceding character
|
||||||
}
|
}
|
||||||
let cap_maybe = matcher_primary.captures(&lower_case_raw_text[i..]);
|
let cap_text = &rawText[i..];
|
||||||
|
let cap_maybe = matcher_primary.captures(cap_text);
|
||||||
//let matcher_maybe = matcher_primary.find_at(&lower_case_raw_text, i);
|
//let matcher_maybe = matcher_primary.find_at(&lower_case_raw_text, i);
|
||||||
if cap_maybe.is_none() {
|
if cap_maybe.is_none() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
let matcher = cap_maybe?;
|
let matcher = cap_maybe?;
|
||||||
i = matcher.get(0)?.end(); // group 0 = whole pattern; end(0) is past final colon
|
i += matcher.get(0)?.end(); // group 0 = whole pattern; end(0) is past final colon
|
||||||
|
|
||||||
let metadataString = matcher.get(1); // group 1 = metadata substring
|
let metadataString = matcher.get(1); // group 1 = metadata substring
|
||||||
let mut metadata: Vec<String> = Vec::new();
|
let mut metadata: Vec<String> = Vec::new();
|
||||||
@@ -221,7 +225,7 @@ fn matchVCardPrefixedField(
|
|||||||
let matchStart = i; // Found the start of a match here
|
let matchStart = i; // Found the start of a match here
|
||||||
while let Some(pos) = rawText[i..].find('\n') {
|
while let Some(pos) = rawText[i..].find('\n') {
|
||||||
// Really, end in \r\n
|
// Really, end in \r\n
|
||||||
i = pos;
|
i += pos; // + i;
|
||||||
// while (i = rawText.indexOf('\n', i)) >= 0 { // Really, end in \r\n
|
// while (i = rawText.indexOf('\n', i)) >= 0 { // Really, end in \r\n
|
||||||
if i < rawText.len() - 1 && // But if followed by tab or space,
|
if i < rawText.len() - 1 && // But if followed by tab or space,
|
||||||
(rawText.chars().nth(i + 1)? == ' ' || // this is only a continuation
|
(rawText.chars().nth(i + 1)? == ' ' || // this is only a continuation
|
||||||
@@ -288,7 +292,7 @@ fn matchVCardPrefixedField(
|
|||||||
// Don't actually support dereferencing URIs, but use scheme-specific part not URI
|
// Don't actually support dereferencing URIs, but use scheme-specific part not URI
|
||||||
// as value, to support tel: and mailto:
|
// as value, to support tel: and mailto:
|
||||||
if let Ok(uri) = URI::try_from(element.as_str()) {
|
if let Ok(uri) = URI::try_from(element.as_str()) {
|
||||||
element = uri.scheme().to_string();
|
element = uri.path().to_string();
|
||||||
}
|
}
|
||||||
// try {
|
// try {
|
||||||
// element = URI.create(element).getSchemeSpecificPart();
|
// element = URI.create(element).getSchemeSpecificPart();
|
||||||
@@ -309,8 +313,11 @@ fn matchVCardPrefixedField(
|
|||||||
i += 1;
|
i += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if matches.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
Some(matches)
|
Some(matches)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn decodeQuotedPrintable(value: &str, charset: &str) -> String {
|
fn decodeQuotedPrintable(value: &str, charset: &str) -> String {
|
||||||
@@ -322,9 +329,11 @@ fn decodeQuotedPrintable(value: &str, charset: &str) -> String {
|
|||||||
while i < length {
|
while i < length {
|
||||||
// for (int i = 0; i < length; i++) {
|
// for (int i = 0; i < length; i++) {
|
||||||
let c = value.chars().nth(i).unwrap_or_default();
|
let c = value.chars().nth(i).unwrap_or_default();
|
||||||
match c {
|
if c == '\r' || c == '\n' {
|
||||||
'\r' | '\n' => break,
|
i += 1;
|
||||||
'=' if i < length - 2 => {
|
continue;
|
||||||
|
}
|
||||||
|
if c == '=' && i < length - 2 {
|
||||||
let nextChar = value.chars().nth(i + 1).unwrap();
|
let nextChar = value.chars().nth(i + 1).unwrap();
|
||||||
if nextChar != '\r' && nextChar != '\n' {
|
if nextChar != '\r' && nextChar != '\n' {
|
||||||
let nextNextChar = value.chars().nth(i + 2).unwrap();
|
let nextNextChar = value.chars().nth(i + 2).unwrap();
|
||||||
@@ -335,13 +344,31 @@ fn decodeQuotedPrintable(value: &str, charset: &str) -> String {
|
|||||||
} // else ignore it, assume it was incorrectly encoded
|
} // else ignore it, assume it was incorrectly encoded
|
||||||
i += 2;
|
i += 2;
|
||||||
}
|
}
|
||||||
break;
|
i += 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
_ => {
|
|
||||||
maybeAppendFragment(&mut fragmentBuffer, charset, &mut result);
|
maybeAppendFragment(&mut fragmentBuffer, charset, &mut result);
|
||||||
result.push(c);
|
result.push(c);
|
||||||
}
|
i += 1;
|
||||||
}
|
// match c {
|
||||||
|
// '\r' | '\n' => break,
|
||||||
|
// '=' if i < length - 2 => {
|
||||||
|
// let nextChar = value.chars().nth(i + 1).unwrap();
|
||||||
|
// if nextChar != '\r' && nextChar != '\n' {
|
||||||
|
// let nextNextChar = value.chars().nth(i + 2).unwrap();
|
||||||
|
// let firstDigit = ResultParser::parseHexDigit(nextChar);
|
||||||
|
// let secondDigit = ResultParser::parseHexDigit(nextNextChar);
|
||||||
|
// if firstDigit >= 0 && secondDigit >= 0 {
|
||||||
|
// fragmentBuffer.push(((firstDigit << 4) + secondDigit) as u8);
|
||||||
|
// } // else ignore it, assume it was incorrectly encoded
|
||||||
|
// i += 2;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// _ => {
|
||||||
|
// maybeAppendFragment(&mut fragmentBuffer, charset, &mut result);
|
||||||
|
// result.push(c);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
// switch (c) {
|
// switch (c) {
|
||||||
// case '\r':
|
// case '\r':
|
||||||
// case '\n':
|
// case '\n':
|
||||||
@@ -432,38 +459,41 @@ fn toPrimaryValue(list: Option<Vec<String>>) -> String {
|
|||||||
// return list == null || list.isEmpty() ? null : list.get(0);
|
// return list == null || list.isEmpty() ? null : list.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toPrimaryValues(lists: Option<Vec<Vec<String>>>) -> Option<Vec<String>> {
|
fn toPrimaryValues(lists: Option<Vec<Vec<String>>>) -> Vec<String> {
|
||||||
let local_lists = lists?;
|
let local_lists = lists.unwrap_or_default();
|
||||||
if local_lists.is_empty() {
|
if local_lists.is_empty() {
|
||||||
return None;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
let mut result = vec!["".to_owned(); local_lists.len()]; // new ArrayList<>(lists.size());
|
let mut result = Vec::with_capacity(local_lists.len()); // new ArrayList<>(lists.size());
|
||||||
for list in local_lists {
|
for list in local_lists {
|
||||||
// for (List<String> list : lists) {
|
// for (List<String> list : lists) {
|
||||||
if let Some(value) = list.get(0) {
|
let position = if list.len() > 1 { list.len() - 1 } else { 0 };
|
||||||
|
if let Some(value) = list.get(position) {
|
||||||
if !value.is_empty() {
|
if !value.is_empty() {
|
||||||
result.push(value.clone());
|
result.push(value.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(result)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn toTypes(lists: Option<Vec<Vec<String>>>) -> Option<Vec<String>> {
|
fn toTypes(lists: Option<Vec<Vec<String>>>) -> Vec<String> {
|
||||||
let local_lists = lists?;
|
let local_lists = lists.unwrap_or_default();
|
||||||
if local_lists.is_empty() {
|
if local_lists.is_empty() {
|
||||||
return None;
|
return Vec::new();
|
||||||
}
|
}
|
||||||
let mut result = vec!["".to_owned(); local_lists.len()]; //new ArrayList<>(lists.size());
|
let mut result = Vec::with_capacity(local_lists.len()); //new ArrayList<>(lists.size());
|
||||||
for list in local_lists {
|
for list in local_lists {
|
||||||
// for (List<String> list : lists) {
|
// for (List<String> list : lists) {
|
||||||
if let Some(value) = list.get(0) {
|
if let Some(value) = list.get(0) {
|
||||||
if !value.is_empty() {
|
if !value.is_empty() {
|
||||||
let mut v_type = String::new();
|
let mut v_type = String::new();
|
||||||
for i in 0..list.len() {
|
let final_value = list.get(list.len() - 1).unwrap_or(&"".to_owned()).clone();
|
||||||
|
if !final_value.is_empty() {
|
||||||
|
for i in 0..list.len() - 1 {
|
||||||
// for (int i = 1; i < list.size(); i++) {
|
// for (int i = 1; i < list.size(); i++) {
|
||||||
let metadatum = list.get(i)?;
|
let metadatum = list.get(i).unwrap_or(&"".to_owned()).clone();
|
||||||
if let Some(equals) = metadatum.find('=') {
|
if let Some(equals) = metadatum.find('=') {
|
||||||
if "TYPE" == (metadatum[0..equals]).to_uppercase() {
|
if "TYPE" == (metadatum[0..equals]).to_uppercase() {
|
||||||
v_type = metadatum[equals + 1..].to_owned();
|
v_type = metadatum[equals + 1..].to_owned();
|
||||||
@@ -480,8 +510,9 @@ fn toTypes(lists: Option<Vec<Vec<String>>>) -> Option<Vec<String>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Some(result)
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn isLikeVCardDate(value: &str) -> bool {
|
fn isLikeVCardDate(value: &str) -> bool {
|
||||||
@@ -505,16 +536,21 @@ fn formatNames(names: &mut Vec<Vec<String>>) {
|
|||||||
if !names.is_empty() {
|
if !names.is_empty() {
|
||||||
for list in names {
|
for list in names {
|
||||||
// for (List<String> list : names) {
|
// for (List<String> list : names) {
|
||||||
let name = list.get(0).unwrap_or(&"".to_owned()).clone();
|
let mut pos = 0;
|
||||||
|
while let Some(_fnd) = list.get(pos).unwrap_or(&"".to_owned()).find("=") {
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
let name = list.get(pos).unwrap_or(&"".to_owned()).clone();
|
||||||
|
|
||||||
let mut components = vec!["".to_owned(); 5];
|
let mut components = vec!["".to_owned(); 5];
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
let mut end = 0;
|
let mut end = 0;
|
||||||
let mut componentIndex = 0;
|
let mut componentIndex = 0;
|
||||||
while componentIndex < components.len() - 1 && end >= 0 {
|
while componentIndex < components.len() - 1 && end < name.len() {
|
||||||
end = if let Some(pos) = name[start..].find(';') {
|
end = if let Some(pos) = name[start..].find(';') {
|
||||||
pos + start
|
pos + start
|
||||||
} else {
|
} else {
|
||||||
0
|
break;
|
||||||
};
|
};
|
||||||
components[componentIndex] = name[start..end].to_owned();
|
components[componentIndex] = name[start..end].to_owned();
|
||||||
componentIndex += 1;
|
componentIndex += 1;
|
||||||
@@ -527,7 +563,7 @@ fn formatNames(names: &mut Vec<Vec<String>>) {
|
|||||||
maybeAppendComponent(&components, 2, &mut newName);
|
maybeAppendComponent(&components, 2, &mut newName);
|
||||||
maybeAppendComponent(&components, 0, &mut newName);
|
maybeAppendComponent(&components, 0, &mut newName);
|
||||||
maybeAppendComponent(&components, 4, &mut newName);
|
maybeAppendComponent(&components, 4, &mut newName);
|
||||||
list[0] = newName.trim().to_owned();
|
list[pos] = newName.trim().to_owned();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user