Function convert_to_spaces

Source
pub fn convert_to_spaces<S: AsRef<str>>(input: S, tab_size: usize) -> String
Expand description

Converts tabs in a string to a specified number of spaces, respecting tab stop logic.

Each tab character (\t) will advance the text to the next multiple of the tab_size. The number of spaces inserted will vary depending on the current column position.

§Arguments

  • input - The string-like reference (AsRef<str>) to convert. This allows passing &str, String, &String, etc.
  • tab_size - The size of a tab stop. If 0, tabs are effectively removed.

§Returns

A String with all tab characters (\t) correctly replaced by spaces according to tab stop rules.

§Examples

use convert_to_spaces::convert_to_spaces;

assert_eq!(convert_to_spaces("SPACE\tTAB", 4), "SPACE   TAB");
assert_eq!(convert_to_spaces("\tHello", 4), "    Hello");
assert_eq!(convert_to_spaces("Line 1\n\tLine 2", 2), "Line 1\n  Line 2");
assert_eq!(convert_to_spaces(String::from("MyString\t"), 8), "MyString        ");
assert_eq!(convert_to_spaces("No tabs here", 4), "No tabs here");