diff options
-rw-r--r-- | isort/isort.py | 7 | ||||
-rw-r--r-- | isort/utils.py | 9 |
2 files changed, 10 insertions, 6 deletions
diff --git a/isort/isort.py b/isort/isort.py index 5635133b..2b5a4a13 100644 --- a/isort/isort.py +++ b/isort/isort.py @@ -111,12 +111,7 @@ class SortImports(object): if self.config['line_ending']: self.line_separator = self.config['line_ending'] else: - if '\r\n' in file_contents: - self.line_separator = '\r\n' - elif '\r' in file_contents: - self.line_separator = '\r' - else: - self.line_separator = '\n' + self.line_separator = utils.infer_line_separator(file_contents) self.in_lines = file_contents.split(self.line_separator) self.original_length = len(self.in_lines) diff --git a/isort/utils.py b/isort/utils.py index 1f1b5143..0f6f57bf 100644 --- a/isort/utils.py +++ b/isort/utils.py @@ -52,3 +52,12 @@ def difference(a: Iterable[Any], b: Container[Any]) -> List[Any]: if item not in b: d.append(item) return d + + +def infer_line_separator(file_contents: str) -> str: + if '\r\n' in file_contents: + return '\r\n' + elif '\r' in file_contents: + return '\r' + else: + return '\n' |