@@ -22,6 +22,66 @@ class in the Apache Commons Lang package. Provides `None` safe methods to perfor
22
22
23
23
EMPTY : str = ""
24
24
25
+ @classmethod
26
+ def abbreviate (
27
+ cls ,
28
+ char_sequence : Optional [str ],
29
+ abbrev_marker : Optional [str ] = "..." ,
30
+ offset : int = 0 ,
31
+ max_width : int = - 1 ,
32
+ ) -> Optional [str ]:
33
+ if cls .is_not_empty (char_sequence ) and cls .EMPTY == abbrev_marker and max_width > 0 :
34
+ return typing .cast (str , char_sequence )[:max_width ]
35
+
36
+ if cls .is_any_empty (char_sequence , abbrev_marker ):
37
+ return char_sequence
38
+
39
+ char_sequence = typing .cast (str , abbrev_marker )
40
+ abbrev_marker = typing .cast (str , abbrev_marker )
41
+ abbrev_marker_length = len (abbrev_marker )
42
+ min_abbrev_width = abbrev_marker_length + 1
43
+ min_abbrev_width_offset = abbrev_marker_length + abbrev_marker_length + 1
44
+
45
+ if max_width == - 1 :
46
+ max_width = len (char_sequence ) - abbrev_marker_length
47
+
48
+ if max_width < min_abbrev_width :
49
+ raise ValueError (f"Minimum abbreviation width is { min_abbrev_width } " )
50
+
51
+ str_len = len (char_sequence )
52
+ if str_len <= max_width :
53
+ return char_sequence
54
+
55
+ offset = min (offset , str_len )
56
+
57
+ if str_len - offset < max_width - abbrev_marker_length :
58
+ offset = str_len - (max_width - abbrev_marker_length )
59
+
60
+ if offset <= abbrev_marker_length + 1 :
61
+ return char_sequence [: max_width - abbrev_marker_length ] + abbrev_marker
62
+
63
+ if max_width < min_abbrev_width_offset :
64
+ raise ValueError (f"Minimum abbreviation width with offset is { min_abbrev_width_offset } " )
65
+ return char_sequence [offset : offset + max_width - abbrev_marker_length ] + abbrev_marker
66
+
67
+ @classmethod
68
+ def abbreviate_middle (
69
+ cls , char_sequence : Optional [str ], middle : Optional [str ] = "..." , length : int = 5
70
+ ) -> Optional [str ]:
71
+ if (
72
+ not cls .is_any_empty (char_sequence , middle )
73
+ and len (typing .cast (str , char_sequence )) > length >= len (typing .cast (str , middle )) + 2
74
+ ):
75
+ middle = typing .cast (str , middle )
76
+ char_sequence = typing .cast (str , char_sequence )
77
+
78
+ target_string = length - len (middle )
79
+ start_offset = target_string // 2 + target_string % 2
80
+ end_offset = len (char_sequence ) - target_string // 2
81
+ return char_sequence [:start_offset ] + middle + char_sequence [end_offset :]
82
+
83
+ return char_sequence
84
+
25
85
@classmethod
26
86
def contains (cls , char_sequence : Optional [str ], search_string : Optional [str ]) -> bool :
27
87
if char_sequence is not None and search_string is not None :
0 commit comments