Skip to content

Commit b35e8b9

Browse files
committed
Merge branch 'fix-allocs' into 'master'
Elide trivially redundant allocations See merge request mkjeldsen/commitmsgfmt!41
2 parents fe466c5 + 34ac2d8 commit b35e8b9

File tree

2 files changed

+47
-76
lines changed

2 files changed

+47
-76
lines changed

src/commitmsgfmt.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ impl CommitMsgFmt {
2828
let mut buf = String::new();
2929
for tok in msg {
3030
match *tok {
31-
Comment(ref c) => {
32-
buf.push_str(c.as_str());
33-
buf.push('\n');
34-
}
35-
Scissored(ref s) => {
36-
buf.push_str(s.as_str());
31+
Comment(ref s) | Literal(ref s) | Scissored(ref s) | Trailer(ref s) => {
32+
buf.push_str(s);
3733
}
3834
ListItem(ref indent, ref li, ref s) => {
3935
let indent = &indent.0;
@@ -45,28 +41,22 @@ impl CommitMsgFmt {
4541
buf.push_str(li);
4642

4743
self.wrap_paragraph_into(&mut buf, s, Some(&continuation));
48-
buf.push('\n');
49-
}
50-
Literal(ref l) => {
51-
buf.push_str(l.as_str());
5244
}
5345
Paragraph(ref p) => {
5446
self.wrap_paragraph_into(&mut buf, p, None);
55-
buf.push('\n');
5647
}
5748
Footnote(ref key, ref rest) => {
5849
buf.push_str(key);
5950
buf.push(' ');
6051
let continuation = " ".repeat(key.graphemes(true).count() + 1);
6152
self.wrap_paragraph_into(&mut buf, rest.trim(), Some(&continuation));
62-
buf.push('\n');
6353
}
64-
Subject(ref s) | Trailer(ref s) => {
65-
buf.push_str(s.as_str());
66-
buf.push('\n');
54+
Subject(ref s) => {
55+
buf.push_str(s);
6756
}
68-
VerticalSpace => buf.push('\n'),
57+
VerticalSpace => {}
6958
}
59+
buf.push('\n');
7060
}
7161

7262
buf

src/parser.rs

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ pub struct ListType(pub String);
88
pub struct ListIndent(pub String);
99

1010
#[derive(Debug, PartialEq, Eq)]
11-
pub enum Token {
12-
Comment(String),
11+
pub enum Token<'input> {
12+
Comment(&'input str),
1313
Footnote(String, String),
1414
ListItem(ListIndent, ListType, String),
15-
Literal(String),
15+
Literal(&'input str),
1616
Paragraph(String),
1717
Subject(String),
18-
Scissored(String),
19-
Trailer(String),
18+
Scissored(&'input str),
19+
Trailer(&'input str),
2020
VerticalSpace,
2121
}
2222

@@ -58,22 +58,13 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
5858
let mut px = false;
5959
for line in lines {
6060
if has_scissors {
61-
match *toks.last_mut().expect("has_scissors") {
62-
Token::Scissored(ref mut s) => {
63-
s.push_str(line);
64-
s.push('\n');
65-
}
66-
_ => unreachable!(),
67-
}
61+
toks.push(Token::Scissored(line));
6862
} else if line.starts_with(comment_char) {
6963
let t = if &line[1..] == " ------------------------ >8 ------------------------" {
7064
has_scissors = true;
71-
let mut raw = String::with_capacity(20 * 60); // Toilet maths.
72-
raw.push_str(line);
73-
raw.push('\n'); // Recover linefeed lost from iterator.
74-
Token::Scissored(raw)
65+
Token::Scissored(line)
7566
} else {
76-
Token::Comment(line.to_owned())
67+
Token::Comment(line)
7768
};
7869
toks.push(t);
7970
} else if blank_or_empty.is_match(line) {
@@ -92,7 +83,7 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
9283
let rest = splitter.next().unwrap().trim().to_owned();
9384
toks.push(Token::Footnote(key, rest));
9485
} else if trailer.is_match(line) {
95-
toks.push(Token::Trailer(line.to_owned()));
86+
toks.push(Token::Trailer(line));
9687
} else if let Some(y) = match toks.last_mut() {
9788
Some(&mut Token::Footnote(_, ref mut b)) => {
9889
b.push(' ');
@@ -117,9 +108,7 @@ pub fn parse(input: &str, comment_char: char) -> Vec<Token> {
117108
if list_item.is_match(line) {
118109
Some(list_item_from_line(&list_item, line))
119110
} else if indented.is_match(line) {
120-
let mut raw = line.to_owned();
121-
raw.push('\n'); // Recover linefeed lost from iterator.
122-
Some(Token::Literal(raw))
111+
Some(Token::Literal(line))
123112
} else {
124113
px = false;
125114
Some(Token::Paragraph(line.trim().to_owned()))
@@ -180,7 +169,7 @@ fn parse_subject(line: &str, toks: &mut Vec<Token>) {
180169
}
181170
}
182171

183-
fn list_item_from_line(pat: &Regex, line: &str) -> Token {
172+
fn list_item_from_line<'a>(pat: &Regex, line: &str) -> Token<'a> {
184173
let captures = pat.captures(line).unwrap();
185174
let indent = captures.name("indent").unwrap();
186175
let li = captures.name("li").unwrap();
@@ -229,24 +218,20 @@ mod tests {
229218

230219
#[test]
231220
fn parses_default_comment() {
232-
assert_eq!(super::parse("# foo", '#'), [Comment("# foo".to_owned())]);
221+
assert_eq!(super::parse("# foo", '#'), [Comment("# foo")]);
233222
}
234223

235224
#[test]
236225
fn parses_custom_comment() {
237-
assert_eq!(super::parse("@ foo", '@'), [Comment("@ foo".to_owned())]);
226+
assert_eq!(super::parse("@ foo", '@'), [Comment("@ foo")]);
238227
assert_eq!(super::parse("# foo", '@'), [Subject("# foo".to_owned())]);
239228
}
240229

241230
#[test]
242231
fn parses_mixed_comment_and_content() {
243232
assert_eq!(
244233
parse("# foo\n\n # bar"),
245-
[
246-
Comment("# foo".to_owned()),
247-
VerticalSpace,
248-
Subject("# bar".to_owned()),
249-
],
234+
[Comment("# foo"), VerticalSpace, Subject("# bar".to_owned()),],
250235
);
251236
}
252237

@@ -273,7 +258,7 @@ mod tests {
273258
#[test]
274259
fn parses_fitting_subject() {
275260
let s = "f".repeat(SUBJECT_CHAR_LIMIT);
276-
assert_eq!(parse(&s), [Subject(s)]);
261+
assert_eq!(parse(&s), [Subject(s.to_owned())]);
277262
}
278263

279264
#[test]
@@ -299,7 +284,10 @@ mod tests {
299284
prefix = autosquash_prefix,
300285
subject = original_subject
301286
);
302-
assert_eq!(parse(&autosquash_subject), [Subject(autosquash_subject),],);
287+
assert_eq!(
288+
parse(&autosquash_subject),
289+
[Subject(autosquash_subject.to_owned()),],
290+
);
303291
}
304292
}
305293

@@ -371,7 +359,7 @@ paragraphs
371359
Paragraph("this is one paragraph".to_owned()),
372360
VerticalSpace,
373361
Paragraph("this is".to_owned()),
374-
Comment("# two".to_owned()),
362+
Comment("# two"),
375363
Paragraph("paragraphs".to_owned()),
376364
]
377365
);
@@ -417,8 +405,8 @@ some other paragraph
417405
VerticalSpace,
418406
Paragraph("some paragraph".to_owned()),
419407
VerticalSpace,
420-
Literal(" some 4-space literal\n".to_owned()),
421-
Literal(" continuation\n".to_owned()),
408+
Literal(" some 4-space literal"),
409+
Literal(" continuation"),
422410
VerticalSpace,
423411
Paragraph("some other paragraph no literal without vertical space".to_owned()),
424412
],
@@ -448,9 +436,9 @@ some other paragraph
448436
VerticalSpace,
449437
Paragraph("some paragraph".to_owned()),
450438
VerticalSpace,
451-
Literal("\tsome 4-space literal\n".to_owned()),
452-
Literal("\t continuation\n".to_owned()),
453-
Literal("\t\tcontinuation\n".to_owned()),
439+
Literal("\tsome 4-space literal"),
440+
Literal("\t continuation"),
441+
Literal("\t\tcontinuation"),
454442
VerticalSpace,
455443
Paragraph("some other paragraph no literal without vertical space".to_owned()),
456444
],
@@ -487,9 +475,9 @@ some other paragraph
487475
VerticalSpace,
488476
Paragraph("some paragraph".to_owned()),
489477
VerticalSpace,
490-
Literal(" some 4-space literal\n".to_owned()),
491-
Literal(" some 4-space literal\n".to_owned()),
492-
Literal(" some 4-space literal\n".to_owned()),
478+
Literal(" some 4-space literal"),
479+
Literal(" some 4-space literal"),
480+
Literal(" some 4-space literal"),
493481
VerticalSpace,
494482
Paragraph("some other paragraph".to_owned()),
495483
],
@@ -520,10 +508,10 @@ Signed-off-by: Jane Doe <[email protected]>
520508
VerticalSpace,
521509
Subject("subject".to_owned()),
522510
VerticalSpace,
523-
Trailer("Fixes: All the things".to_owned()),
524-
Trailer("Cc: John Doe <[email protected]>".to_owned()),
525-
Trailer("Reviewed-by: NSA".to_owned()),
526-
Trailer("Signed-off-by: Jane Doe <[email protected]>".to_owned()),
511+
Trailer("Fixes: All the things"),
512+
Trailer("Cc: John Doe <[email protected]>"),
513+
Trailer("Reviewed-by: NSA"),
514+
Trailer("Signed-off-by: Jane Doe <[email protected]>"),
527515
],
528516
);
529517
}
@@ -874,15 +862,11 @@ do
874862
VerticalSpace,
875863
Paragraph("format this".to_owned()),
876864
VerticalSpace,
877-
Scissored(
878-
r#"# ------------------------ >8 ------------------------
879-
do
880-
not
881-
format
882-
this
883-
"#
884-
.to_owned()
885-
),
865+
Scissored("# ------------------------ >8 ------------------------"),
866+
Scissored("do"),
867+
Scissored(" not"),
868+
Scissored(" format"),
869+
Scissored(" this"),
886870
],
887871
);
888872
}
@@ -910,13 +894,10 @@ do
910894
VerticalSpace,
911895
Paragraph("# ------------------------ >8 ------------------------ above is not a comment; do the needful".to_owned()),
912896
VerticalSpace,
913-
Scissored(
914-
r#"$ ------------------------ >8 ------------------------
915-
do
916-
not
917-
format
918-
"#.to_owned()
919-
),
897+
Scissored("$ ------------------------ >8 ------------------------"),
898+
Scissored("do"),
899+
Scissored(" not"),
900+
Scissored(" format"),
920901
],
921902
);
922903
}

0 commit comments

Comments
 (0)