Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11cade26 authored by Martin Geisler's avatar Martin Geisler Committed by Automerger Merge Worker
Browse files

Merge "pdl: fix computation of `SourceLocation` line numbers" am: c1a968aa...

Merge "pdl: fix computation of `SourceLocation` line numbers" am: c1a968aa am: 71914c35 am: b8ceff42 am: b2f472a0 am: f1b1178e

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Bluetooth/+/2053925



Change-Id: If19bd12710aa3bcf4e79ec4b24bd619bc593521f
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 1fc459ed f1b1178e
Loading
Loading
Loading
Loading
+14 −5
Original line number Diff line number Diff line
@@ -8,11 +8,9 @@ package {
    default_applicable_licenses: ["system_bt_license"],
}

rust_binary_host {
    name: "pdl",
    srcs: [
        "src/main.rs",
    ],
rust_defaults {
    name: "pdl_defaults",
    srcs: ["src/main.rs"],
    rustlibs: [
        "libpest",
        "libserde",
@@ -24,3 +22,14 @@ rust_binary_host {
        "libpest_derive",
    ],
}

rust_binary_host {
    name: "pdl",
    defaults: ["pdl_defaults"],
}

rust_test_host {
    name: "pdl_inline_tests",
    defaults: ["pdl_defaults"],
    test_suites: ["general-tests"],
}
+49 −3
Original line number Diff line number Diff line
@@ -14,8 +14,11 @@ pub type SourceDatabase = files::SimpleFiles<String, String>;

#[derive(Debug, Copy, Clone, Serialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct SourceLocation {
    /// Byte offset into the file (counted from zero).
    pub offset: usize,
    /// Line number (counted from zero).
    pub line: usize,
    /// Column number (counted from zero)
    pub column: usize,
}

@@ -176,13 +179,20 @@ pub trait Named<'d> {
}

impl SourceLocation {
    /// Construct a new source location.
    ///
    /// The `line_starts` indicates the byte offsets where new lines
    /// start in the file. The first element should thus be `0` since
    /// every file has at least one line starting at offset `0`.
    pub fn new(offset: usize, line_starts: &[usize]) -> SourceLocation {
        let mut loc = SourceLocation { offset, line: 0, column: offset };
        for (line, start) in line_starts.iter().enumerate() {
            if *start <= offset {
                return SourceLocation { offset, line, column: offset - start };
            if *start > offset {
                break;
            }
            loc = SourceLocation { offset, line, column: offset - start };
        }
        unreachable!()
        loc
    }
}

@@ -299,3 +309,39 @@ impl<'d> Named<'d> for Decl {
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn source_location_new() {
        let line_starts = &[0, 20, 80, 120, 150];
        assert_eq!(
            SourceLocation::new(0, line_starts),
            SourceLocation { offset: 0, line: 0, column: 0 }
        );
        assert_eq!(
            SourceLocation::new(10, line_starts),
            SourceLocation { offset: 10, line: 0, column: 10 }
        );
        assert_eq!(
            SourceLocation::new(50, line_starts),
            SourceLocation { offset: 50, line: 1, column: 30 }
        );
        assert_eq!(
            SourceLocation::new(100, line_starts),
            SourceLocation { offset: 100, line: 2, column: 20 }
        );
        assert_eq!(
            SourceLocation::new(1000, line_starts),
            SourceLocation { offset: 1000, line: 4, column: 850 }
        );
    }

    #[test]
    fn source_location_new_no_crash_with_empty_line_starts() {
        let loc = SourceLocation::new(100, &[]);
        assert_eq!(loc, SourceLocation { offset: 100, line: 0, column: 100 });
    }
}