rust/hg-core/src/utils.rs
changeset 46708 e8cd519a0a34
parent 46653 a069639783a0
child 46755 91ab5190a3de
equal deleted inserted replaced
46707:fb2368598281 46708:e8cd519a0a34
    65         }
    65         }
    66     }
    66     }
    67 }
    67 }
    68 
    68 
    69 pub trait SliceExt {
    69 pub trait SliceExt {
       
    70     fn trim_end_newlines(&self) -> &Self;
    70     fn trim_end(&self) -> &Self;
    71     fn trim_end(&self) -> &Self;
    71     fn trim_start(&self) -> &Self;
    72     fn trim_start(&self) -> &Self;
    72     fn trim(&self) -> &Self;
    73     fn trim(&self) -> &Self;
    73     fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
    74     fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
    74     fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])>;
    75     fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])>;
    78 fn is_not_whitespace(c: &u8) -> bool {
    79 fn is_not_whitespace(c: &u8) -> bool {
    79     !(*c as char).is_whitespace()
    80     !(*c as char).is_whitespace()
    80 }
    81 }
    81 
    82 
    82 impl SliceExt for [u8] {
    83 impl SliceExt for [u8] {
       
    84     fn trim_end_newlines(&self) -> &[u8] {
       
    85         if let Some(last) = self.iter().rposition(|&byte| byte != b'\n') {
       
    86             &self[..=last]
       
    87         } else {
       
    88             &[]
       
    89         }
       
    90     }
    83     fn trim_end(&self) -> &[u8] {
    91     fn trim_end(&self) -> &[u8] {
    84         if let Some(last) = self.iter().rposition(is_not_whitespace) {
    92         if let Some(last) = self.iter().rposition(is_not_whitespace) {
    85             &self[..=last]
    93             &self[..=last]
    86         } else {
    94         } else {
    87             &[]
    95             &[]