Skip to content

Commit afa7a71

Browse files
authored
Merge pull request #31 from bugadani/try-opt
Optimizations
2 parents 4d38cf3 + b4675ab commit afa7a71

7 files changed

Lines changed: 89 additions & 92 deletions

File tree

src/alignment/center.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Horizontal and vertical center aligned text.
22
use crate::{
33
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
4-
parser::Token,
4+
parser::{Parser, Token},
55
rendering::{
66
cursor::Cursor,
77
line::{StyledLineIterator, UniformSpaceConfig},
@@ -30,13 +30,13 @@ where
3030
F: Font + Copy,
3131
{
3232
/// Starts processing a line.
33-
NextLine(Option<Token<'a>>, Cursor<F>),
33+
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),
3434

3535
/// Renders the processed line.
3636
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, CenterAligned>),
3737
}
3838

39-
impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, CenterAligned, V>
39+
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, CenterAligned, V>
4040
where
4141
C: PixelColor,
4242
F: Font + Copy,
@@ -46,8 +46,8 @@ where
4646

4747
#[inline]
4848
#[must_use]
49-
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
50-
State::NextLine(None, cursor)
49+
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
50+
State::NextLine(None, cursor, parser)
5151
}
5252
}
5353

@@ -63,26 +63,25 @@ where
6363
fn next(&mut self) -> Option<Self::Item> {
6464
loop {
6565
match self.state {
66-
State::NextLine(ref carried_token, ref mut cursor) => {
66+
State::NextLine(ref carried_token, mut cursor, ref mut parser) => {
6767
if !cursor.in_display_area() {
6868
break None;
6969
}
7070

71-
if carried_token.is_none() && self.parser.is_empty() {
71+
if carried_token.is_none() && parser.is_empty() {
7272
break None;
7373
}
7474

75+
let parser_clone = parser.clone();
7576
let max_line_width = cursor.line_width();
76-
let (width, _, _) = self.style.measure_line(
77-
&mut self.parser.clone(),
78-
carried_token.clone(),
79-
max_line_width,
80-
);
77+
let (width, _, _) =
78+
self.style
79+
.measure_line(parser, carried_token.clone(), max_line_width);
8180
cursor.advance_unchecked((max_line_width - width + 1) / 2);
8281

8382
self.state = State::DrawLine(StyledLineIterator::new(
84-
self.parser.clone(),
85-
*cursor,
83+
parser_clone,
84+
cursor,
8685
UniformSpaceConfig {
8786
space_width: F::total_char_width(' '),
8887
},
@@ -96,10 +95,13 @@ where
9695
break pixel;
9796
}
9897

99-
self.parser = line_iterator.parser.clone();
10098
let carried_token = line_iterator.remaining_token();
10199

102-
self.state = State::NextLine(carried_token, line_iterator.cursor);
100+
self.state = State::NextLine(
101+
carried_token,
102+
line_iterator.cursor,
103+
line_iterator.parser.clone(),
104+
);
103105
}
104106
}
105107
}

src/alignment/justified.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Fully justified text.
22
use crate::{
33
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
4-
parser::Token,
4+
parser::{Parser, Token},
55
rendering::{
66
cursor::Cursor,
77
line::{SpaceConfig, StyledLineIterator},
@@ -76,13 +76,13 @@ where
7676
F: Font + Copy,
7777
{
7878
/// Starts processing a line.
79-
NextLine(Option<Token<'a>>, Cursor<F>),
79+
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),
8080

8181
/// Renders the processed line.
8282
DrawLine(StyledLineIterator<'a, C, F, JustifiedSpaceConfig, Justified>),
8383
}
8484

85-
impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, Justified, V>
85+
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, Justified, V>
8686
where
8787
C: PixelColor,
8888
F: Font + Copy,
@@ -92,8 +92,8 @@ where
9292

9393
#[inline]
9494
#[must_use]
95-
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
96-
State::NextLine(None, cursor)
95+
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
96+
State::NextLine(None, cursor, parser)
9797
}
9898
}
9999

@@ -109,21 +109,20 @@ where
109109
fn next(&mut self) -> Option<Self::Item> {
110110
loop {
111111
match self.state {
112-
State::NextLine(ref carried_token, ref cursor) => {
112+
State::NextLine(ref carried_token, cursor, ref mut parser) => {
113113
if !cursor.in_display_area() {
114114
break None;
115115
}
116116

117-
if carried_token.is_none() && self.parser.is_empty() {
117+
if carried_token.is_none() && parser.is_empty() {
118118
break None;
119119
}
120120

121+
let parser_clone = parser.clone();
121122
let max_line_width = cursor.line_width();
122-
let (width, total_whitespace_count, t) = self.style.measure_line(
123-
&mut self.parser.clone(),
124-
carried_token.clone(),
125-
max_line_width,
126-
);
123+
let (width, total_whitespace_count, t) =
124+
self.style
125+
.measure_line(parser, carried_token.clone(), max_line_width);
127126

128127
let space = max_line_width
129128
- (width - total_whitespace_count * F::total_char_width(' '));
@@ -138,8 +137,8 @@ where
138137
};
139138

140139
self.state = State::DrawLine(StyledLineIterator::new(
141-
self.parser.clone(),
142-
*cursor,
140+
parser_clone,
141+
cursor,
143142
space_info,
144143
self.style.text_style,
145144
carried_token.clone(),
@@ -151,10 +150,11 @@ where
151150
break pixel;
152151
}
153152

154-
self.parser = line_iterator.parser.clone();
155-
let carried_token = line_iterator.remaining_token();
156-
157-
self.state = State::NextLine(carried_token, line_iterator.cursor);
153+
self.state = State::NextLine(
154+
line_iterator.remaining_token(),
155+
line_iterator.cursor,
156+
line_iterator.parser.clone(),
157+
);
158158
}
159159
}
160160
}

src/alignment/left.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Left aligned text.
22
use crate::{
33
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
4-
parser::Token,
4+
parser::{Parser, Token},
55
rendering::{
66
cursor::Cursor,
77
line::{StyledLineIterator, UniformSpaceConfig},
@@ -28,13 +28,13 @@ where
2828
F: Font + Copy,
2929
{
3030
/// Starts processing a line.
31-
NextLine(Option<Token<'a>>, Cursor<F>),
31+
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),
3232

3333
/// Renders the processed line.
3434
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, LeftAligned>),
3535
}
3636

37-
impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, LeftAligned, V>
37+
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, LeftAligned, V>
3838
where
3939
C: PixelColor,
4040
F: Font + Copy,
@@ -44,8 +44,8 @@ where
4444

4545
#[inline]
4646
#[must_use]
47-
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
48-
State::NextLine(None, cursor)
47+
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
48+
State::NextLine(None, cursor, parser)
4949
}
5050
}
5151

@@ -61,18 +61,18 @@ where
6161
fn next(&mut self) -> Option<Self::Item> {
6262
loop {
6363
match self.state {
64-
State::NextLine(ref carried_token, ref cursor) => {
64+
State::NextLine(ref carried_token, cursor, ref parser) => {
6565
if !cursor.in_display_area() {
6666
break None;
6767
}
6868

69-
if carried_token.is_none() && self.parser.is_empty() {
69+
if carried_token.is_none() && parser.is_empty() {
7070
break None;
7171
}
7272

7373
self.state = State::DrawLine(StyledLineIterator::new(
74-
self.parser.clone(),
75-
*cursor,
74+
parser.clone(),
75+
cursor,
7676
UniformSpaceConfig {
7777
space_width: F::total_char_width(' '),
7878
},
@@ -86,10 +86,11 @@ where
8686
break pixel;
8787
}
8888

89-
self.parser = line_iterator.parser.clone();
90-
let carried_token = line_iterator.remaining_token();
91-
92-
self.state = State::NextLine(carried_token, line_iterator.cursor);
89+
self.state = State::NextLine(
90+
line_iterator.remaining_token(),
91+
line_iterator.cursor,
92+
line_iterator.parser.clone(),
93+
);
9394
}
9495
};
9596
}

src/alignment/right.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Right aligned text.
22
use crate::{
33
alignment::{HorizontalTextAlignment, VerticalTextAlignment},
4-
parser::Token,
4+
parser::{Parser, Token},
55
rendering::{
66
cursor::Cursor,
77
line::{StyledLineIterator, UniformSpaceConfig},
@@ -28,13 +28,13 @@ where
2828
F: Font + Copy,
2929
{
3030
/// Starts processing a line.
31-
NextLine(Option<Token<'a>>, Cursor<F>),
31+
NextLine(Option<Token<'a>>, Cursor<F>, Parser<'a>),
3232

3333
/// Renders the processed line.
3434
DrawLine(StyledLineIterator<'a, C, F, UniformSpaceConfig, RightAligned>),
3535
}
3636

37-
impl<'a, C, F, V> StateFactory<F> for StyledTextBox<'a, C, F, RightAligned, V>
37+
impl<'a, C, F, V> StateFactory<'a, F> for StyledTextBox<'a, C, F, RightAligned, V>
3838
where
3939
C: PixelColor,
4040
F: Font + Copy,
@@ -44,8 +44,8 @@ where
4444

4545
#[inline]
4646
#[must_use]
47-
fn create_state(&self, cursor: Cursor<F>) -> Self::PixelIteratorState {
48-
State::NextLine(None, cursor)
47+
fn create_state(&self, cursor: Cursor<F>, parser: Parser<'a>) -> Self::PixelIteratorState {
48+
State::NextLine(None, cursor, parser)
4949
}
5050
}
5151

@@ -61,26 +61,25 @@ where
6161
fn next(&mut self) -> Option<Self::Item> {
6262
loop {
6363
match self.state {
64-
State::NextLine(ref carried_token, ref mut cursor) => {
64+
State::NextLine(ref carried_token, mut cursor, ref mut parser) => {
6565
if !cursor.in_display_area() {
6666
break None;
6767
}
6868

69-
if carried_token.is_none() && self.parser.is_empty() {
69+
if carried_token.is_none() && parser.is_empty() {
7070
break None;
7171
}
7272

73+
let parser_clone = parser.clone();
7374
let max_line_width = cursor.line_width();
74-
let (width, _, _) = self.style.measure_line(
75-
&mut self.parser.clone(),
76-
carried_token.clone(),
77-
max_line_width,
78-
);
75+
let (width, _, _) =
76+
self.style
77+
.measure_line(parser, carried_token.clone(), max_line_width);
7978
cursor.advance_unchecked(max_line_width - width);
8079

8180
self.state = State::DrawLine(StyledLineIterator::new(
82-
self.parser.clone(),
83-
*cursor,
81+
parser_clone,
82+
cursor,
8483
UniformSpaceConfig {
8584
space_width: F::total_char_width(' '),
8685
},
@@ -94,10 +93,11 @@ where
9493
break pixel;
9594
}
9695

97-
self.parser = line_iterator.parser.clone();
98-
let carried_token = line_iterator.remaining_token();
99-
100-
self.state = State::NextLine(carried_token, line_iterator.cursor);
96+
self.state = State::NextLine(
97+
line_iterator.remaining_token(),
98+
line_iterator.cursor,
99+
line_iterator.parser.clone(),
100+
);
101101
}
102102
}
103103
}

0 commit comments

Comments
 (0)