2
0
Эх сурвалжийг харах

Fix a few bugs in and test `AmlName::from_str`

Isaac Woods 5 жил өмнө
parent
commit
4698acdf6d

+ 40 - 4
aml_parser/src/name_object.rs

@@ -25,6 +25,10 @@ impl AmlName {
     /// Convert a string representation of an AML name into an `AmlName`. Returns `None` if the
     /// passed string is not a valid AML path.
     pub fn from_str(mut string: &str) -> Option<AmlName> {
+        if string.len() == 0 {
+            return None;
+        }
+
         let mut components = Vec::new();
 
         // If it starts with a \, make it an absolute name
@@ -33,10 +37,17 @@ impl AmlName {
             string = &string[1..];
         }
 
-        // Divide the rest of it into segments, and parse those
-        for part in string.split('.') {
-            // TODO: handle prefix chars
-            components.push(NameComponent::Segment(NameSeg::from_str(part)?));
+        if string.len() > 0 {
+            // Divide the rest of it into segments, and parse those
+            for mut part in string.split('.') {
+                // Handle prefix chars
+                while part.starts_with('^') {
+                    components.push(NameComponent::Prefix);
+                    part = &part[1..];
+                }
+
+                components.push(NameComponent::Segment(NameSeg::from_str(part)?));
+            }
         }
 
         Some(AmlName(components))
@@ -287,4 +298,29 @@ mod tests {
             &[]
         );
     }
+
+    #[test]
+    fn test_aml_name_from_str() {
+        assert_eq!(AmlName::from_str(""), None);
+        assert_eq!(AmlName::from_str("\\"), Some(AmlName::root()));
+        assert_eq!(
+            AmlName::from_str("\\_SB.PCI0"),
+            Some(AmlName(alloc::vec![
+                NameComponent::Root,
+                NameComponent::Segment(NameSeg([b'_', b'S', b'B', b'_'])),
+                NameComponent::Segment(NameSeg([b'P', b'C', b'I', b'0']))
+            ]))
+        );
+        assert_eq!(
+            AmlName::from_str("\\_SB.^^^PCI0"),
+            Some(AmlName(alloc::vec![
+                NameComponent::Root,
+                NameComponent::Segment(NameSeg([b'_', b'S', b'B', b'_'])),
+                NameComponent::Prefix,
+                NameComponent::Prefix,
+                NameComponent::Prefix,
+                NameComponent::Segment(NameSeg([b'P', b'C', b'I', b'0']))
+            ]))
+        );
+    }
 }