Skip to content

Commit 9aa6444

Browse files
committed
Remove XBuf from ExecCtx
1 parent 765101c commit 9aa6444

3 files changed

Lines changed: 16 additions & 36 deletions

File tree

crates/engine_bibtex/src/bst.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ fn bst_execute_command(
260260
.set_offset(BufTy::Base, 2, globals.buffers.offset(BufTy::Base, 2) + 1);
261261

262262
// TODO: Associated method on ExecCtx
263-
ctx.lit_stk_ptr = 0;
263+
ctx.lit_stack.clear();
264264
ctx.bib_str_ptr = globals.pool.str_ptr();
265265

266266
ctx.mess_with_entries = false;
@@ -406,7 +406,7 @@ fn bst_iterate_command(
406406
.buffers
407407
.set_offset(BufTy::Base, 2, globals.buffers.offset(BufTy::Base, 2) + 1);
408408

409-
ctx.lit_stk_ptr = 0;
409+
ctx.lit_stack.clear();
410410
ctx.bib_str_ptr = globals.pool.str_ptr();
411411

412412
ctx.mess_with_entries = true;
@@ -828,7 +828,7 @@ fn bst_reverse_command(
828828
.buffers
829829
.set_offset(BufTy::Base, 2, globals.buffers.offset(BufTy::Base, 2) + 1);
830830

831-
ctx.lit_stk_ptr = 0;
831+
ctx.lit_stack.clear();
832832
ctx.bib_str_ptr = globals.pool.str_ptr();
833833

834834
ctx.mess_with_entries = true;

crates/engine_bibtex/src/char_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) enum LexClass {
3131

3232
impl LexClass {
3333
/// Get the `LexClass` of a character
34-
pub fn of(char: ASCIICode) -> LexClass {
34+
pub const fn of(char: ASCIICode) -> LexClass {
3535
LEX_CLASS[char as usize]
3636
}
3737
}
@@ -50,7 +50,7 @@ impl IdClass {
5050
}
5151
}
5252

53-
static LEX_CLASS: [LexClass; 256] = {
53+
const LEX_CLASS: [LexClass; 256] = {
5454
let mut lex_class = [LexClass::Other; 256];
5555

5656
const_for!(128..=255 => lex_class[..] = LexClass::Alpha);

crates/engine_bibtex/src/exec.rs

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ use crate::{
1818
check_brace_level, decr_brace_level, enough_text_chars, name_scan_for_and,
1919
von_name_ends_and_last_name_starts_stuff, von_token_found, QUOTE_NEXT_FN,
2020
},
21-
xbuf::{SafelyZero, XBuf},
2221
ASCIICode, Bibtex, BibtexError, BufPointer, GlobalItems, HashPointer, PoolPointer, StrIlk,
2322
StrNumber,
2423
};
2524
use std::ops::Index;
2625

27-
const LIT_STK_SIZE: usize = 100;
28-
2926
#[derive(Copy, Clone, PartialEq, Eq)]
3027
pub(crate) enum StkType {
3128
Integer,
@@ -56,14 +53,10 @@ impl ExecVal {
5653
}
5754
}
5855

59-
// SAFETY: We require our zero discriminant to be an integer, which is valid for any bit pattern, including 0
60-
unsafe impl SafelyZero for ExecVal {}
61-
6256
pub(crate) struct ExecCtx<'a, 'bib, 'cbs> {
6357
pub glbl_ctx: &'a mut Bibtex<'bib, 'cbs>,
6458
pub _default: HashPointer,
65-
pub(crate) lit_stack: Box<XBuf<ExecVal>>,
66-
pub lit_stk_ptr: usize,
59+
pub(crate) lit_stack: Vec<ExecVal>,
6760
pub mess_with_entries: bool,
6861
/// Pointer to the current top of the string pool, used to optimize certain string operations
6962
pub bib_str_ptr: StrNumber,
@@ -74,35 +67,22 @@ impl<'a, 'bib, 'cbs> ExecCtx<'a, 'bib, 'cbs> {
7467
ExecCtx {
7568
glbl_ctx,
7669
_default: 0,
77-
lit_stack: Box::new(XBuf::new(LIT_STK_SIZE + 1)),
78-
lit_stk_ptr: 0,
70+
lit_stack: Vec::new(),
7971
mess_with_entries: false,
8072
bib_str_ptr: 0,
8173
}
8274
}
8375

8476
pub(crate) fn push_stack(&mut self, val: ExecVal) {
85-
self.lit_stack[self.lit_stk_ptr] = val;
86-
87-
if self.lit_stk_ptr >= self.lit_stack.len() {
88-
self.grow_stack();
89-
}
90-
91-
self.lit_stk_ptr += 1;
77+
self.lit_stack.push(val);
9278
}
9379

9480
pub(crate) fn pop_stack(
9581
&mut self,
9682
pool: &mut StringPool,
9783
cites: &CiteInfo,
9884
) -> Result<ExecVal, BibtexError> {
99-
if self.lit_stk_ptr == 0 {
100-
write_logs("You can't pop an empty literal stack");
101-
bst_ex_warn_print(self, pool, cites)?;
102-
Ok(ExecVal::Illegal)
103-
} else {
104-
self.lit_stk_ptr -= 1;
105-
let pop = self.lit_stack[self.lit_stk_ptr];
85+
if let Some(pop) = self.lit_stack.pop() {
10686
if let ExecVal::String(str) = pop {
10787
if str >= self.bib_str_ptr {
10888
if str != pool.str_ptr() - 1 {
@@ -115,13 +95,13 @@ impl<'a, 'bib, 'cbs> ExecCtx<'a, 'bib, 'cbs> {
11595
}
11696
}
11797
Ok(pop)
98+
} else {
99+
write_logs("You can't pop an empty literal stack");
100+
bst_ex_warn_print(self, pool, cites)?;
101+
Ok(ExecVal::Illegal)
118102
}
119103
}
120104

121-
fn grow_stack(&mut self) {
122-
self.lit_stack.grow(LIT_STK_SIZE);
123-
}
124-
125105
pub(crate) fn glbl_ctx(&self) -> &Bibtex<'bib, 'cbs> {
126106
&*self.glbl_ctx
127107
}
@@ -278,7 +258,7 @@ fn pop_whole_stack(
278258
hash: &HashData,
279259
cites: &CiteInfo,
280260
) -> Result<(), BibtexError> {
281-
while ctx.lit_stk_ptr > 0 {
261+
while !ctx.lit_stack.is_empty() {
282262
pop_top_and_print(ctx, pool, hash, cites)?;
283263
}
284264
Ok(())
@@ -632,8 +612,8 @@ pub(crate) fn check_command_execution(
632612
hash: &HashData,
633613
cites: &CiteInfo,
634614
) -> Result<(), BibtexError> {
635-
if ctx.lit_stk_ptr != 0 {
636-
write_logs(&format!("ptr={}, stack=\n", ctx.lit_stk_ptr));
615+
if !ctx.lit_stack.is_empty() {
616+
write_logs(&format!("ptr={}, stack=\n", ctx.lit_stack.len()));
637617
pop_whole_stack(ctx, pool, hash, cites)?;
638618
write_logs("---the literal stack isn't empty");
639619
bst_ex_warn_print(ctx, pool, cites)?;

0 commit comments

Comments
 (0)