Skip to content

Commit 9d98be8

Browse files
committed
Make performance of String::insert_str more precise
1 parent 87b4541 commit 9d98be8

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

library/alloc/src/string.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,10 +1489,11 @@ impl String {
14891489
Some(ch)
14901490
}
14911491

1492-
/// Removes a [`char`] from this `String` at a byte position and returns it.
1492+
/// Removes a [`char`] from this `String` at byte position `idx` and returns it.
14931493
///
1494-
/// This is an *O*(*n*) operation, as it requires copying every element in the
1495-
/// buffer.
1494+
/// Copies all bytes after the removed char to new positions.
1495+
///
1496+
/// Note that calling this in a loop can result in quadratic behavior.
14961497
///
14971498
/// # Panics
14981499
///
@@ -1678,10 +1679,13 @@ impl String {
16781679
drop(guard);
16791680
}
16801681

1681-
/// Inserts a character into this `String` at a byte position.
1682+
/// Inserts a character into this `String` at byte position `idx`.
1683+
///
1684+
/// Reallocates if `self.capacity()` is insufficient,
1685+
/// which may involve copying all `self.capacity()` bytes.
1686+
/// Makes space for the insertion by copying all bytes of `&self[idx..]` to new positions.
16821687
///
1683-
/// This is an *O*(*n*) operation as it requires copying every element in the
1684-
/// buffer.
1688+
/// Note that calling this in a loop can result in quadratic behavior.
16851689
///
16861690
/// # Panics
16871691
///
@@ -1733,10 +1737,13 @@ impl String {
17331737
}
17341738
}
17351739

1736-
/// Inserts a string slice into this `String` at a byte position.
1740+
/// Inserts a string slice into this `String` at byte position `idx`.
1741+
///
1742+
/// Reallocates if `self.capacity()` is insufficient,
1743+
/// which may involve copying all `self.capacity()` bytes.
1744+
/// Makes space for the insertion by copying all bytes of `&self[idx..]` to new positions.
17371745
///
1738-
/// This is an *O*(*n*) operation as it requires copying every element in the
1739-
/// buffer.
1746+
/// Note that calling this in a loop can result in quadratic behavior.
17401747
///
17411748
/// # Panics
17421749
///

0 commit comments

Comments
 (0)