pub struct RubyString { /* private fields */ }
Expand description
A string type that can have ruby glosses attached to parts of it.
§Memory layout
The text content is stored in two String instances, one being the main text and one being a concatenation of all the rubies. Placement of the rubies is stored as a list of indices into both strings. Compared to the trivial structure (where each rubied substring is held as a separate String), this layout reduces memory usage and the number of separate allocations at the expense of slightly more complicated indexing logic.
Implementations§
Source§impl RubyString
impl RubyString
Sourcepub fn new() -> RubyString
pub fn new() -> RubyString
Creates a new empty RubyString
.
Sourcepub fn push_str(&mut self, string: &str)
pub fn push_str(&mut self, string: &str)
Appends plain text (that does not have a ruby gloss attached to it) to this RubyString
.
Sourcepub fn push_segment<'a>(&mut self, segment: Segment<'a>)
pub fn push_segment<'a>(&mut self, segment: Segment<'a>)
Appends text to this RubyString
and attaches a ruby gloss to it.
Sourcepub fn to_plain_text(&self) -> String
pub fn to_plain_text(&self) -> String
Returns the plain text stored in this RubyString
. The result has no ruby glosses attached
to it anymore.
let mut rs = RubyString::new();
rs.push_str("ここは");
rs.push_segment(Segment::Rubied { text: "東", ruby: "とう" });
rs.push_segment(Segment::Rubied { text: "京", ruby: "きょう" });
rs.push_str("です");
assert_eq!(rs.to_plain_text(), "ここは東京です");
Sourcepub fn to_interlinear_encoding(&self) -> String
pub fn to_interlinear_encoding(&self) -> String
Returns an encoding of this RubyString
as a plain String using interlinear annotation
characters.
let mut rs = RubyString::new();
rs.push_str("ここは");
rs.push_segment(Segment::Rubied { text: "東", ruby: "とう" });
rs.push_segment(Segment::Rubied { text: "京", ruby: "きょう" });
rs.push_str("です");
let encoded = rs.to_interlinear_encoding();
assert_eq!(encoded, "ここは\u{FFF9}東\u{FFFA}とう\u{FFFB}\u{FFF9}京\u{FFFA}きょう\u{FFFB}です");
Sourcepub fn segments(&self) -> SegmentIterator<'_> ⓘ
pub fn segments(&self) -> SegmentIterator<'_> ⓘ
An iterator over the segments in this RubyString
.
Trait Implementations§
Source§impl Clone for RubyString
impl Clone for RubyString
Source§fn clone(&self) -> RubyString
fn clone(&self) -> RubyString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl Default for RubyString
impl Default for RubyString
Source§impl<'a> Extend<Segment<'a>> for RubyString
impl<'a> Extend<Segment<'a>> for RubyString
Source§fn extend<I: IntoIterator<Item = Segment<'a>>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = Segment<'a>>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)