Enum icu::pattern::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 copy 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<(), Error>

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§

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.
source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T
where T: Send + Sync,