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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
// Copyright (c) 2020 DarkWeb Design // // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and // associated documentation files (the "Software"), to deal in the Software without restriction, // including without limitation the rights to use, copy, modify, merge, publish, distribute, // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // https://www.php.net/manual/en/ref.strings.php /// Split a string by a string. /// /// # Description /// /// Returns an vec of strings, each of which is a substring of string formed by splitting it on /// boundaries formed by the string delimiter. /// /// # Parameters /// /// **limit** /// /// If limit is set and positive, the returned vec will contain a maximum of limit elements with the /// last element containing the rest of string. /// /// If the limit parameter is negative, all components except the last -limit are returned. /// /// If the limit parameter is zero, then this is treated as 1. /// /// # Examples /// /// Example #1 explode() examples /// /// ``` /// use phpify::string::explode; /// /// let pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; /// let pieces = explode(" ", pizza, std::isize::MAX).unwrap(); /// /// assert_eq!(pieces, ["piece1", "piece2", "piece3", "piece4", "piece5", "piece6"]); /// ``` /// /// ``` /// use phpify::string::explode; /// /// let data = "foo:*:1023:1000::/home/foo:/bin/sh"; /// let elements = explode(":", data, std::isize::MAX).unwrap(); /// /// assert_eq!(elements[0], "foo".to_string()); /// assert_eq!(elements[1], "*".to_string()); /// ``` /// /// Example #2 explode() return examples /// /// ``` /// use phpify::string::explode; /// /// let input1 = "hello"; /// let input2 = "hello,there"; /// let input3 = ","; /// /// assert_eq!(explode(",", input1, std::isize::MAX).unwrap(), ["hello"]); /// assert_eq!(explode(",", input2, std::isize::MAX).unwrap(), ["hello", "there"]); /// assert_eq!(explode(",", input3, std::isize::MAX).unwrap(), ["", ""]); /// ``` /// /// Example #3 limit parameter examples /// /// ``` /// use phpify::string::explode; /// /// let str = "one|two|three|four"; /// /// assert_eq!(explode("|", str, 2).unwrap(), ["one", "two|three|four"]); /// assert_eq!(explode("|", str, -1).unwrap(), ["one", "two", "three"]); /// ``` pub fn explode<D, S>(delimiter: D, string: S, limit: isize) -> Option<Vec<String>> where D: AsRef<str>, S: AsRef<str> { let delimiter = delimiter.as_ref(); let string = string.as_ref(); if delimiter.is_empty() { return None; } if limit == 0 || limit == 1 { return Some(vec![string.to_string()]); } let vec: Vec<String> = string.split(delimiter).map(String::from).collect(); let vec_length = vec.len() as isize; if limit > vec_length { return Some(vec); } if limit > 0 { let (left, right) = vec.split_at(limit as usize - 1); let mut vec = left.to_vec(); vec.push(right.join(delimiter)); return Some(vec); } if limit < 0 { if limit <= (0 - vec_length) { return Some(vec![]); } let (left, _right) = vec.split_at((vec_length + limit) as usize); let vec = left.to_vec(); return Some(vec); } Some(vec) } /// Join vec elements with a string. /// /// # Description /// /// Join vec elements with a glue string. /// /// # Examples /// /// Example #1 implode() example /// /// ``` /// use phpify::string::implode; /// /// let vec = vec!["lastname".to_string(), "email".to_string(), "phone".to_string()]; /// let comma_separated = implode(",", &vec); /// /// assert_eq!(comma_separated, "lastname,email,phone"); /// ``` pub fn implode<G>(glue: G, pieces: &Vec<String>) -> String where G: AsRef<str> { pieces.join(glue.as_ref()) } /// Get string length. /// /// # Description /// /// Returns the length of the given string. /// /// # Examples /// /// Example #1 strlen() example /// /// ``` /// use phpify::string::strlen; /// /// let str = "abcdef"; /// assert_eq!(strlen(str), 6); /// /// let str = " ab cd "; /// assert_eq!(strlen(str), 7); /// ``` /// /// # notes /// /// strlen() returns the number of bytes rather than the number of characters in a string. pub fn strlen<S>(string: S) -> usize where S: AsRef<str> { string.as_ref().len() } /// Find the position of the first occurrence of a substring in a string. /// /// # Description /// /// Find the numeric position of the first occurrence of needle in the haystack string. /// /// # Parameters /// /// **offset** /// /// If the offset is negative, the search will start this number of characters counted from the end /// of the string. /// /// # Examples /// /// Example #1 strpos() example /// /// ``` /// use phpify::string::strpos; /// /// let mystring = "abc"; /// let findme = "a"; /// let pos = strpos(mystring, findme, 0).unwrap(); /// /// assert_eq!(pos, 0); /// ``` /// /// Example #2 using an offset /// /// ``` /// use phpify::string::strpos; /// /// let newstring = "abcdef abcdef"; /// let pos = strpos(newstring, "a", 1).unwrap(); /// /// assert_eq!(pos, 7); /// ``` /// pub fn strpos<H, N>(haystack: H, needle: N, offset: isize) -> Option<usize> where H: AsRef<str>, N: AsRef<str> { let mut haystack = haystack.as_ref().to_string(); let needle = needle.as_ref(); let mut offset = offset; let haystack_length = haystack.len() as isize; if offset > 0 && offset > haystack_length { return None; } if offset < 0 { if offset < (0 - haystack_length) { return None; } offset = haystack_length + offset; } if offset == 0 { return haystack.find(needle); } haystack = haystack.chars().skip(offset as usize).collect(); match haystack.find(needle) { None => None, Some(position) => Some(position + offset as usize), } } /// Find the position of the first occurrence of a case-insensitive substring in a string. /// /// # Description /// /// Find the numeric position of the first occurrence of needle in the haystack string. /// /// Unlike the strpos(), stripos() is case-insensitive. /// /// # Parameters /// /// **offset** /// /// If the offset is negative, the search will start this number of characters counted from the end /// of the string. /// /// # Examples /// /// Example #1 stripos() example /// /// ``` /// use phpify::string::stripos; /// /// let mystring = "ABC"; /// let findme = "a"; /// let pos = stripos(mystring, findme, 0).unwrap(); /// /// assert_eq!(pos, 0); /// ``` pub fn stripos<H, N>(haystack: H, needle: N, offset: isize) -> Option<usize> where H: AsRef<str>, N: AsRef<str> { strpos(haystack.as_ref().to_lowercase(), needle.as_ref().to_lowercase(), offset) } /// Return part of a string. /// /// # Description /// /// Returns the portion of string specified by the start and length parameters. /// /// # Parameters /// /// **start** /// /// If start is non-negative, the returned string will start at the start'th position in string, /// counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', /// the character at position 2 is 'c', and so forth. /// /// If start is negative, the returned string will start at the start'th character from the end of /// string. /// /// If string is less than start characters long, *None* will be returned. /// /// **length** /// /// If length is given and is positive, the string returned will contain at most length characters /// beginning from start (depending on the length of string). /// /// If length is given and is negative, then that many characters will be omitted from the end of /// string (after the start position has been calculated when a start is negative). If start denotes /// the position of this truncation or beyond, *NONE* will be returned. /// /// If length is given and is 0, an empty string will be returned. /// /// # Examples /// /// Example #1 substr() examples /// /// ``` /// use phpify::string::substr; /// /// assert_eq!(substr("abcdef", 1, std::isize::MAX).unwrap(), "bcdef"); /// assert_eq!(substr("abcdef", 1, 3).unwrap(), "bcd"); /// assert_eq!(substr("abcdef", 0, 4).unwrap(), "abcd"); /// assert_eq!(substr("abcdef", 0, 8).unwrap(), "abcdef"); /// assert_eq!(substr("abcdef", -1, 1).unwrap(), "f"); /// ``` /// /// Example #2 using a negative start /// /// ``` /// use phpify::string::substr; /// /// assert_eq!(substr("abcdef", -1, std::isize::MAX).unwrap(), "f"); /// assert_eq!(substr("abcdef", -2, std::isize::MAX).unwrap(), "ef"); /// assert_eq!(substr("abcdef", -3, 1).unwrap(), "d"); /// ``` /// /// Example #3 using a negative length /// /// ``` /// use phpify::string::substr; /// /// assert_eq!(substr("abcdef", 0, -1).unwrap(), "abcde"); /// assert_eq!(substr("abcdef", 2, -1).unwrap(), "cde"); /// assert_eq!(substr("abcdef", 4, -4), None); /// assert_eq!(substr("abcdef", -3, -1).unwrap(), "de"); /// ``` pub fn substr<S>(string: S, start: isize, length: isize) -> Option<String> where S: AsRef<str> { let string = string.as_ref(); let mut start = start; let mut length = length; let string_length = string.len() as isize; if start > 0 && start >= string_length { return None; } if length == 0 { return None; } if start < 0 { start = string_length + start; if start < 0 { start = 0; } } if length < 0 { if start > string_length + length { return None; } length = string_length + length - start; } if start == 0 && length == string_length { return Some(string.to_string()); } Some(string.chars().skip(start as usize).take(length as usize).collect::<String>()) } #[cfg(test)] mod tests { use crate::string::*; #[test] fn explode_test() { assert_eq!(explode("|", "one|two|three", 3), Some(vec!["one".to_string(), "two".to_string(), "three".to_string()])); assert_eq!(explode("|", "one|two|three", 1), Some(vec!["one|two|three".to_string()])); assert_eq!(explode("|", "one|two|three", 2), Some(vec!["one".to_string(), "two|three".to_string()])); assert_eq!(explode("|", "one|two|three", -1), Some(vec!["one".to_string(), "two".to_string()])); assert_eq!(explode("|", "one|two|three", -3), Some(vec![])); assert_eq!(explode(",", "one|two|three", 1), Some(vec!["one|two|three".to_string()])); assert_eq!(explode("", "one|two|three", 3), None); } #[test] fn implode_test() { assert_eq!(implode("|", &vec!["one".to_string(), "two".to_string(), "three".to_string()]), "one|two|three".to_string()); assert_eq!(implode("", &vec!["one".to_string(), "two".to_string(), "three".to_string()]), "onetwothree".to_string()); assert_eq!(implode("", &vec![]), "".to_string()); } #[test] fn strlen_test() { assert_eq!(strlen("Hello World"), 11); assert_eq!(strlen(""), 0); } #[test] fn strpos_test() { let haystack = "Hello World"; let needle = "World"; assert_eq!(strpos(haystack, needle, 0), Some(6)); assert_eq!(strpos(haystack, needle, 6), Some(6)); assert_eq!(strpos(haystack, needle, -5), Some(6)); assert_eq!(strpos(haystack, needle, -11), Some(6)); assert_eq!(strpos(haystack, needle, 7), None); assert_eq!(strpos(haystack, needle, 11), None); assert_eq!(strpos(haystack, needle, -12), None); } #[test] fn stripos_test() { assert_eq!(stripos("HELLO WORLD", "world", 0), Some(6)); } #[test] fn substr_test() { let string = &"Hello World".to_string(); assert_eq!(substr(string, 0, 5), Some("Hello".to_string())); assert_eq!(substr(string, 6, 5), Some("World".to_string())); assert_eq!(substr(string, 0, 20), Some("Hello World".to_string())); assert_eq!(substr(string, 0, -6), Some("Hello".to_string())); assert_eq!(substr(string, 3, -3), Some("lo Wo".to_string())); assert_eq!(substr(string, -11, 11), Some("Hello World".to_string())); assert_eq!(substr(string, -20, 11), Some("Hello World".to_string())); assert_eq!(substr(string, 0, 0), None); assert_eq!(substr(string, 11, 1), None); assert_eq!(substr(string, 7, -5), None); } // #[test] fn bla() { let pizza = "piece1 piece2 piece3 piece4 piece5 piece6".to_string(); let pieces = explode(&" ".to_string(), &pizza, std::isize::MAX).unwrap(); println!("### {:?}", pieces); // ["piece1", "piece2", "piece3", "piece4", "piece5", "piece6"] println!("### {:?}", pizza); assert_eq!(1, 0); } }