Skip to content

Commit f71140a

Browse files
committed
Make max_gap work in the DFA
Signed-off-by: Sean Young <[email protected]>
1 parent b051b26 commit f71140a

File tree

5 files changed

+73
-53
lines changed

5 files changed

+73
-53
lines changed

Diff for: irp/src/build_bpf.rs

+38-34
Original file line numberDiff line numberDiff line change
@@ -312,23 +312,25 @@ impl<'a> Builder<'a> {
312312

313313
self.builder.position_at_end(ok);
314314

315-
let ok = context.append_basic_block(function, "ok");
316-
317-
let res = self
318-
.builder
319-
.build_int_compare(
320-
IntPredicate::ULE,
321-
length,
322-
i32.const_int(*max as u64, false),
323-
"max",
324-
)
325-
.unwrap();
326-
327-
self.builder
328-
.build_conditional_branch(res, ok, next)
329-
.unwrap();
330-
331-
self.builder.position_at_end(ok);
315+
if let Some(max) = max {
316+
let ok = context.append_basic_block(function, "ok");
317+
318+
let res = self
319+
.builder
320+
.build_int_compare(
321+
IntPredicate::ULE,
322+
length,
323+
i32.const_int(*max as u64, false),
324+
"max",
325+
)
326+
.unwrap();
327+
328+
self.builder
329+
.build_conditional_branch(res, ok, next)
330+
.unwrap();
331+
332+
self.builder.position_at_end(ok);
333+
}
332334
}
333335
Action::Gap {
334336
length: Length::Range(min, max),
@@ -360,23 +362,25 @@ impl<'a> Builder<'a> {
360362

361363
self.builder.position_at_end(ok);
362364

363-
let ok = context.append_basic_block(function, "ok");
364-
365-
let res = self
366-
.builder
367-
.build_int_compare(
368-
IntPredicate::ULE,
369-
length,
370-
i32.const_int(*max as u64, false),
371-
"max",
372-
)
373-
.unwrap();
374-
375-
self.builder
376-
.build_conditional_branch(res, ok, next)
377-
.unwrap();
378-
379-
self.builder.position_at_end(ok);
365+
if let Some(max) = max {
366+
let ok = context.append_basic_block(function, "ok");
367+
368+
let res = self
369+
.builder
370+
.build_int_compare(
371+
IntPredicate::ULE,
372+
length,
373+
i32.const_int(*max as u64, false),
374+
"max",
375+
)
376+
.unwrap();
377+
378+
self.builder
379+
.build_conditional_branch(res, ok, next)
380+
.unwrap();
381+
382+
self.builder.position_at_end(ok);
383+
}
380384
}
381385
Action::Set { var, expr } => {
382386
let value = self.emit(expr, context);

Diff for: irp/src/build_dfa.rs

+25-10
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,16 @@ impl<'a> Builder<'a> {
255255
(length * (100 - self.options.eps)) / 100,
256256
);
257257

258-
let max = std::cmp::min(
258+
let max = std::cmp::max(
259259
length + self.options.aeps,
260260
(length * (100 + self.options.eps)) / 100,
261261
);
262262

263-
Length::Range(min, max)
263+
if self.options.max_gap > 0 && max < self.options.max_gap {
264+
Length::Range(min, Some(max))
265+
} else {
266+
Length::Range(min, None)
267+
}
264268
} else {
265269
Length::Expression(length)
266270
}
@@ -381,7 +385,10 @@ fn replace_vars(expr: &Rc<Expression>, vars: &HashMap<&str, Rc<Expression>>) ->
381385
impl Length {
382386
fn overlaps(&self, other: &Self) -> bool {
383387
if let (Length::Range(min1, max1), Length::Range(min2, max2)) = (self, other) {
384-
!(max1 < min2 || max2 < min1)
388+
let max1 = max1.unwrap_or(u32::MAX);
389+
let max2 = max2.unwrap_or(u32::MAX);
390+
391+
!(max1 < *min2 || max2 < *min1)
385392
} else {
386393
false
387394
}
@@ -400,13 +407,21 @@ impl Length {
400407

401408
#[test]
402409
fn overlaps() {
403-
assert!(!Length::Range(1, 10).overlaps(&Length::Range(11, 20)));
404-
assert!(!Length::Range(11, 20).overlaps(&Length::Range(1, 10)));
410+
assert!(!Length::Range(1, Some(10)).overlaps(&Length::Range(11, Some(20))));
411+
assert!(!Length::Range(11, Some(20)).overlaps(&Length::Range(1, Some(10))));
412+
413+
assert!(Length::Range(1, Some(11)).overlaps(&Length::Range(11, Some(20))));
414+
assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(1, Some(11))));
415+
416+
assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(11, Some(20))));
417+
assert!(Length::Range(5, Some(25)).overlaps(&Length::Range(11, Some(20))));
418+
assert!(Length::Range(11, Some(20)).overlaps(&Length::Range(5, Some(25))));
419+
420+
assert!(Length::Range(5, None).overlaps(&Length::Range(11, Some(20))));
421+
assert!(!Length::Range(21, None).overlaps(&Length::Range(11, Some(20))));
405422

406-
assert!(Length::Range(1, 11).overlaps(&Length::Range(11, 20)));
407-
assert!(Length::Range(11, 20).overlaps(&Length::Range(1, 11)));
423+
assert!(Length::Range(5, Some(25)).overlaps(&Length::Range(11, None)));
424+
assert!(!Length::Range(11, Some(20)).overlaps(&Length::Range(21, None)));
408425

409-
assert!(Length::Range(11, 20).overlaps(&Length::Range(11, 20)));
410-
assert!(Length::Range(5, 25).overlaps(&Length::Range(11, 20)));
411-
assert!(Length::Range(11, 20).overlaps(&Length::Range(5, 25)));
426+
assert!(Length::Range(5, None).overlaps(&Length::Range(11, None)));
412427
}

Diff for: irp/src/build_nfa.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) struct Edge {
2222
#[derive(PartialEq, Debug, Hash, Eq, Clone)]
2323
pub(crate) enum Length {
2424
Expression(Rc<Expression>),
25-
Range(u32, u32),
25+
Range(u32, Option<u32>),
2626
}
2727

2828
#[derive(PartialEq, Debug, Hash, Eq, Clone)]
@@ -1584,7 +1584,8 @@ impl fmt::Display for Length {
15841584
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15851585
match self {
15861586
Length::Expression(e) => write!(f, "{e}"),
1587-
Length::Range(min, max) => write!(f, "{min}..{max}"),
1587+
Length::Range(min, None) => write!(f, "{min}.."),
1588+
Length::Range(min, Some(max)) => write!(f, "{min}..{max}"),
15881589
}
15891590
}
15901591
}

