Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quotes in patterns #5532

Merged
merged 10 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions components/experimental/src/relativetime/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ where
type Error = icu_pattern::PatternError;

fn try_from(elements: PluralElements<&'data str>) -> Result<Self, Self::Error> {
let make_pattern = |s: &&str|
// TODO: Make pattern support apostrophes
Pattern::<B, String>::from_str(&s.replace('\'', "''")).map(|p| p.take_store());
let make_pattern = |s: &&str| Pattern::<B, String>::from_str(s).map(|p| p.take_store());

Ok(Self {
strings: PluralElements::new(make_pattern(elements.other())?.as_str())
Expand Down
8 changes: 0 additions & 8 deletions utils/pattern/src/double.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,6 @@ impl DoublePlaceholderInfo {
/// "yesterday",
/// );
///
/// // Escaped placeholder and a placeholder value 1 (note, "bar" is used):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is being added back in #5531, but there's no way to pass options through FromStr.

/// assert_eq!(
/// Pattern::<DoublePlaceholder, _>::from_str("'{0}' {1}")
/// .unwrap()
/// .interpolate_to_string(("foo", "bar")),
/// "{0} bar",
/// );
///
/// // Pattern with the placeholders in the opposite order:
/// assert_eq!(
/// Pattern::<DoublePlaceholder, _>::from_str("A {1} B {0} C")
Expand Down
11 changes: 3 additions & 8 deletions utils/pattern/src/frontend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ mod databake;
mod serde;
use crate::common::*;
use crate::Error;
use crate::PatternOrUtf8Error;
#[cfg(feature = "alloc")]
use crate::{Parser, ParserOptions};
use crate::Parser;
use crate::PatternOrUtf8Error;
#[cfg(feature = "alloc")]
use alloc::{borrow::ToOwned, str::FromStr, string::String};
use core::{
Expand Down Expand Up @@ -277,12 +277,7 @@ where
/// .expect_err("mismatched braces");
/// ```
fn from_str(pattern: &str) -> Result<Self, Self::Err> {
let parser = Parser::new(
pattern,
ParserOptions {
allow_raw_letters: true,
},
);
let parser = Parser::new(pattern, Default::default());
let store = B::try_from_items(parser)?;
#[cfg(debug_assertions)]
match B::validate_store(core::borrow::Borrow::borrow(&store)) {
Expand Down
2 changes: 2 additions & 0 deletions utils/pattern/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub use parser::Parser;
pub use parser::ParserError;
#[cfg(feature = "alloc")]
pub use parser::ParserOptions;
#[cfg(feature = "alloc")]
pub use parser::QuoteMode;
pub use single::SinglePlaceholder;
pub use single::SinglePlaceholderKey;
#[doc(no_inline)]
Expand Down
70 changes: 36 additions & 34 deletions utils/pattern/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,33 @@ macro_rules! handle_literal {
/// Options passed to the constructor of [`Parser`].
///
/// ✨ *Enabled with the `alloc` Cargo feature.*
#[derive(Debug)]
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct ParserOptions {
/// Controls whether ASCII letters can appear in the raw
/// pattern.
///
/// If set to `true`, ASCII letters can be used directly in the pattern,
/// like "{0} days".
/// Controls how quotes (`'`) are interpreted.
pub quote_mode: QuoteMode,
}

/// Controls how quotes (`'`) are interpreted.
#[derive(Debug, Default, PartialEq)]
#[non_exhaustive]
pub enum QuoteMode {
/// Quotes are interpreted as literals, i.e. `{0} o'clock` will interpolate to `5 o'clock`.
#[default]
QuotesAreLiterals,
/// Quotes can be used to quote ASCII characters, i.e. both `{0} World` and `{0} 'World'` will interpolate to `Hello World`.
///
/// If set to `false`, ASCII letters can only appear in quoted literals,
robertbastian marked this conversation as resolved.
Show resolved Hide resolved
/// like "{0} 'days'".
/// A double quote can be used to create a quote literal, i.e. `{0} o''clock`.
QuotingSupported,
/// Quotes are required to quote ASCII characters, i.e. `{0} 'World'` will interpolate to `Hello World`, while `{0} World` is an error.
///
/// Default is `true`.
pub allow_raw_letters: bool,
/// A double quote can be used to create a quote literal, i.e. `{0} 'o''clock'`.
QuotingRequired,
}

impl Default for ParserOptions {
fn default() -> Self {
Self {
allow_raw_letters: true,
}
impl From<QuoteMode> for ParserOptions {
fn from(quote_mode: QuoteMode) -> Self {
Self { quote_mode }
}
}

Expand Down Expand Up @@ -180,11 +186,11 @@ impl Default for ParserOptions {
///
/// ### Examples
/// ```
/// use icu_pattern::{ParsedPatternItem, Parser, ParserOptions};
/// use icu_pattern::{ParsedPatternItem, Parser, QuoteMode};
///
/// let input = "{0} 'and' {1}";
///
/// let mut parser = Parser::new(input, ParserOptions::default());
/// let mut parser = Parser::new(input, QuoteMode::QuotingSupported.into());
///
/// let mut result = vec![];
///
Expand Down Expand Up @@ -255,7 +261,7 @@ pub struct Parser<'p, P> {
input: &'p str,
len: usize,

allow_raw_letters: bool,
quote_mode: QuoteMode,

start_idx: usize,
idx: usize,
Expand All @@ -280,7 +286,7 @@ impl<'p, P> Parser<'p, P> {
input,
len: input.len(),

allow_raw_letters: options.allow_raw_letters,
quote_mode: options.quote_mode,

start_idx: 0,
idx: 0,
Expand Down Expand Up @@ -337,7 +343,9 @@ impl<'p, P> Parser<'p, P> {
.map(|ret| Some(ParsedPatternItem::Placeholder(ret)))
.map_err(ParserError::InvalidPlaceholder);
}
ParserState::QuotedLiteral if *b == b'\'' => {
ParserState::QuotedLiteral
if *b == b'\'' && self.quote_mode != QuoteMode::QuotesAreLiterals =>
{
if self.input.as_bytes().get(self.idx + 1) == Some(&b'\'') {
handle_literal!(self, true, ParserState::Apostrophe { quoted: true })
} else {
Expand All @@ -347,14 +355,18 @@ impl<'p, P> Parser<'p, P> {
ParserState::Default if *b == b'{' => {
handle_literal!(self, false, ParserState::Placeholder)
}
ParserState::Default if *b == b'\'' => {
ParserState::Default
if *b == b'\'' && self.quote_mode != QuoteMode::QuotesAreLiterals =>
{
if self.input.as_bytes().get(self.idx + 1) == Some(&b'\'') {
handle_literal!(self, false, ParserState::Apostrophe { quoted: false })
} else {
handle_literal!(self, false, ParserState::QuotedLiteral)
}
}
ParserState::Default if !self.allow_raw_letters && b.is_ascii_alphabetic() => {
ParserState::Default
if self.quote_mode == QuoteMode::QuotingRequired && b.is_ascii_alphabetic() =>
{
return Err(ParserError::IllegalCharacter(*b as char));
}
ParserState::Apostrophe { quoted } => {
Expand Down Expand Up @@ -552,12 +564,7 @@ mod tests {
];

for (input, expected) in samples {
let parser = Parser::new(
input,
ParserOptions {
allow_raw_letters: true,
},
);
let parser = Parser::new(input, QuoteMode::QuotingSupported.into());
let result = parser
.try_collect_into_vec()
.expect("Failed to parse a pattern");
Expand Down Expand Up @@ -589,12 +596,7 @@ mod tests {
];

for (input, error) in broken {
let parser = Parser::<usize>::new(
input,
ParserOptions {
allow_raw_letters: false,
},
);
let parser = Parser::<usize>::new(input, QuoteMode::QuotingRequired.into());
let result = parser.try_collect_into_vec();
if let Some(error) = error {
assert_eq!(result.expect_err("Should have failed."), error,);
Expand Down
8 changes: 0 additions & 8 deletions utils/pattern/src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,6 @@ where
/// .interpolate_to_string(["hi"]),
/// "yesterday",
/// );
///
/// // Escaped placeholder and a real placeholder:
/// assert_eq!(
/// Pattern::<SinglePlaceholder, _>::from_str("'{0}' {1}")
/// .unwrap()
/// .interpolate_to_string(("hi",)),
/// "{0} hi",
/// );
/// ```
///
/// [`Pattern::interpolate()`]: crate::Pattern::interpolate
Expand Down