MCPcopy
hub / github.com/django/django / _text_chars

Method _text_chars

django/utils/text.py:214–232  ·  view source on GitHub ↗

Truncate a string after a certain number of chars.

(self, length, truncate, text)

Source from the content-addressed store, hash-verified

212 return self._text_chars(length, truncate, text)
213
214 def _text_chars(self, length, truncate, text):
215 """Truncate a string after a certain number of chars."""
216 truncate_len = calculate_truncate_chars_length(length, truncate)
217 s_len = 0
218 end_index = None
219 for i, char in enumerate(text):
220 if unicodedata.combining(char):
221 # Don't consider combining characters
222 # as adding to the string length
223 continue
224 s_len += 1
225 if end_index is None and s_len > truncate_len:
226 end_index = i
227 if s_len > length:
228 # Return the truncated string
229 return add_truncation_text(text[: end_index or 0], truncate)
230
231 # Return the original string since no truncation was necessary
232 return text
233
234 def words(self, num, truncate=None, html=False):
235 """

Callers 1

charsMethod · 0.95

Calls 2

add_truncation_textFunction · 0.85

Tested by

no test coverage detected