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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
/// Returns a value between matched delimiters from part of a string. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim` - The delimiter to match within. /// /// # Panics /// /// If the matched value cannot be parsed to the specified type. #[must_use] pub fn matched<T>(s: &str, delim: &str) -> Option<T> where T: std::str::FromStr, T::Err: std::fmt::Debug, { matched_s(&s, delim).map(|m| m.parse::<T>().unwrap()) } /// Returns a substring between enclosing matched delimiters. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim` - The enclosing delimiter. #[must_use] pub fn matched_s<'a>(s: &'a str, delim: &str) -> Option<&'a str> { mismatched_s(&s, delim, delim) } /// Returns a value between matched delimiters from part of a string. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim_start` - The opening delimiter. /// * `delim_end` - The closing delimiter. /// /// # Panics /// /// If the matched value cannot be parsed to the specified type. #[must_use] pub fn mismatched<T>(s: &str, delim_start: &str, delim_end: &str) -> Option<T> where T: std::str::FromStr, T::Err: std::fmt::Debug, { mismatched_s(&s, delim_start, delim_end).map(|m| m.parse::<T>().unwrap()) } /// Returns a substring between enclosing mismatched delimiters from part of a /// string. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim_start` - The opening delimiter. /// * `delim_end` - The closing delimiter. #[must_use] pub fn mismatched_s<'a>( s: &'a str, delim_start: &str, delim_end: &str, ) -> Option<&'a str> { s.find(delim_start).and_then(|mut s_ix| { s_ix += 1; // Consume matched leading delim s[s_ix..].find(delim_end).map(|e_ix| &s[s_ix..e_ix + s_ix]) }) } /// Returns a value prefixed by a delimiter. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim` - The delimiter prefix. /// * `len` - The length of the prefixed value, in bytes. /// /// # Panics /// /// If the matched value cannot be parsed to the specified type. #[must_use] pub fn prefixed<T>(s: &str, delim: &str, len: usize) -> Option<T> where T: std::str::FromStr, T::Err: std::fmt::Debug, { prefixed_s(&s, delim, len).map(|m| m.parse::<T>().unwrap()) } /// Returns a substring of bytes following a prefix delimiter. /// /// # Arguments /// /// * `self` - The delimited string to search. /// * `delim` - The prefix delimiter. /// * `len` - The length of the prefixed substring, in bytes. #[must_use] pub fn prefixed_s<'a>(s: &'a str, delim: &str, len: usize) -> Option<&'a str> { s.find(delim).map(|mut s_ix| { s_ix += 1; // Consume matched leading delim &s[s_ix..s_ix + len] }) } #[cfg(test)] mod tests { use super::*; // #region tests: matched /// Matched delimiters; happy path #[test] fn test_matched() { assert_eq!(matched("abc:12:def", ":"), Some(12)); } /// Matched delimiters; empty string #[test] fn test_matched_empty() { assert_eq!(matched::<usize>("", ":"), None); } /// Matched delimiters; start delimiter missing #[test] fn test_matched_missing_start() { assert_eq!(matched::<usize>("abc12:def", ":"), None); } /// Matched delimiters; end delimiter missing #[test] fn test_matched_missing_end() { assert_eq!(matched::<usize>("abc:12def", ":"), None); } /// Matched delimiters; both delimiters missing #[test] fn test_matched_missing_both() { assert_eq!(matched::<usize>("abcdef", ":"), None); } // #endregion tests: matched // #region tests: matched_s /// Matched delimiters (str); happy path #[test] fn test_matched_s() { assert_eq!(matched_s("abc:12:def", ":"), Some("12")); } /// Matched delimiters (str); empty string #[test] fn test_matched_s_empty() { assert_eq!(matched_s("", ":"), None); } /// Matched delimiters (str); start delimiter missing #[test] fn test_matched_s_missing_start() { assert_eq!(matched_s("abc12:def", ":"), None); } /// Matched delimiters (str); end delimiter missing #[test] fn test_matched_s_missing_end() { assert_eq!(matched_s("abc:12def", ":"), None); } /// Matched delimiters (str); both delimiters missing #[test] fn test_matched_s_missing_both() { assert_eq!(matched_s("abcdef", ":"), None); } // #endregion tests: matched_s // #region tests: mismatched /// Mismatched delimiters; happy path #[test] fn test_mismatched() { assert_eq!(mismatched("abc:12;def", ":", ";"), Some(12)); } /// Mismatched delimiters; empty string #[test] fn test_mismatched_empty() { assert_eq!(mismatched::<usize>("", ":", ";"), None); } /// Mismatched delimiters; start missing #[test] fn test_mismatched_missing_start() { assert_eq!(mismatched::<usize>("abc12;def", ":", ";"), None) } /// Mismatched delimiters; end missing #[test] fn test_mismatched_missing_end() { assert_eq!(mismatched::<usize>("abc:12def", ":", ";"), None) } /// Mismatched delimiters; both missing #[test] fn test_mismatched_missing_both() { assert_eq!(mismatched::<usize>("abc12def", ":", ";"), None) } // #endregion tests: mismatched // #region tests: mismatched_s /// Mismatched delimiters (str); happy path #[test] fn test_mismatched_s() { assert_eq!(mismatched_s("abc:12;def", ":", ";"), Some("12")); } /// Mismatched delimiters (str); empty string #[test] fn test_mismatched_s_empty() { assert_eq!(mismatched_s("", ":", ";"), None); } /// Mismatched delimiters (str); start missing #[test] fn test_mismatched_s_missing_start() { assert_eq!(mismatched_s("abc12;def", ":", ";"), None) } /// Mismatched delimiters (str); end missing #[test] fn test_mismatched_s_missing_end() { assert_eq!(mismatched_s("abc:12def", ":", ";"), None) } /// Mismatched delimiters (str); both missing #[test] fn test_mismatched_s_missing_both() { assert_eq!(mismatched_s("abc12def", ":", ";"), None) } // #endregion tests: mismatched_s // #region tests: prefixed /// Prefixed; happy path #[test] fn test_prefixed() { assert_eq!(prefixed("abc<12def", "<", 2), Some(12)); } /// Prefixed; empty string #[test] fn test_prefixed_empty() { assert_eq!(prefixed::<usize>("", "<", 2), None); } /// Prefixed; start delimiter missing #[test] fn test_prefixed_missing_start() { assert_eq!(prefixed::<usize>("abc12:def", "<", 2), None); } /// Prefixed; end delimiter missing #[test] fn test_prefixed_missing_end() { assert_eq!(prefixed::<usize>("abc:12def", "<", 2), None); } /// Prefixed; both delimiters missing #[test] fn test_prefixed_missing_both() { assert_eq!(prefixed::<usize>("abcdef", "<", 2), None); } // #endregion tests: prefixed // #region tests: prefixed_s /// Prefixed (str); happy path #[test] fn test_prefixed_s() { assert_eq!(prefixed_s("abc<12def", "<", 2), Some("12")); } /// Prefixed (str); empty string #[test] fn test_prefixed_s_empty() { assert_eq!(prefixed_s("", "<", 2), None); } /// Prefixed (str); start delimiter missing #[test] fn test_prefixed_s_missing_start() { assert_eq!(prefixed_s("abc12:def", "<", 2), None); } /// Prefixed (str); end delimiter missing #[test] fn test_prefixed_s_missing_end() { assert_eq!(prefixed_s("abc:12def", "<", 2), None); } /// Prefixed (str); both delimiters missing #[test] fn test_prefixed_s_missing_both() { assert_eq!(prefixed_s("abcdef", "<", 2), None); } // #endregion tests: prefixed_s }