Review of domain_to_idna() to support more tests cases
authorfunilrys <redacted>
Sun, 4 Mar 2018 10:17:38 +0000 (11:17 +0100)
committerfunilrys <redacted>
Sun, 4 Mar 2018 10:17:38 +0000 (11:17 +0100)
Please note this patch comes after an issue reported by @FadeMind
which I could reproduce in 1/3 computer.

updateHostsFile.py

index d7c41b65f4a117ef0c52f28df27b94fe067d0da6..33ea85958108c0f7624c0da5b71f17b029b95017 100644 (file)
@@ -1159,26 +1159,47 @@ def domain_to_idna(line):
     """
 
     if not line.startswith('#'):
-        for separator in ['\t', ' ']:
-            comment = ''
-
-            if separator in line:
-                splited_line = line.split(separator)
-                if '#' in splited_line[1]:
-                    index_comment = splited_line[1].find('#')
-
-                    if index_comment > -1:
-                        comment = splited_line[1][index_comment:]
-
-                        splited_line[1] = splited_line[1] \
-                            .split(comment)[0] \
-                            .encode("IDNA").decode("UTF-8") + \
-                            comment
-
-                splited_line[1] = splited_line[1] \
-                    .encode("IDNA") \
-                    .decode("UTF-8")
-                return separator.join(splited_line)
+        tabs = '\t'
+        space = ' '
+
+        tabs_position, space_position = (line.find(tabs), line.find(space))
+
+        if tabs_position > -1 and space_position > -1:
+            if space_position < tabs_position:
+                separator = space
+            else:
+                separator = tabs
+        elif not tabs_position == -1:
+            separator = tabs
+        elif not space_position == -1:
+            separator = space
+        else:
+            separator = ''
+
+        if separator:
+            splited_line = line.split(separator)
+
+            index = 1
+            while index < len(splited_line):
+                if splited_line[index]:
+                    break
+                index += 1
+
+            if '#' in splited_line[index]:
+                index_comment = splited_line[index].find('#')
+
+                if index_comment > -1:
+                    comment = splited_line[index][index_comment:]
+
+                    splited_line[index] = splited_line[index] \
+                        .split(comment)[0] \
+                        .encode("IDNA").decode("UTF-8") + \
+                        comment
+
+            splited_line[index] = splited_line[index] \
+                .encode("IDNA") \
+                .decode("UTF-8")
+            return separator.join(splited_line)
         return line.encode("IDNA").decode("UTF-8")
     return line.encode("UTF-8").decode("UTF-8")
 
git clone https://git.99rst.org/PROJECT