Skip to content

Commit 741997c

Browse files
authored
Move keywords and delimiters to use u8 slices (#848)
1 parent c359412 commit 741997c

5 files changed

Lines changed: 63 additions & 57 deletions

File tree

librubyfmt/src/delimiters.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ use crate::line_tokens::ConcreteLineToken;
22

33
#[derive(Debug, Clone, Eq, PartialEq)]
44
struct DelimiterPair {
5-
open: &'static str,
6-
close: &'static str,
5+
open: &'static [u8],
6+
close: &'static [u8],
77
}
88

99
impl DelimiterPair {
10-
fn new(open: &'static str, close: &'static str) -> Self {
10+
fn new(open: &'static [u8], close: &'static [u8]) -> Self {
1111
DelimiterPair { open, close }
1212
}
1313
}
@@ -21,71 +21,71 @@ pub struct BreakableDelims {
2121
impl BreakableDelims {
2222
pub fn for_method_call() -> Self {
2323
BreakableDelims {
24-
single_line: DelimiterPair::new("(", ")"),
25-
multi_line: DelimiterPair::new("(", ")"),
24+
single_line: DelimiterPair::new(b"(", b")"),
25+
multi_line: DelimiterPair::new(b"(", b")"),
2626
}
2727
}
2828

2929
pub fn for_return_kw() -> Self {
3030
BreakableDelims {
31-
single_line: DelimiterPair::new(" ", ""),
32-
multi_line: DelimiterPair::new(" [", "]"),
31+
single_line: DelimiterPair::new(b" ", b""),
32+
multi_line: DelimiterPair::new(b" [", b"]"),
3333
}
3434
}
3535

3636
pub fn for_kw() -> Self {
3737
BreakableDelims {
38-
single_line: DelimiterPair::new(" ", ""),
39-
multi_line: DelimiterPair::new("(", ")"),
38+
single_line: DelimiterPair::new(b" ", b""),
39+
multi_line: DelimiterPair::new(b"(", b")"),
4040
}
4141
}
4242

4343
pub fn for_block_params() -> Self {
4444
BreakableDelims {
45-
single_line: DelimiterPair::new(" |", "|"),
46-
multi_line: DelimiterPair::new(" |", "|"),
45+
single_line: DelimiterPair::new(b" |", b"|"),
46+
multi_line: DelimiterPair::new(b" |", b"|"),
4747
}
4848
}
4949

5050
pub fn for_array() -> Self {
5151
BreakableDelims {
52-
single_line: DelimiterPair::new("[", "]"),
53-
multi_line: DelimiterPair::new("[", "]"),
52+
single_line: DelimiterPair::new(b"[", b"]"),
53+
multi_line: DelimiterPair::new(b"[", b"]"),
5454
}
5555
}
5656

5757
pub fn for_when() -> Self {
5858
BreakableDelims {
59-
single_line: DelimiterPair::new(" ", ""),
60-
multi_line: DelimiterPair::new("", ""),
59+
single_line: DelimiterPair::new(b" ", b""),
60+
multi_line: DelimiterPair::new(b"", b""),
6161
}
6262
}
6363

6464
pub fn for_hash() -> Self {
6565
BreakableDelims {
66-
single_line: DelimiterPair::new("{", "}"),
67-
multi_line: DelimiterPair::new("{", "}"),
66+
single_line: DelimiterPair::new(b"{", b"}"),
67+
multi_line: DelimiterPair::new(b"{", b"}"),
6868
}
6969
}
7070

7171
pub fn for_brace_block() -> Self {
7272
BreakableDelims {
73-
single_line: DelimiterPair::new("{", " }"),
74-
multi_line: DelimiterPair::new("{", "}"),
73+
single_line: DelimiterPair::new(b"{", b" }"),
74+
multi_line: DelimiterPair::new(b"{", b"}"),
7575
}
7676
}
7777

7878
pub fn for_binary_op() -> Self {
7979
BreakableDelims {
80-
single_line: DelimiterPair::new("", ""),
81-
multi_line: DelimiterPair::new("", ""),
80+
single_line: DelimiterPair::new(b"", b""),
81+
multi_line: DelimiterPair::new(b"", b""),
8282
}
8383
}
8484

8585
pub fn for_parens() -> Self {
8686
BreakableDelims {
87-
single_line: DelimiterPair::new("(", ")"),
88-
multi_line: DelimiterPair::new("(", ")"),
87+
single_line: DelimiterPair::new(b"(", b")"),
88+
multi_line: DelimiterPair::new(b"(", b")"),
8989
}
9090
}
9191

librubyfmt/src/format_prism.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ fn format_begin_node<'src>(ps: &mut ParserState<'src>, begin_node: prism::BeginN
507507
// the body of a `def`
508508
ps.end_indent();
509509
} else {
510-
ps.emit_keyword("begin");
510+
ps.emit_keyword(b"begin");
511511
}
512512
ps.new_block(|ps| {
513513
// For implicit nodes, this newline was already emitted by the caller
@@ -1194,7 +1194,7 @@ fn format_ensure_node<'src>(ps: &mut ParserState<'src>, ensure_node: prism::Ensu
11941194
// aren't always handled with `format_node`, which usually handles this
11951195
ps.at_offset(ensure_node.location().start_offset());
11961196

1197-
ps.emit_keyword("ensure");
1197+
ps.emit_keyword(b"ensure");
11981198
ps.new_block(|ps| {
11991199
ps.emit_newline();
12001200
if let Some(statements) = ensure_node.statements() {
@@ -1506,7 +1506,7 @@ fn format_else_node<'src>(ps: &mut ParserState<'src>, else_node: prism::ElseNode
15061506
} else {
15071507
// In a ternary
15081508
ps.emit_space();
1509-
ps.emit_conditional_keyword(":");
1509+
ps.emit_conditional_keyword(b":");
15101510
ps.emit_space();
15111511
ps.with_start_of_line(false, |ps| {
15121512
format_node(
@@ -3409,14 +3409,14 @@ fn format_float_node<'src>(ps: &mut ParserState<'src>, float_node: prism::FloatN
34093409
}
34103410

34113411
fn format_for_node<'src>(ps: &mut ParserState<'src>, for_node: prism::ForNode<'src>) {
3412-
ps.emit_keyword("for");
3412+
ps.emit_keyword(b"for");
34133413
ps.emit_space();
34143414

34153415
ps.with_start_of_line(false, |ps| {
34163416
format_node(ps, for_node.index());
34173417

34183418
ps.emit_space();
3419-
ps.emit_keyword("in");
3419+
ps.emit_keyword(b"in");
34203420
ps.emit_space();
34213421

34223422
format_node(ps, for_node.collection());
@@ -3660,7 +3660,7 @@ fn format_inline_conditional<'src>(
36603660
ps: &mut ParserState<'src>,
36613661
predicate: prism::Node<'src>,
36623662
statements: Option<prism::StatementsNode<'src>>,
3663-
keyword: &'static str,
3663+
keyword: &'static [u8],
36643664
) {
36653665
if let Some(statements) = statements {
36663666
// There can only be a single statement in modifier form.
@@ -3732,7 +3732,7 @@ impl<'pr> Conditional<'pr> {
37323732

37333733
fn format_conditional_node<'src>(
37343734
ps: &mut ParserState<'src>,
3735-
conditional_keyword: &'static str,
3735+
conditional_keyword: &'static [u8],
37363736
requires_end_keyword: bool,
37373737
conditional: &Conditional<'src>,
37383738
) {
@@ -3806,7 +3806,7 @@ fn format_conditional_node<'src>(
38063806

38073807
fn format_conditional_block_form<'src>(
38083808
ps: &mut ParserState<'src>,
3809-
conditional_keyword: &'static str,
3809+
conditional_keyword: &'static [u8],
38103810
predicate: prism::Node<'src>,
38113811
statements: Option<prism::StatementsNode<'src>>,
38123812
subsequent_or_else: Option<prism::Node<'src>>,
@@ -3845,7 +3845,11 @@ fn format_if_node<'src>(ps: &mut ParserState<'src>, if_node: prism::IfNode<'src>
38453845
// different that we handle it in its own branch
38463846
if let Some(if_loc) = if_node.if_keyword_loc() {
38473847
let is_if_keyword = (if_loc.end_offset() - if_loc.start_offset()) == 2;
3848-
let conditional_keyword = if is_if_keyword { "if" } else { "elsif" };
3848+
let conditional_keyword = if is_if_keyword {
3849+
b"if" as &[u8]
3850+
} else {
3851+
b"elsif"
3852+
};
38493853

38503854
format_conditional_node(
38513855
ps,
@@ -4544,7 +4548,7 @@ fn format_post_execution_node<'src>(
45444548
ps: &mut ParserState<'src>,
45454549
post_execution_node: prism::PostExecutionNode<'src>,
45464550
) {
4547-
ps.emit_keyword("END");
4551+
ps.emit_keyword(b"END");
45484552
ps.emit_space();
45494553
ps.emit_open_curly_bracket();
45504554

@@ -4567,7 +4571,7 @@ fn format_pre_execution_node<'src>(
45674571
ps: &mut ParserState<'src>,
45684572
pre_execution_node: prism::PreExecutionNode<'src>,
45694573
) {
4570-
ps.emit_keyword("BEGIN");
4574+
ps.emit_keyword(b"BEGIN");
45714575
ps.emit_space();
45724576
ps.emit_open_curly_bracket();
45734577

@@ -4640,7 +4644,7 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc
46404644
// aren't always handled with `format_node`, which usually handles this
46414645
ps.at_offset(rescue_node.location().start_offset());
46424646

4643-
ps.emit_keyword("rescue");
4647+
ps.emit_keyword(b"rescue");
46444648
let exceptions = rescue_node.exceptions();
46454649
let reference = rescue_node.reference();
46464650
if !exceptions.is_empty() {
@@ -4697,7 +4701,7 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc
46974701
}
46984702

46994703
fn format_retry_node(ps: &mut ParserState) {
4700-
ps.emit_keyword("retry");
4704+
ps.emit_keyword(b"retry");
47014705
}
47024706

47034707
fn format_return_node<'src>(ps: &mut ParserState<'src>, return_node: prism::ReturnNode<'src>) {
@@ -4803,11 +4807,11 @@ fn format_undef_node<'src>(ps: &mut ParserState<'src>, undef_node: prism::UndefN
48034807
}
48044808

48054809
fn format_unless_node<'src>(ps: &mut ParserState<'src>, unless_node: prism::UnlessNode<'src>) {
4806-
format_conditional_node(ps, "unless", true, &Conditional::Unless(unless_node));
4810+
format_conditional_node(ps, b"unless", true, &Conditional::Unless(unless_node));
48074811
}
48084812

48094813
fn format_until_node<'src>(ps: &mut ParserState<'src>, until_node: prism::UntilNode<'src>) {
4810-
format_conditional_node(ps, "until", true, &Conditional::Until(until_node));
4814+
format_conditional_node(ps, b"until", true, &Conditional::Until(until_node));
48114815
}
48124816

48134817
fn format_when_node<'src>(ps: &mut ParserState<'src>, when_node: prism::WhenNode<'src>) {
@@ -4845,7 +4849,7 @@ fn format_when_node<'src>(ps: &mut ParserState<'src>, when_node: prism::WhenNode
48454849
}
48464850

48474851
fn format_while_node<'src>(ps: &mut ParserState<'src>, while_node: prism::WhileNode<'src>) {
4848-
format_conditional_node(ps, "while", true, &Conditional::While(while_node));
4852+
format_conditional_node(ps, b"while", true, &Conditional::While(while_node));
48494853
}
48504854

48514855
fn format_x_string_node<'src>(ps: &mut ParserState<'src>, x_string_node: prism::XStringNode<'src>) {

librubyfmt/src/line_tokens.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ pub enum ConcreteLineToken<'src> {
3333
depth: u32,
3434
},
3535
Keyword {
36-
keyword: &'static str,
36+
keyword: &'static [u8],
3737
},
3838
DefKeyword,
3939
ClassKeyword,
4040
ModuleKeyword,
4141
DoKeyword,
4242
ConditionalKeyword {
43-
contents: &'static str,
43+
contents: &'static [u8],
4444
},
4545
DirectPart {
4646
part: Cow<'src, [u8]>,
@@ -72,7 +72,7 @@ pub enum ConcreteLineToken<'src> {
7272
contents: Cow<'src, [u8]>,
7373
},
7474
Delim {
75-
contents: &'static str,
75+
contents: &'static [u8],
7676
},
7777
End,
7878
HeredocClose {
@@ -97,8 +97,8 @@ impl<'src> ConcreteLineToken<'src> {
9797
match self {
9898
Self::HardNewLine => Cow::Borrowed(b"\n"),
9999
Self::Indent { depth } => get_indent(depth as usize),
100-
Self::Keyword { keyword } => Cow::Borrowed(keyword.as_bytes()),
101-
Self::ConditionalKeyword { contents } => Cow::Borrowed(contents.as_bytes()),
100+
Self::Keyword { keyword } => Cow::Borrowed(keyword),
101+
Self::ConditionalKeyword { contents } => Cow::Borrowed(contents),
102102
Self::DoKeyword => Cow::Borrowed(b"do"),
103103
Self::ClassKeyword => Cow::Borrowed(b"class"),
104104
Self::DefKeyword => Cow::Borrowed(b"def"),
@@ -122,7 +122,7 @@ impl<'src> ConcreteLineToken<'src> {
122122
Self::LTStringContent { content } => content,
123123
Self::SingleSlash => Cow::Borrowed(b"\\"),
124124
Self::Comment { contents } => contents,
125-
Self::Delim { contents } => Cow::Borrowed(contents.as_bytes()),
125+
Self::Delim { contents } => Cow::Borrowed(contents),
126126
Self::End => Cow::Borrowed(b"end"),
127127
Self::HeredocClose { symbol } => Cow::Owned(symbol),
128128
Self::HeredocStart { symbol, .. } => Cow::Borrowed(symbol),
@@ -172,14 +172,16 @@ impl<'src> ConcreteLineToken<'src> {
172172
Self::DirectPart { part } => {
173173
part.as_ref() == b"}" || part.as_ref() == b"]" || part.as_ref() == b")"
174174
}
175-
Self::Delim { contents } => *contents == "}" || *contents == "]" || *contents == ")",
175+
Self::Delim { contents } => *contents == b"}" || *contents == b"]" || *contents == b")",
176176
_ => false,
177177
}
178178
}
179179

180180
fn is_conditional_spaced_token(&self) -> bool {
181181
match self {
182-
Self::ConditionalKeyword { contents } => !(*contents == "else" || *contents == "elsif"),
182+
Self::ConditionalKeyword { contents } => {
183+
!(*contents == b"else" || *contents == b"elsif")
184+
}
183185
Self::Dot | Self::LonelyOperator => false,
184186
Self::DirectPart { part } => part.as_ref() != b"&.",
185187
_ => true,

librubyfmt/src/parser_state.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,19 +563,19 @@ impl<'src> ParserState<'src> {
563563
}
564564

565565
pub(crate) fn emit_rescue(&mut self) {
566-
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: "rescue" });
566+
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: b"rescue" });
567567
}
568568

569569
pub(crate) fn emit_case_keyword(&mut self) {
570-
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: "case" });
570+
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: b"case" });
571571
}
572572

573573
pub(crate) fn emit_when_keyword(&mut self) {
574-
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: "when" });
574+
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: b"when" });
575575
}
576576

577577
pub(crate) fn emit_in_keyword(&mut self) {
578-
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: "in" });
578+
self.push_concrete_token(ConcreteLineToken::Keyword { keyword: b"in" });
579579
}
580580

581581
pub(crate) fn emit_do_keyword(&mut self) {
@@ -591,7 +591,7 @@ impl<'src> ParserState<'src> {
591591
}
592592

593593
pub(crate) fn emit_else(&mut self) {
594-
self.emit_conditional_keyword("else");
594+
self.emit_conditional_keyword(b"else");
595595
}
596596

597597
pub(crate) fn emit_data(&mut self, data: &'src [u8]) {
@@ -702,11 +702,11 @@ impl<'src> ParserState<'src> {
702702
self.push_concrete_token(ConcreteLineToken::DefKeyword);
703703
}
704704

705-
pub(crate) fn emit_keyword(&mut self, keyword: &'static str) {
705+
pub(crate) fn emit_keyword(&mut self, keyword: &'static [u8]) {
706706
self.push_concrete_token(ConcreteLineToken::Keyword { keyword });
707707
}
708708

709-
pub(crate) fn emit_conditional_keyword(&mut self, contents: &'static str) {
709+
pub(crate) fn emit_conditional_keyword(&mut self, contents: &'static [u8]) {
710710
self.push_concrete_token(ConcreteLineToken::ConditionalKeyword { contents });
711711
}
712712
}
@@ -930,7 +930,7 @@ impl<'src> ParserState<'src> {
930930
/// Format a conditional modifier expression (e.g., `x if y` or `x unless y`).
931931
pub(crate) fn conditional_layout_of<FP, FS>(
932932
&mut self,
933-
keyword: &'static str,
933+
keyword: &'static [u8],
934934
format_predicate: FP,
935935
format_statement: FS,
936936
) where

librubyfmt/src/render_targets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,13 +516,13 @@ pub enum ConditionalLayoutPhase {
516516
pub struct ConditionalLayoutEntry<'src> {
517517
predicate_tokens: Vec<AbstractLineToken<'src>>,
518518
statement_tokens: Vec<AbstractLineToken<'src>>,
519-
keyword: &'static str,
519+
keyword: &'static [u8],
520520
indent_depth: u32,
521521
phase: ConditionalLayoutPhase,
522522
}
523523

524524
impl<'src> ConditionalLayoutEntry<'src> {
525-
pub fn new(keyword: &'static str, indent_depth: u32) -> Self {
525+
pub fn new(keyword: &'static [u8], indent_depth: u32) -> Self {
526526
ConditionalLayoutEntry {
527527
predicate_tokens: Vec::new(),
528528
statement_tokens: Vec::new(),

0 commit comments

Comments
 (0)