ifc_lite_core/fast_parse_indices.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! The one index rule for tessellated face sets, and the raw-byte reading of
6//! a `CoordIndex` that applies it (#7689).
7//!
8//! `IfcTriangulatedFaceSet.CoordIndex` is `LIST [1:?] OF LIST [3:3] OF
9//! IfcPositiveInteger`; `PnIndex` and `IfcIndexedPolygonalFace.CoordIndex`
10//! are lists of the same `IfcPositiveInteger`. One rule converts each value
11//! ([`ifc_index_to_zero_based`]): an INTEGER in `1..=u32::MAX` is 1-based and
12//! becomes `value - 1`; `0`, a negative value, a value past `u32::MAX` and a
13//! REAL are not indices. The decoded reading
14//! (`AttributeValue::parse_index_list`) and the byte reading here both apply
15//! it, and both read a triangle that is not exactly three entries as one
16//! dropped slot, so a short or long face never shifts the faces after it.
17//!
18//! The byte reading is only an accelerator for the decoded one. Where the
19//! text is anything but digits, commas, parentheses and trivia (a sign, a
20//! REAL, a `$`, a nested list where an index belongs) it answers `None` and
21//! the caller decodes the record instead, so the two cannot disagree on
22//! malformed input: the byte reading never decides a case it does not share.
23
24use crate::parser::skip_step_trivia;
25
26/// The slot value of an index that fails the rule. Every consumer
27/// bounds-checks (`index >= vertex_count`) and drops the triangle, so a
28/// slot keeps its position (for parallel `TexCoordIndex` / `ColourIndex`
29/// lists) without ever naming a real vertex.
30pub const INVALID_INDEX: u32 = u32::MAX;
31
32/// The one rule for a 1-based IFC index (`IfcPositiveInteger`): its 0-based
33/// position, or `None` for `0`, a negative value or one past `u32::MAX`.
34#[inline]
35pub(crate) fn ifc_index_to_zero_based(one_based: i64) -> Option<u32> {
36 u32::try_from(one_based).ok().and_then(|v| v.checked_sub(1))
37}
38
39/// Read a `CoordIndex` list value (`((1,2,3),(2,1,4),...)`, trivia allowed
40/// anywhere) as flat 0-based triangle corners, three per face, with
41/// [`INVALID_INDEX`] for values that fail the rule and for all three corners
42/// of a face that is not exactly three entries long.
43///
44/// `None` when the text holds anything the decoded reading must settle (see
45/// the module doc), including an unterminated comment or a list that never
46/// closes.
47pub(super) fn read_coord_index(bytes: &[u8]) -> Option<Vec<u32>> {
48 let mut out = Vec::with_capacity(super::estimate_int_count(bytes));
49 let mut pos = skip_step_trivia(bytes, 0)?;
50 if bytes.get(pos) != Some(&b'(') {
51 return None;
52 }
53 pos = skip_step_trivia(bytes, pos + 1)?;
54 if bytes.get(pos) == Some(&b')') {
55 return ends_here(bytes, pos + 1).then_some(out);
56 }
57 loop {
58 if bytes.get(pos) != Some(&b'(') {
59 return None;
60 }
61 pos = skip_step_trivia(bytes, pos + 1)?;
62 let mut face = [INVALID_INDEX; 3];
63 let mut count = 0usize;
64 if bytes.get(pos) != Some(&b')') {
65 loop {
66 let (value, next) = read_index(bytes, pos)?;
67 if let Some(slot) = face.get_mut(count) {
68 *slot = value;
69 }
70 count += 1;
71 pos = skip_step_trivia(bytes, next)?;
72 match *bytes.get(pos)? {
73 b',' => pos = skip_step_trivia(bytes, pos + 1)?,
74 b')' => break,
75 _ => return None,
76 }
77 }
78 }
79 if count != 3 {
80 face = [INVALID_INDEX; 3];
81 }
82 out.extend_from_slice(&face);
83 pos = skip_step_trivia(bytes, pos + 1)?;
84 match *bytes.get(pos)? {
85 b',' => pos = skip_step_trivia(bytes, pos + 1)?,
86 b')' => return ends_here(bytes, pos + 1).then_some(out),
87 _ => return None,
88 }
89 }
90}
91
92/// Only trivia follows the list.
93#[inline]
94fn ends_here(bytes: &[u8], pos: usize) -> bool {
95 skip_step_trivia(bytes, pos) == Some(bytes.len())
96}
97
98/// The unsigned digit run at `pos` under the index rule, and the position
99/// after it. `None` when no digit starts there (a sign, `$`, `(`, ...) or
100/// the run is past `i64::MAX`, where the tokenizer, not this rule, decides.
101#[inline]
102fn read_index(bytes: &[u8], mut pos: usize) -> Option<(u32, usize)> {
103 let start = pos;
104 let mut value: i64 = 0;
105 while let Some(&b) = bytes.get(pos).filter(|b| b.is_ascii_digit()) {
106 value = value.checked_mul(10)?.checked_add(i64::from(b - b'0'))?;
107 pos += 1;
108 }
109 if pos == start {
110 return None;
111 }
112 Some((ifc_index_to_zero_based(value).unwrap_or(INVALID_INDEX), pos))
113}