Diff for: irp/src/decoder.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,10 @@ impl<'a> Decoder<'a> {
182182
) -> bool {
183183
match ir {
184184
Some(InfraredData::Gap(received)) => {
185-
if expected > self.options.max_gap as i64 && *received >= self.options.max_gap {
185+
if self.options.max_gap > 0
186+
&& expected > self.options.max_gap as i64
187+
&& *received >= self.options.max_gap
188+
{
186189
trace!("large gap matched gap {} (expected {})", received, expected,);
187190
*ir = None;
188191
true
@@ -376,7 +379,7 @@ impl<'a> Decoder<'a> {
376379
} else if self.consume_flash_range(
377380
&mut ir,
378381
(*min).into(),
379-
(*max).into(),
382+
max.unwrap_or(u32::MAX).into(),
380383
*complete,
381384
) {
382385
continue;
@@ -407,7 +410,7 @@ impl<'a> Decoder<'a> {
407410
} else if self.consume_gap_range(
408411
&mut ir,
409412
(*min).into(),
410-
(*max).into(),
413+
max.unwrap_or(u32::MAX).into(),
411414
*complete,
412415
) {
413416
continue;

Diff for: irp/tests/bpf_decoding.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ fn rc5() {
2323
source: file!(),
2424
aeps: 100,
2525
eps: 3,
26+
max_gap: 20000,
2627
..Default::default()
2728
};
2829

2930
let dfa = irp.compile(&options).unwrap();
3031

31-
dfa.dotgraphviz("lircd.dot");
32-
3332
let (object, vars) = dfa.compile_bpf(&options).unwrap();
3433

3534
let mut obj = Object::parse(&object).unwrap();
@@ -58,8 +57,6 @@ fn rc5() {
5857
rel_maps.push((name.as_str(), 7, map));
5958
}
6059

61-
println!("value_size: {value_size:?}");
62-
6360
obj.relocate_maps(rel_maps.into_iter(), &text_sections)
6461
.unwrap();
6562

0 commit comments

Comments
 (0)