]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Optimize comment parsing
authorAarni Koskela <akx@iki.fi>
Mon, 17 Mar 2025 11:25:05 +0000 (13:25 +0200)
committerAarni Koskela <akx@iki.fi>
Fri, 21 Mar 2025 06:23:57 +0000 (08:23 +0200)
babel/messages/pofile.py

index ff6e23522b93ace7f08d8bbd230a3747db913071..987193e90ded2663073057e00b6750b436015da7 100644 (file)
@@ -271,28 +271,32 @@ class PoFileParser:
 
         self._finish_current_message()
 
-        if line[1:].startswith(':'):
+        prefix = line[:2]
+        if prefix == '#:':
             for location in _extract_locations(line[2:]):
-                pos = location.rfind(':')
-                if pos >= 0:
+                a, colon, b = location.rpartition(':')
+                if colon:
                     try:
-                        lineno = int(location[pos + 1:])
+                        self.locations.append((a, int(b)))
                     except ValueError:
                         continue
-                    self.locations.append((location[:pos], lineno))
-                else:
+                else:  # No line number specified
                     self.locations.append((location, None))
-        elif line[1:].startswith(','):
-            for flag in line[2:].lstrip().split(','):
-                self.flags.append(flag.strip())
-        elif line[1:].startswith('.'):
+            return
+
+        if prefix == '#,':
+            self.flags.extend(flag.strip() for flag in line[2:].lstrip().split(','))
+            return
+
+        if prefix == '#.':
             # These are called auto-comments
             comment = line[2:].strip()
             if comment:  # Just check that we're not adding empty comments
                 self.auto_comments.append(comment)
-        else:
-            # These are called user comments
-            self.user_comments.append(line[1:].strip())
+            return
+
+        # These are called user comments
+        self.user_comments.append(line[1:].strip())
 
     def parse(self, fileobj: IO[AnyStr] | Iterable[AnyStr]) -> None:
         """