Skip to main content

DoublePlaceholder

Enum DoublePlaceholder 

Source
pub enum DoublePlaceholder {}
Expand description

Backend for patterns containing zero, one, or two placeholders.

This empty type is not constructible.

§Placeholder Keys

The placeholder is either DoublePlaceholderKey::Place0 or DoublePlaceholderKey::Place1.

In Pattern::interpolate(), pass a two-element array or tuple.

§Encoding Details

The first two code points contain the indices of the first and second placeholders with the following encoding:

  1. First bit: 0 for DoublePlaceholderKey::Place0, 1 for DoublePlaceholderKey::Place1.
  2. Second and higher bits: 1 plus the byte offset of the placeholder counting from after the placeholder index code points. If zero, skip this placeholder.

§Examples

Parsing a pattern into the encoding:

use core::str::FromStr;
use icu_pattern::DoublePlaceholder;
use icu_pattern::Pattern;

// Parse the string syntax and check the resulting data store:
let pattern = Pattern::<DoublePlaceholder>::try_from_str(
    "Hello, {0} and {1}!",
    Default::default(),
)
.unwrap();

assert_eq!("\x10\x1BHello,  and !", &pattern.store);

Explanation of the lead code points:

  1. \x10 is placeholder 0 at index 7: ((7 + 1) << 1) | 0
  2. \x1B is placeholder 1 at index 12: ((12 + 1) << 1) | 1

Example patterns supported by this backend:

use core::str::FromStr;
use icu_pattern::DoublePlaceholder;
use icu_pattern::Pattern;
use icu_pattern::QuoteMode;

// Single numeric placeholder (note, "5" is used):
assert_eq!(
    Pattern::<DoublePlaceholder>::try_from_str(
        "{0} days ago",
        Default::default()
    )
    .unwrap()
    .interpolate_to_string([5, 7]),
    "5 days ago",
);

// No placeholder (note, the placeholder value is never accessed):
assert_eq!(
    Pattern::<DoublePlaceholder>::try_from_str(
        "yesterday",
        Default::default()
    )
    .unwrap()
    .interpolate_to_string(["foo", "bar"]),
    "yesterday",
);

// Escaped placeholder and a placeholder value 1 (note, "bar" is used):
assert_eq!(
    Pattern::<DoublePlaceholder>::try_from_str(
        "'{0}' {1}",
        QuoteMode::QuotingSupported.into()
    )
    .unwrap()
    .interpolate_to_string(("foo", "bar")),
    "{0} bar",
);

// Pattern with the placeholders in the opposite order:
assert_eq!(
    Pattern::<DoublePlaceholder>::try_from_str(
        "A {1} B {0} C",
        Default::default()
    )
    .unwrap()
    .interpolate_to_string(("D", "E")),
    "A E B D C",
);

// No literals, only placeholders:
assert_eq!(
    Pattern::<DoublePlaceholder>::try_from_str(
        "{1}{0}",
        Default::default()
    )
    .unwrap()
    .interpolate_to_string((1, "A")),
    "A1",
);

Trait Implementations§

Source§

impl Clone for DoublePlaceholder

Source§

fn clone(&self) -> DoublePlaceholder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DoublePlaceholder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for DoublePlaceholder

Source§

fn eq(&self, other: &DoublePlaceholder) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PatternBackend for DoublePlaceholder

Source§

type PlaceholderKey<'a> = DoublePlaceholderKey

The type to be used as the placeholder key in code.
Source§

type PlaceholderKeyCow<'a> = DoublePlaceholderKey

Cowable version of the type to be used as the placeholder key in code.
Source§

type Error<'a> = Infallible

The type of error that the TryWriteable for this backend can return.
Source§

type Store = str

Source§

impl Copy for DoublePlaceholder

Source§

impl Eq for DoublePlaceholder

Source§

impl StructuralPartialEq for DoublePlaceholder

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.