5.1.1.3.1. RawTextDocument¶
This module provides an enhancement over a standard array of characters, so called Text Buffer in this documentation. It features an API to manipulate the text buffer using line indexing and slicing.
Definition of therms used in this document:
- Text Buffer
- A text buffer is an object that provides the characters and implements the method __getitem__ to index or slice its content and the method __len__ to get the number of characters.
- Text Document
- A text document is a text buffer adapted by a
RawTextDocumentclass featuring the line indexing and slicing.- Flat Slice
- A flat slice represents an interval of character indexes. Only the sliced object know how to interpret the slice.
- Line Slice
- A line slice represents an interval of line indexes. The only difference with a flat slice is semantic.
- Text Chunk or View
- A text chunk represents a sliced text buffer.
- Line
- A line represents either a slice, a view or the corresponding string of characters.
-
class
CodeReview.Diff.RawTextDocument.RawTextDocument(text_buffer)[source]¶ Bases:
CodeReview.Diff.RawTextDocument.RawTextDocumentAbcThis class implements a Text Document.
The parameter text_buffer specifies the text buffer, cf.
RawTextDocumentAbcfor explanations.-
_append_sentinel(line_start_locations, line_separators, stop_location)[source]¶ Append a sentinel to the lists: line_start_locations and line_separators.
The parameter stop_location corresponds to the stop index of the text chunk.
This method ensures the list line_separators end by stop_location and it appends an empty string to line_separators if it is not true.
-
_split_lines(text_buffer)[source]¶ Split the lines and return the 2-tuple (line_start_locations, line_separators), cf.
RawTextDocumentAbcfor their definitions.
-
light_view(slice_)[source]¶ Return a
RawTextDocumentLightViewinstance for the corresponding slice.
-
view(slice_)[source]¶ Return a
RawTextDocumentViewinstance for the corresponding slice.
-
-
class
CodeReview.Diff.RawTextDocument.RawTextDocumentAbc(text_buffer, flat_slice, line_start_locations, line_separators)[source]¶ Bases:
objectThis class implements the basic function for a Text Chunk.
To get the number of characters of the chunk use the function
len()and to test if the slice is empty use a Boolean evaluation of the instance.To get the text buffer use the function
str().To get a view or light view if the light view mode is set, we can use:
text_document[slice]
The light view mode is set using the boolean attribute
light_view_mode.The parameter text_buffer specifies the text buffer. It must implement the method __getitem__ to index and slice the characters.
The parameter flat_slice specifies the flat slice corresponding to the text chunk.
The list line_start_locations contains the position of the new lines in the text chunk and the list line_separators contains the corresponding new line separators. The standard separators (
\r\n,\r,\n) are supported. The list line_start_locations ends by a sentinel that corresponds to the number of characters in the text chunk and the list line_separators by an empty string. This sentinel corresponds to a virtual line at the end of the text buffer.-
light_view(slice_)[source]¶ Return a
RawTextDocumentLightViewinstance for the corresponding slice.Not implemented.
-
light_view_mode= False¶
-
line_iterator(new_line_separator=True)[source]¶ Return an iterator on the string lines. If new_line_separator is set then the line separator is included.
-
line_slice_iterator(new_line_separator=True)[source]¶ Return an iterator on the line’s flat slices. If new_line_separator is set then the line separator is included.
-
lines(new_line_separator=True)[source]¶ Return the list of string lines. If new_line_separator is set then the line separator is included.
-
view(slice_)[source]¶ Return a
RawTextDocumentViewinstance for the corresponding slice.Not implemented.
-
-
class
CodeReview.Diff.RawTextDocument.RawTextDocumentLightView(raw_text_document, flat_slice)[source]¶ Bases:
objectThis class implements a light view on a Text Document.
A light view doesn’t feature line indexing and slicing. The memory footprint is lighter than a standard view and thus well suited for many small chunks.
To get the number of characters of the chunk use the function
len()and to test if the slice is empty use a Boolean evaluation of the instance.To get the text buffer use the function
unicode().To get a light view, we can use:
text_document[slice]
Public attributes:
The parameter raw_text_document specifies the text document.
The parameter flat_slice specifies the slice corresponding to the view.
-
_raw_text_document¶
-
flat_slice¶
-
-
class
CodeReview.Diff.RawTextDocument.RawTextDocumentView(raw_text_document, slice_, *args)[source]¶ Bases:
CodeReview.Diff.RawTextDocument.RawTextDocumentAbcThis class implements a view on a Text Document.
The parameter raw_text_document specifies the text document.
The parameter slice_ specifies the slice corresponding to the view, it can be either a flat ro a line slice.
The remaining parameters are those of the
RawTextDocumentAbc.__init__()method.Public attributes:
sliceis a copy of the slice passed as argument.-
to_document_flat_slice(slice_)[source]¶ Convert a slice into the view to a flat slice in the document space and return it.
-