Function tr

Source
pub fn tr(s: *mut i8, from: i8, to: i8)
Expand description

In-place translation of a C-style NUL-terminated string, replacing all occurrences of one byte with another.

This function mutates the given pointer to a NUL-terminated string (*mut i8), replacing every occurrence of the byte from with the byte to. The operation stops at the first NUL byte (\0).

§Safety

  • The pointer s must be valid and point to a mutable, NUL-terminated string.
  • The memory must be writable and large enough to contain the string and the NUL terminator.
  • Behavior is undefined if s is not properly NUL-terminated or is invalid.

§Arguments

  • s - Pointer to the mutable NUL-terminated string.
  • from - The byte value to replace.
  • to - The byte value to substitute.

§Examples

use text_fx::transform::tr;

let mut buf = *b"hello\0";
tr(buf.as_mut_ptr() as *mut _, b'l' as i8, b'p' as i8);
assert_eq!(buf, *b"heppo\0");