From 2c7c85c2803a1845270afea2c7b6da7ac730fb81 Mon Sep 17 00:00:00 2001 From: Ian Hobson Date: Fri, 5 Apr 2024 08:11:45 +0200 Subject: [PATCH] Fix support for single-char-but-multi-byte fill characters --- crates/parser/src/string_format_options.rs | 12 +++++++++++- docs/language_guide.md | 7 ++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/parser/src/string_format_options.rs b/crates/parser/src/string_format_options.rs index 9b1fd2ef0..a2947d18a 100644 --- a/crates/parser/src/string_format_options.rs +++ b/crates/parser/src/string_format_options.rs @@ -46,7 +46,8 @@ impl StringFormatOptions { match (next, chars.peek(), position) { // Check for single-char fill character at the start of the string (_, Some('<' | '^' | '>'), Start) => { - result.fill_character = Some(add_string_constant(&format_string[0..1])?); + result.fill_character = + Some(add_string_constant(&format_string[0..next.len_utf8()])?); result.alignment = char_to_alignment(chars.next().unwrap()); position = MinWidth; } @@ -205,6 +206,15 @@ mod tests { ..Default::default() }, ), + ( + "πœ‡<.9", + StringFormatOptions { + alignment: StringAlignment::Left, + fill_character: Some(0), + precision: Some(9), + ..Default::default() + }, + ), ( "🫢🏽>20.10", StringFormatOptions { diff --git a/docs/language_guide.md b/docs/language_guide.md index a8e77aee5..b0aabedac 100644 --- a/docs/language_guide.md +++ b/docs/language_guide.md @@ -1336,7 +1336,12 @@ check! ('l', 'l', 'ΓΈ') Interpolated string expressions can be formatted using formatting options similar to [Rust's][rust-format-options]. -Options can be provided after a `:` separator inside the `{}` expression. +Inside an interpolated expression, options are provided after a `:` separator. + +```koto +print! '{number.pi:πœ‹^8.2}' +check! πœ‹πœ‹3.14πœ‹πœ‹ +``` ### Minimum Width and Alignment