changeset 31776:fe9b33bcec6a

util: extract pure tolf/tocrlf() functions from eol extension This can be used for EOL conversion of text files.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 29 Mar 2017 21:28:54 +0900
parents 8181f378b073
children 6a5b69b0abec
files hgext/eol.py mercurial/util.py
diffstat 2 files changed, 13 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/hgext/eol.py	Wed Mar 29 21:23:28 2017 +0900
+++ b/hgext/eol.py	Wed Mar 29 21:28:54 2017 +0900
@@ -113,11 +113,6 @@
 
 # Matches a lone LF, i.e., one that is not part of CRLF.
 singlelf = re.compile('(^|[^\r])\n')
-# Matches a single EOL which can either be a CRLF where repeated CR
-# are removed or a LF. We do not care about old Macintosh files, so a
-# stray CR is an error.
-eolre = re.compile('\r*\n')
-
 
 def inconsistenteol(data):
     return '\r\n' in data and singlelf.search(data)
@@ -131,7 +126,7 @@
     if (ui.configbool('eol', 'fix-trailing-newline', False)
         and s and s[-1] != '\n'):
         s = s + '\n'
-    return eolre.sub('\n', s)
+    return util.tolf(s)
 
 def tocrlf(s, params, ui, **kwargs):
     """Filter to convert to CRLF EOLs."""
@@ -142,7 +137,7 @@
     if (ui.configbool('eol', 'fix-trailing-newline', False)
         and s and s[-1] != '\n'):
         s = s + '\n'
-    return eolre.sub('\r\n', s)
+    return util.tocrlf(s)
 
 def isbinary(s, params):
     """Filter to do nothing with the file."""
--- a/mercurial/util.py	Wed Mar 29 21:23:28 2017 +0900
+++ b/mercurial/util.py	Wed Mar 29 21:28:54 2017 +0900
@@ -2200,6 +2200,17 @@
     (1, 1, _('%.0f bytes')),
     )
 
+# Matches a single EOL which can either be a CRLF where repeated CR
+# are removed or a LF. We do not care about old Macintosh files, so a
+# stray CR is an error.
+_eolre = remod.compile(br'\r*\n')
+
+def tolf(s):
+    return _eolre.sub('\n', s)
+
+def tocrlf(s):
+    return _eolre.sub('\r\n', s)
+
 def escapestr(s):
     # call underlying function of s.encode('string_escape') directly for
     # Python 3 compatibility