Show More
@@ -20,6 +20,23 from . import ( | |||
|
20 | 20 | |
|
21 | 21 | from .pure import charencode as charencodepure |
|
22 | 22 | |
|
23 | if not globals(): # hide this from non-pytype users | |
|
24 | from typing import ( | |
|
25 | Any, | |
|
26 | Callable, | |
|
27 | List, | |
|
28 | Text, | |
|
29 | Type, | |
|
30 | TypeVar, | |
|
31 | Union, | |
|
32 | ) | |
|
33 | ||
|
34 | # keep pyflakes happy | |
|
35 | for t in (Any, Callable, List, Text, Type, Union): | |
|
36 | assert t | |
|
37 | ||
|
38 | _Tlocalstr = TypeVar('_Tlocalstr', bound=localstr) | |
|
39 | ||
|
23 | 40 | charencode = policy.importmod(r'charencode') |
|
24 | 41 | |
|
25 | 42 | isasciistr = charencode.isasciistr |
@@ -45,6 +62,7 assert all(i.startswith((b"\xe2", b"\xef | |||
|
45 | 62 | |
|
46 | 63 | |
|
47 | 64 | def hfsignoreclean(s): |
|
65 | # type: (bytes) -> bytes | |
|
48 | 66 | """Remove codepoints ignored by HFS+ from s. |
|
49 | 67 | |
|
50 | 68 | >>> hfsignoreclean(u'.h\u200cg'.encode('utf-8')) |
@@ -99,6 +117,7 class localstr(bytes): | |||
|
99 | 117 | round-tripped to the local encoding and back''' |
|
100 | 118 | |
|
101 | 119 | def __new__(cls, u, l): |
|
120 | # type: (Type[_Tlocalstr], Text, bytes) -> _Tlocalstr | |
|
102 | 121 | s = bytes.__new__(cls, l) |
|
103 | 122 | s._utf8 = u |
|
104 | 123 | return s |
@@ -119,6 +138,7 class safelocalstr(bytes): | |||
|
119 | 138 | |
|
120 | 139 | |
|
121 | 140 | def tolocal(s): |
|
141 | # type: (Text) -> bytes | |
|
122 | 142 | """ |
|
123 | 143 | Convert a string from internal UTF-8 to local encoding |
|
124 | 144 | |
@@ -185,6 +205,7 def tolocal(s): | |||
|
185 | 205 | |
|
186 | 206 | |
|
187 | 207 | def fromlocal(s): |
|
208 | # type: (bytes) -> Text | |
|
188 | 209 | """ |
|
189 | 210 | Convert a string from the local character encoding to UTF-8 |
|
190 | 211 | |
@@ -214,16 +235,19 def fromlocal(s): | |||
|
214 | 235 | |
|
215 | 236 | |
|
216 | 237 | def unitolocal(u): |
|
238 | # type: (Text) -> bytes | |
|
217 | 239 | """Convert a unicode string to a byte string of local encoding""" |
|
218 | 240 | return tolocal(u.encode('utf-8')) |
|
219 | 241 | |
|
220 | 242 | |
|
221 | 243 | def unifromlocal(s): |
|
244 | # type: (bytes) -> Text | |
|
222 | 245 | """Convert a byte string of local encoding to a unicode string""" |
|
223 | 246 | return fromlocal(s).decode('utf-8') |
|
224 | 247 | |
|
225 | 248 | |
|
226 | 249 | def unimethod(bytesfunc): |
|
250 | # type: (Callable[[Any], bytes]) -> Callable[[Any], Text] | |
|
227 | 251 | """Create a proxy method that forwards __unicode__() and __str__() of |
|
228 | 252 | Python 3 to __bytes__()""" |
|
229 | 253 | |
@@ -281,11 +305,13 else: | |||
|
281 | 305 | |
|
282 | 306 | |
|
283 | 307 | def colwidth(s): |
|
308 | # type: (bytes) -> int | |
|
284 | 309 | b"Find the column width of a string for display in the local encoding" |
|
285 | 310 | return ucolwidth(s.decode(_sysstr(encoding), r'replace')) |
|
286 | 311 | |
|
287 | 312 | |
|
288 | 313 | def ucolwidth(d): |
|
314 | # type: (Text) -> int | |
|
289 | 315 | b"Find the column width of a Unicode string for display" |
|
290 | 316 | eaw = getattr(unicodedata, 'east_asian_width', None) |
|
291 | 317 | if eaw is not None: |
@@ -294,6 +320,7 def ucolwidth(d): | |||
|
294 | 320 | |
|
295 | 321 | |
|
296 | 322 | def getcols(s, start, c): |
|
323 | # type: (bytes, int, int) -> bytes | |
|
297 | 324 | '''Use colwidth to find a c-column substring of s starting at byte |
|
298 | 325 | index start''' |
|
299 | 326 | for x in pycompat.xrange(start + c, len(s)): |
@@ -303,6 +330,7 def getcols(s, start, c): | |||
|
303 | 330 | |
|
304 | 331 | |
|
305 | 332 | def trim(s, width, ellipsis=b'', leftside=False): |
|
333 | # type: (bytes, int, bytes, bool) -> bytes | |
|
306 | 334 | """Trim string 's' to at most 'width' columns (including 'ellipsis'). |
|
307 | 335 | |
|
308 | 336 | If 'leftside' is True, left side of string 's' is trimmed. |
@@ -400,6 +428,7 def trim(s, width, ellipsis=b'', leftsid | |||
|
400 | 428 | |
|
401 | 429 | |
|
402 | 430 | def lower(s): |
|
431 | # type: (bytes) -> bytes | |
|
403 | 432 | b"best-effort encoding-aware case-folding of local string s" |
|
404 | 433 | try: |
|
405 | 434 | return asciilower(s) |
@@ -422,6 +451,7 def lower(s): | |||
|
422 | 451 | |
|
423 | 452 | |
|
424 | 453 | def upper(s): |
|
454 | # type: (bytes) -> bytes | |
|
425 | 455 | b"best-effort encoding-aware case-folding of local string s" |
|
426 | 456 | try: |
|
427 | 457 | return asciiupper(s) |
@@ -430,6 +460,7 def upper(s): | |||
|
430 | 460 | |
|
431 | 461 | |
|
432 | 462 | def upperfallback(s): |
|
463 | # type: (Any) -> Any | |
|
433 | 464 | try: |
|
434 | 465 | if isinstance(s, localstr): |
|
435 | 466 | u = s._utf8.decode("utf-8") |
@@ -464,6 +495,7 class normcasespecs(object): | |||
|
464 | 495 | |
|
465 | 496 | |
|
466 | 497 | def jsonescape(s, paranoid=False): |
|
498 | # type: (Any, Any) -> Any | |
|
467 | 499 | '''returns a string suitable for JSON |
|
468 | 500 | |
|
469 | 501 | JSON is problematic for us because it doesn't support non-Unicode |
@@ -527,6 +559,7 else: | |||
|
527 | 559 | |
|
528 | 560 | |
|
529 | 561 | def getutf8char(s, pos): |
|
562 | # type: (Any, Any) -> Any | |
|
530 | 563 | '''get the next full utf-8 character in the given string, starting at pos |
|
531 | 564 | |
|
532 | 565 | Raises a UnicodeError if the given location does not start a valid |
@@ -545,6 +578,7 def getutf8char(s, pos): | |||
|
545 | 578 | |
|
546 | 579 | |
|
547 | 580 | def toutf8b(s): |
|
581 | # type: (Any) -> Any | |
|
548 | 582 | '''convert a local, possibly-binary string into UTF-8b |
|
549 | 583 | |
|
550 | 584 | This is intended as a generic method to preserve data when working |
@@ -613,6 +647,7 def toutf8b(s): | |||
|
613 | 647 | |
|
614 | 648 | |
|
615 | 649 | def fromutf8b(s): |
|
650 | # type: (Text) -> bytes | |
|
616 | 651 | '''Given a UTF-8b string, return a local, possibly-binary string. |
|
617 | 652 | |
|
618 | 653 | return the original binary string. This |
@@ -24,4 +24,5 run pyflakes on all tracked files ending | |||
|
24 | 24 | contrib/perf.py:*: undefined name 'xrange' (glob) (?) |
|
25 | 25 | mercurial/hgweb/server.py:*: undefined name 'reload' (glob) (?) |
|
26 | 26 | mercurial/util.py:*: undefined name 'file' (glob) (?) |
|
27 | mercurial/encoding.py:*: undefined name 'localstr' (glob) (?) | |
|
27 | 28 |
General Comments 0
You need to be logged in to leave comments.
Login now