Patchwork [3,of,5] util: extract pure tolf/tocrlf() functions from eol extension

login
register
mail settings
Submitter Yuya Nishihara
Date March 29, 2017, 2:46 p.m.
Message ID <24dce83e6706dfaad602.1490798799@mimosa>
Download mbox | patch
Permalink /patch/19820/
State Accepted
Headers show

Comments

Yuya Nishihara - March 29, 2017, 2:46 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1490790534 -32400
#      Wed Mar 29 21:28:54 2017 +0900
# Node ID 24dce83e6706dfaad602e9ff0440ac40f6a4cc37
# Parent  0b1a6949c3a05cab2b3a7028501a071d3dfea127
util: extract pure tolf/tocrlf() functions from eol extension

This can be used for EOL conversion of text files.

Patch

diff --git a/hgext/eol.py b/hgext/eol.py
--- a/hgext/eol.py
+++ b/hgext/eol.py
@@ -113,11 +113,6 @@  testedwith = 'ships-with-hg-core'
 
 # 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 @@  def tolf(s, params, ui, **kwargs):
     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 @@  def tocrlf(s, params, ui, **kwargs):
     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."""
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -2197,6 +2197,17 @@  bytecount = unitcountfn(
     (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