MCPcopy Create free account
hub / github.com/apache/datafusion / format_signed

Method format_signed

datafusion/spark/src/function/string/format_string.rs:1894–1936  ·  view source on GitHub ↗
(&self, writer: &mut String, value: i64)

Source from the content-addressed store, hash-verified

1892 }
1893
1894 fn format_signed(&self, writer: &mut String, value: i64) -> Result<()> {
1895 let negative = value < 0;
1896 let abs_val = value.abs();
1897
1898 let (sign_prefix, sign_suffix) = if negative && self.negative_in_parentheses {
1899 ("(".to_owned(), ")".to_owned())
1900 } else if negative {
1901 ("-".to_owned(), "".to_owned())
1902 } else if self.force_sign {
1903 ("+".to_owned(), "".to_owned())
1904 } else if self.space_sign {
1905 (" ".to_owned(), "".to_owned())
1906 } else {
1907 ("".to_owned(), "".to_owned())
1908 };
1909
1910 let mut mod_spec = *self;
1911 mod_spec.width = match self.width {
1912 NumericParam::Literal(w) => NumericParam::Literal(
1913 w - sign_prefix.len() as i32 - sign_suffix.len() as i32,
1914 ),
1915 _ => NumericParam::FromArgument,
1916 };
1917 let mut formatted = String::new();
1918 mod_spec.format_unsigned(&mut formatted, abs_val as u64)?;
1919 // put the sign a after any leading spaces
1920 let mut actual_number = &formatted[0..];
1921 let mut leading_spaces = &formatted[0..0];
1922 if let Some(first_non_space) = formatted.find(|c| c != ' ') {
1923 actual_number = &formatted[first_non_space..];
1924 leading_spaces = &formatted[0..first_non_space];
1925 }
1926 write!(
1927 writer,
1928 "{}{}{}{}",
1929 leading_spaces.to_owned(),
1930 sign_prefix,
1931 actual_number,
1932 sign_suffix
1933 )
1934 .map_err(|e| exec_datafusion_err!("Write error: {}", e))?;
1935 Ok(())
1936 }
1937
1938 fn format_unsigned(&self, writer: &mut String, value: u64) -> Result<()> {
1939 let mut s = String::new();

Callers 1

formatMethod · 0.80

Calls 4

newFunction · 0.85
format_unsignedMethod · 0.80
LiteralInterface · 0.50
lenMethod · 0.45

Tested by

no test coverage detected