MCPcopy Index your code
hub / github.com/aiscriptdev/aiscript / format

Method format

aiscript-vm/src/builtins/format.rs:107–184  ·  view source on GitHub ↗
(&self, value: &Value)

Source from the content-addressed store, hash-verified

105 }
106
107 fn format(&self, value: &Value) -> Result<String, VmError> {
108 let mut formatted = match value {
109 Value::Number(n) => match self.format_type {
110 Some('d') => format!("{:.0}", n),
111 Some('x') => {
112 let int_val = *n as i64;
113 format!("{:x}", int_val)
114 }
115 Some('X') => {
116 let int_val = *n as i64;
117 format!("{:X}", int_val)
118 }
119 Some('o') => {
120 let int_val = *n as i64;
121 format!("{:o}", int_val)
122 }
123 Some('b') => {
124 let int_val = *n as i64;
125 format!("{:b}", int_val)
126 }
127 Some('f') => {
128 if let Some(precision) = self.precision {
129 format!("{:.*}", precision, n)
130 } else {
131 format!("{}", n)
132 }
133 }
134 _ => {
135 if let Some(precision) = self.precision {
136 format!("{:.*}", precision, n)
137 } else {
138 format!("{}", n)
139 }
140 }
141 },
142 Value::String(s) => {
143 let s = s.to_str().unwrap();
144 if let Some(precision) = self.precision {
145 s[..s.chars().take(precision).map(|c| c.len_utf8()).sum()].to_string()
146 } else {
147 s.to_string()
148 }
149 }
150 _ => format!("{}", value),
151 };
152
153 // Apply width and alignment
154 if let Some(width) = self.width {
155 if formatted.chars().count() < width {
156 let padding = width - formatted.chars().count();
157 let fill_char = self.fill.unwrap_or(' ');
158
159 match self.align.unwrap_or('>') {
160 '<' => {
161 formatted.extend(std::iter::repeat(fill_char).take(padding));
162 }
163 '>' => {
164 let mut new_string = String::new();

Callers 2

time_format_datetimeFunction · 0.80
format_stringFunction · 0.80

Calls 3

repeatFunction · 0.85
to_strMethod · 0.80
countMethod · 0.80

Tested by

no test coverage detected