Skip to content

Commit

Permalink
Fix the conflict.rs/parse_unique_name fn for non ASCII characters.
Browse files Browse the repository at this point in the history
  • Loading branch information
inetic committed Jan 8, 2024
1 parent 44bff08 commit a59c71a
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions lib/src/conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@ pub fn create_unique_name(name: &str, branch_id: &PublicKey) -> String {

/// Parse a name created with `create_unique_name` into the original name and the disambiguation
/// suffix.
pub fn parse_unique_name(name: &str) -> (&str, Option<[u8; SUFFIX_LEN / 2]>) {
let index = if let Some(index) = name.len().checked_sub(SUFFIX_LEN + SUFFIX_SEPARATOR.len()) {
index
} else {
return (name, None);
};
pub fn parse_unique_name(unique_name: &str) -> (&str, Option<[u8; SUFFIX_LEN / 2]>) {
let mut split = unique_name.rsplitn(2, &SUFFIX_SEPARATOR);

if split.clone().count() < 2 {
return (unique_name, None);
}

// Unwraps below OK because of the above theck.

if &name[index..index + SUFFIX_SEPARATOR.len()] != SUFFIX_SEPARATOR {
return (name, None);
let suffix = split.next().unwrap();

if suffix.len() != SUFFIX_LEN {
return (unique_name, None);
}

let mut suffix = [0; SUFFIX_LEN / 2];
let mut suffix_bytes = [0; SUFFIX_LEN / 2];

if hex::decode_to_slice(&name[index + 2..], &mut suffix).is_ok() {
(&name[..index], Some(suffix))
if hex::decode_to_slice(suffix, &mut suffix_bytes).is_ok() {
let name = split.next().unwrap();
(name, Some(suffix_bytes))
} else {
(name, None)
(unique_name, None)
}
}

Expand Down Expand Up @@ -69,4 +74,12 @@ mod tests {
assert_eq!(parsed_base_name, base_name);
assert!(branch_id.starts_with(&branch_id_prefix));
}

#[test]
fn parse_chinesse_file_name() {
let filename = "复制.txt";
let (newfilename, suffix) = parse_unique_name(&filename);
assert!(suffix.is_none());
assert_eq!(filename, newfilename);
}
}

0 comments on commit a59c71a

Please sign in to comment.