(
builder: &mut B,
string: &str,
from: &str,
to: &str,
)
| 245 | |
| 246 | #[inline] |
| 247 | fn apply_replace<B: BulkNullStringArrayBuilder>( |
| 248 | builder: &mut B, |
| 249 | string: &str, |
| 250 | from: &str, |
| 251 | to: &str, |
| 252 | ) { |
| 253 | // Hot path: single ASCII byte → single ASCII byte. An ASCII byte (< 0x80) |
| 254 | // cannot appear inside a multi-byte UTF-8 sequence, so any multi-byte |
| 255 | // sequences in `string` pass through unchanged and output stays valid |
| 256 | // UTF-8. |
| 257 | if let (&[from_byte], &[to_byte]) = (from.as_bytes(), to.as_bytes()) |
| 258 | && from_byte.is_ascii() |
| 259 | && to_byte.is_ascii() |
| 260 | { |
| 261 | // SAFETY: see the contract above. |
| 262 | unsafe { |
| 263 | builder.append_byte_map(string.as_bytes(), |b| { |
| 264 | if b == from_byte { to_byte } else { b } |
| 265 | }); |
| 266 | } |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | if from.is_empty() { |
| 271 | // Empty `from`: insert `to` before each character and at both ends. |
| 272 | builder.append_with(|w| { |
| 273 | w.write_str(to); |
| 274 | for ch in string.chars() { |
| 275 | w.write_char(ch); |
| 276 | w.write_str(to); |
| 277 | } |
| 278 | }); |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | builder.append_with(|w| replace_into_writer(w, string, from, to)); |
| 283 | } |
| 284 | |
| 285 | #[inline] |
| 286 | fn replace_into_writer<W: StringWriter>(w: &mut W, string: &str, from: &str, to: &str) { |
no test coverage detected
searching dependent graphs…