Move account name escaping code to the end
This commit is contained in:
@@ -21,59 +21,6 @@ pub struct Connection {
|
||||
ctx: Arc<RwLock<Context>>,
|
||||
}
|
||||
|
||||
fn escape_one(b: u8) -> String {
|
||||
format!("_{:0<2x}", b)
|
||||
}
|
||||
|
||||
// Some non-empty sequence of ASCII letters, digits and underscores
|
||||
fn escape(s: String) -> String {
|
||||
// Special-case the empty string
|
||||
if s.len() == 0 {
|
||||
return "_".to_string();
|
||||
}
|
||||
|
||||
let bytes = s.into_bytes();
|
||||
let mut iter = bytes.iter();
|
||||
let mut out = String::new();
|
||||
|
||||
// Only alphanumeric in the first byte
|
||||
let x = *iter.next().expect("Already checked len > 0");
|
||||
let first = match x {
|
||||
b'a'..=b'z' | b'A'..=b'Z' => unsafe { String::from_utf8_unchecked(vec![x]) },
|
||||
_ => escape_one(x),
|
||||
};
|
||||
|
||||
out.push_str(&first);
|
||||
|
||||
for x in iter {
|
||||
let next = match x {
|
||||
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => unsafe {
|
||||
String::from_utf8_unchecked(vec![*x])
|
||||
},
|
||||
_ => escape_one(*x),
|
||||
};
|
||||
|
||||
out.push_str(&next);
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_escape() {
|
||||
assert_eq!(escape("".to_string()), "_");
|
||||
assert_eq!(escape("foo".to_string()), "foo");
|
||||
assert_eq!(escape("foo@bar".to_string()), "foo_40bar");
|
||||
assert_eq!(escape("foo_bar".to_string()), "foo_5fbar");
|
||||
assert_eq!(escape("foo__@__bar".to_string()), "foo_5f_5f_40_5f_5fbar");
|
||||
assert_eq!(escape("1foo".to_string()), "_31foo");
|
||||
}
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn new(params: HashMap<&str, super::Variant>) -> Result<Self, dbus::tree::MethodErr> {
|
||||
let err = Err(dbus::tree::MethodErr::no_arg());
|
||||
@@ -390,3 +337,56 @@ impl telepathy::Connection for Connection {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
||||
fn escape_one(b: u8) -> String {
|
||||
format!("_{:0<2x}", b)
|
||||
}
|
||||
|
||||
// Some non-empty sequence of ASCII letters, digits and underscores
|
||||
fn escape(s: String) -> String {
|
||||
// Special-case the empty string
|
||||
if s.len() == 0 {
|
||||
return "_".to_string();
|
||||
}
|
||||
|
||||
let bytes = s.into_bytes();
|
||||
let mut iter = bytes.iter();
|
||||
let mut out = String::new();
|
||||
|
||||
// Only alphanumeric in the first byte
|
||||
let x = *iter.next().expect("Already checked len > 0");
|
||||
let first = match x {
|
||||
b'a'..=b'z' | b'A'..=b'Z' => unsafe { String::from_utf8_unchecked(vec![x]) },
|
||||
_ => escape_one(x),
|
||||
};
|
||||
|
||||
out.push_str(&first);
|
||||
|
||||
for x in iter {
|
||||
let next = match x {
|
||||
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => unsafe {
|
||||
String::from_utf8_unchecked(vec![*x])
|
||||
},
|
||||
_ => escape_one(*x),
|
||||
};
|
||||
|
||||
out.push_str(&next);
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_escape() {
|
||||
assert_eq!(escape("".to_string()), "_");
|
||||
assert_eq!(escape("foo".to_string()), "foo");
|
||||
assert_eq!(escape("foo@bar".to_string()), "foo_40bar");
|
||||
assert_eq!(escape("foo_bar".to_string()), "foo_5fbar");
|
||||
assert_eq!(escape("foo__@__bar".to_string()), "foo_5f_5f_40_5f_5fbar");
|
||||
assert_eq!(escape("1foo".to_string()), "_31foo");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user