Skip to content

Commit

Permalink
sqf: Fix sqfc byte offset (#771)
Browse files Browse the repository at this point in the history
* sqf: Fix sqfc byte offset

* for clippy
  • Loading branch information
PabstMirror committed Sep 10, 2024
1 parent fe78d1e commit a53c917
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 3 additions & 5 deletions libs/sqf/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ impl Statements {
ctx.add_constant(Constant::String(self.source.clone()))?,
))
} else {
let start = processed
.mapping(self.span.start)
.expect("first statement not in mapping");
let offset = start.processed_start().offset() as u32;
let offset = processed.get_byte_offset(self.span.start);
let source = processed.extract(self.span.clone());
let length = if self.content.is_empty() {
0
Expand All @@ -102,8 +99,9 @@ pub fn location_to_source(processed: &Processed, location: &Range<usize>) -> Sou
let map = processed.mapping(location.start).expect(
"location not in mapping, this should not happen as the location is from the processed file",
).original();
let offset = processed.get_byte_offset(location.start);
SourceInfo {
offset: location.start as u32,
offset,
file_index: processed
.sources()
.iter()
Expand Down
8 changes: 8 additions & 0 deletions libs/workspace/src/reporting/processed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ impl Processed {
self.mappings(offset).last().copied()
}

#[must_use]
#[allow(clippy::cast_possible_truncation)]
/// Get offset as number of raw bytes into the output string
pub fn get_byte_offset(&self, offset: usize) -> u32 {
let ret: usize = self.output.chars().take(offset).map(char::len_utf8).sum();
ret as u32
}

#[must_use]
/// Returns the warnings
pub fn warnings(&self) -> &[Arc<dyn Code>] {
Expand Down

0 comments on commit a53c917

Please sign in to comment.