diff options
author | Maxim Kurnikov <maxim.kurnikov@gmail.com> | 2019-02-26 02:22:09 +0300 |
---|---|---|
committer | Maxim Kurnikov <maxim.kurnikov@gmail.com> | 2019-02-26 23:16:40 +0300 |
commit | 9338202167e7b3fa6c2ead9079aba1058066e768 (patch) | |
tree | bd7cbc95784199afc897ad38665d4eded681c712 | |
parent | 3825fabc9ba74f793a43257b244fe299500e0eb0 (diff) | |
download | isort-9338202167e7b3fa6c2ead9079aba1058066e768.tar.gz |
extract infer_line_separator method
-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' |