MCPcopy Index your code
hub / github.com/RustPython/RustPython / IncrementalNewlineDecoder

Class IncrementalNewlineDecoder

Lib/_pyio.py:1938–2020  ·  view source on GitHub ↗

r"""Codec used when reading a file in universal newlines mode. It wraps another incremental decoder, translating \r\n and \r into \n. It also records the types of newlines encountered. When used with translate=False, it ensures that the newline sequence is returned in one piece.

Source from the content-addressed store, hash-verified

1936
1937
1938class IncrementalNewlineDecoder(codecs.IncrementalDecoder):
1939 r"""Codec used when reading a file in universal newlines mode. It wraps
1940 another incremental decoder, translating \r\n and \r into \n. It also
1941 records the types of newlines encountered. When used with
1942 translate=False, it ensures that the newline sequence is returned in
1943 one piece.
1944 """
1945 def __init__(self, decoder, translate, errors='strict'):
1946 codecs.IncrementalDecoder.__init__(self, errors=errors)
1947 self.translate = translate
1948 self.decoder = decoder
1949 self.seennl = 0
1950 self.pendingcr = False
1951
1952 def decode(self, input, final=False):
1953 # decode input (with the eventual \r from a previous pass)
1954 if self.decoder is None:
1955 output = input
1956 else:
1957 output = self.decoder.decode(input, final=final)
1958 if self.pendingcr and (output or final):
1959 output = "\r" + output
1960 self.pendingcr = False
1961
1962 # retain last \r even when not translating data:
1963 # then readline() is sure to get \r\n in one pass
1964 if output.endswith("\r") and not final:
1965 output = output[:-1]
1966 self.pendingcr = True
1967
1968 # Record which newlines are read
1969 crlf = output.count('\r\n')
1970 cr = output.count('\r') - crlf
1971 lf = output.count('\n') - crlf
1972 self.seennl |= (lf and self._LF) | (cr and self._CR) \
1973 | (crlf and self._CRLF)
1974
1975 if self.translate:
1976 if crlf:
1977 output = output.replace("\r\n", "\n")
1978 if cr:
1979 output = output.replace("\r", "\n")
1980
1981 return output
1982
1983 def getstate(self):
1984 if self.decoder is None:
1985 buf = b""
1986 flag = 0
1987 else:
1988 buf, flag = self.decoder.getstate()
1989 flag <<= 1
1990 if self.pendingcr:
1991 flag |= 1
1992 return buf, flag
1993
1994 def setstate(self, state):
1995 buf, flag = state

Callers 2

_newline_convertFunction · 0.90
_get_decoderMethod · 0.70

Calls

no outgoing calls

Tested by 1

_newline_convertFunction · 0.72