1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
//! This crate provides functions to deal with slashes in strings. //! //! ## Examples //! //! To see examples, check out the documentation for each function. /// Delete an ending slash in a string except for '/'. /// /// ``` /// extern crate slash_formatter; /// /// assert_eq!("path", slash_formatter::delete_end_slash("path/")); /// ``` pub fn delete_end_slash(s: &str) -> &str { let length = s.len(); if length > 1 && s.ends_with('/') { return &s[..length - 1]; } else { return s; } } /// Delete an ending slash in a string except for '/'. /// /// ``` /// extern crate slash_formatter; /// /// let s = String::from("path/"); /// /// let s = slash_formatter::delete_end_slash_owned(s); /// /// assert_eq!("path", s); /// ``` pub fn delete_end_slash_owned(mut s: String) -> String { let length = s.len(); if length > 1 && s.ends_with('/') { s.remove(length - 1); } return s; } /// Delete an starting slash in a string except for '/'. /// /// ``` /// extern crate slash_formatter; /// /// assert_eq!("path", slash_formatter::delete_start_slash("/path")); /// ``` pub fn delete_start_slash(s: &str) -> &str { let length = s.len(); if length > 1 && s.starts_with('/') { return &s[1..]; } else { return s; } } /// Delete an starting slash in a string except for '/'. /// /// ``` /// extern crate slash_formatter; /// /// let s = String::from("/path"); /// /// let s = slash_formatter::delete_start_slash_owned(s); /// /// assert_eq!("path", s); /// ``` pub fn delete_start_slash_owned(mut s: String) -> String { let length = s.len(); if length > 1 && s.starts_with('/') { s.remove(0); } return s; } /// Add an starting slash into a string. /// /// ``` /// extern crate slash_formatter; /// /// assert_eq!("/path", slash_formatter::add_start_slash("path")); /// ``` pub fn add_start_slash(s: &str) -> String { add_start_slash_owned(s.to_string()) } /// Add an starting slash into a string. /// /// ``` /// extern crate slash_formatter; /// /// let s = String::from("path"); /// /// let s = slash_formatter::add_start_slash_owned(s); /// /// assert_eq!("/path", s); /// ``` pub fn add_start_slash_owned(mut s: String) -> String { if !s.starts_with('/') { s.insert(0, '/'); } return s; } /// Add an ending slash into a string. /// /// ``` /// extern crate slash_formatter; /// /// assert_eq!("path/", slash_formatter::add_end_slash("path")); /// ``` pub fn add_end_slash(s: &str) -> String { add_end_slash_owned(s.to_string()) } /// Add an ending slash into a string. /// /// ``` /// extern crate slash_formatter; /// /// let s = String::from("path"); /// /// let s = slash_formatter::add_end_slash_owned(s); /// /// assert_eq!("path/", s); /// ``` pub fn add_end_slash_owned(mut s: String) -> String { if !s.ends_with('/') { s.push('/'); } return s; } /// Concatenate two strings with a slash. /// /// ``` /// extern crate slash_formatter; /// /// assert_eq!("path/to", slash_formatter::concat_with_slash("path", "to/")); /// ``` pub fn concat_with_slash(s1: &str, s2: &str) -> String { concat_with_slash_owned(s1.to_string(), s2) } /// Concatenate two strings with a slash. /// /// ``` /// extern crate slash_formatter; /// /// let s = String::from("path"); /// /// let s = slash_formatter::concat_with_slash_owned(s, "to/"); /// /// assert_eq!("path/to", s); /// ``` pub fn concat_with_slash_owned(s1: String, s2: &str) -> String { return delete_end_slash_owned(add_end_slash_owned(s1) + delete_start_slash(s2)); }