@@ -287,6 +287,52 @@ test "index_of" {
287
287
inspect! ("abc" .index_of ("b" , from = 100 ), content = "-1" )
288
288
}
289
289
290
+ ///|
291
+ test "String::index_of empty strings" {
292
+ // Empty string cases
293
+ assert_eq! ("" .index_of ("" ), 0 )
294
+ assert_eq! ("abc" .index_of ("" ), 0 )
295
+ assert_eq! ("" .index_of ("a" ), - 1 )
296
+ }
297
+
298
+ ///|
299
+ test "String::index_of basic matching" {
300
+ // Basic substring matching
301
+ assert_eq! ("abc" .index_of ("a" ), 0 )
302
+ assert_eq! ("abc" .index_of ("b" ), 1 )
303
+ assert_eq! ("abc" .index_of ("c" ), 2 )
304
+ assert_eq! ("abc" .index_of ("ab" ), 0 )
305
+ assert_eq! ("abc" .index_of ("bc" ), 1 )
306
+ assert_eq! ("abc" .index_of ("abc" ), 0 )
307
+ assert_eq! ("abc" .index_of ("d" ), - 1 )
308
+ assert_eq! ("abc" .index_of ("abcd" ), - 1 )
309
+ }
310
+
311
+ ///|
312
+ test "String::index_of with from parameter" {
313
+ // Testing from parameter
314
+ assert_eq! ("abcabc" .index_of ("a" , from = 1 ), 3 )
315
+ assert_eq! ("abcabc" .index_of ("a" , from = 4 ), - 1 )
316
+ assert_eq! ("abcabc" .index_of ("bc" , from = 2 ), 4 )
317
+ assert_eq! ("abc" .index_of ("" , from = 1 ), 1 )
318
+ assert_eq! ("abc" .index_of ("" , from = 3 ), 3 )
319
+ assert_eq! ("abc" .index_of ("" , from = 4 ), 3 ) // Bounded to length
320
+ }
321
+
322
+ ///|
323
+ test "String::index_of with negative from" {
324
+ // Testing negative from parameter
325
+ assert_eq! ("abc" .index_of ("a" , from = - 1 ), 0 )
326
+ assert_eq! ("abc" .index_of ("b" , from = - 2 ), 1 )
327
+ }
328
+
329
+ ///|
330
+ test "String::index_of overlapping patterns" {
331
+ // Testing overlapping patterns
332
+ assert_eq! ("aaa" .index_of ("aa" ), 0 )
333
+ assert_eq! ("aaa" .index_of ("aa" , from = 1 ), 1 )
334
+ }
335
+
290
336
///|
291
337
test "last_index_of" {
292
338
inspect! ("abc" .last_index_of ("a" ), content = "0" )
@@ -589,8 +635,8 @@ test "string pattern matching" {
589
635
} derive (Show )
590
636
fn process_string (s : String ) {
591
637
match s {
592
- [.."true" , ..] => True
593
- [.."false" , ..] => False
638
+ [.. "true" , ..] => True
639
+ [.. "false" , ..] => False
594
640
_ => Other
595
641
}
596
642
}
0 commit comments