4
4
import six
5
5
6
6
from mog_commons .collection import distinct
7
+ from mog_commons .types import *
7
8
8
9
__all__ = [
9
10
'is_unicode' ,
@@ -29,6 +30,7 @@ def is_strlike(s):
29
30
return isinstance (s , (six .string_types , bytes ))
30
31
31
32
33
+ @types (s = String )
32
34
def unicode_width (s ):
33
35
if is_unicode (s ):
34
36
return sum (__unicode_width_mapping [east_asian_width (c )] for c in s )
@@ -37,6 +39,7 @@ def unicode_width(s):
37
39
return len (s )
38
40
39
41
42
+ @types (encoding = Option (String ), errors = String )
40
43
def to_unicode (s , encoding = None , errors = 'strict' ):
41
44
"""
42
45
Make unicode string from any value
@@ -58,6 +61,7 @@ def to_unicode(s, encoding=None, errors='strict'):
58
61
return str (s )
59
62
60
63
64
+ @types (encoding = Option (String ), errors = String )
61
65
def to_str (s , encoding = None , errors = 'strict' ):
62
66
"""
63
67
Make str from any value
@@ -77,6 +81,7 @@ def to_str(s, encoding=None, errors='strict'):
77
81
return str (s )
78
82
79
83
84
+ @types (encoding = Option (String ), errors = String )
80
85
def to_bytes (s , encoding = None , errors = 'strict' ):
81
86
"""Convert string to bytes."""
82
87
encoding = encoding or 'utf-8'
@@ -92,11 +97,40 @@ def to_bytes(s, encoding=None, errors='strict'):
92
97
return str (s ).encode (encoding , errors )
93
98
94
99
95
- def edge_just (left , right , width , fillchar = ' ' ):
96
- padding = fillchar * max (1 , width - unicode_width (left + right ))
100
+ @types (left = String , right = String , width = int , fillchar = String , min_padding_length = int )
101
+ def edge_just (left , right , width , fillchar = ' ' , min_padding_length = 1 ):
102
+ """
103
+ :param left:
104
+ :param right:
105
+ :param width:
106
+ :param fillchar:
107
+ :param min_padding_length: minimum padding length
108
+ :return:
109
+ """
110
+ assert unicode_width (fillchar ) == 1 , 'fillchar must be single-width char'
111
+ padding = fillchar * max (min_padding_length , width - unicode_width (left + right ))
97
112
return left + padding + right
98
113
99
114
115
+ @types (s = String , width = int , fillchar = String )
116
+ def unicode_ljust (s , width , fillchar = ' ' ):
117
+ """
118
+ Return Unicode string left-justified in a print-length width.
119
+ Padding is done using the specified fill character (default is a space).
120
+ """
121
+ return edge_just (s , '' , width , fillchar , 0 )
122
+
123
+
124
+ @types (s = String , width = int , fillchar = String )
125
+ def unicode_rjust (s , width , fillchar = ' ' ):
126
+ """
127
+ Return Unicode string right-justified in a print-length width.
128
+ Padding is done using the specified fill character (default is a space).
129
+ """
130
+ return edge_just ('' , s , width , fillchar , 0 )
131
+
132
+
133
+ @types (s = String , width = int )
100
134
def unicode_left (s , width ):
101
135
"""Cut unicode string from left to fit a given width."""
102
136
i = 0
@@ -109,6 +143,7 @@ def unicode_left(s, width):
109
143
return s [:i ]
110
144
111
145
146
+ @types (s = String , width = int )
112
147
def unicode_right (s , width ):
113
148
"""Cut unicode string from right to fit a given width."""
114
149
i = len (s )
@@ -121,6 +156,7 @@ def unicode_right(s, width):
121
156
return s [i :]
122
157
123
158
159
+ @types (encoding_list = (String , ListOf (String )))
124
160
def unicode_decode (data , encoding_list ):
125
161
"""
126
162
Decode string data with one or more encodings, trying sequentially
0 commit comments