Changeset 264715 in webkit
- Timestamp:
- Jul 22, 2020 11:35:42 AM (4 years ago)
- Location:
- trunk/Tools
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Tools/ChangeLog
r264711 r264715 1 2020-07-22 Jonathan Bedard <jbedard@apple.com> 2 3 [webkitcorepy] Add string_utils (Part 1) 4 https://bugs.webkit.org/show_bug.cgi?id=214405 5 6 Reviewed by Dewei Zhu. 7 8 Centralize handling of unicode encoding/decoding along with various tools 9 for printing strings. 10 11 * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: 12 * Scripts/libraries/webkitcorepy/webkitcorepy/string_utils.py: Added. 13 (encode): Encode a string as bytes. 14 (decode): Decode a bytes as a string. 15 (ordinal): Convert a number to 1st, 2nd, 3rd, 4th, ect. 16 (pluralize): Convert a string to it's plurlarized version, if provided number indicates 17 it should be. 18 (join): Join a list of elements in a human-readable form. 19 (out_of): Create a fraction which takes up a constant amount of characters. 20 (elapsed): Describe the amount of time elapsed as a human-readable string. 21 * Scripts/libraries/webkitcorepy/webkitcorepy/tests/string_utils_unittest.py: Added. 22 (StringUtils): Test string_utils. 23 * Scripts/webkitpy/common/unicode_compatibility.py: Replaced with webkitcorepy.string_utils. 24 1 25 2020-07-22 Aakash Jain <aakash_jain@apple.com> 2 26 -
trunk/Tools/Scripts/libraries/webkitcorepy/README.md
r264425 r264715 11 11 version = Version(1, 2, 3) 12 12 ``` 13 14 Unicode stream management across Python 2 and 3 15 ``` 16 from webkitcorepy import BytesIO, StringIO, UnicodeIO, unicode 17 ``` 18 19 Encoding and decoding byte strings and unicode strings 20 ``` 21 from webkitcorepy import string_utils 22 23 string_utils.encode(...) 24 string_utils.decode(...) 25 ``` -
trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py
r264425 r264715 22 22 23 23 from webkitcorepy.version import Version 24 version = Version(0, 0, 1) 24 from webkitcorepy.string_utils import BytesIO, StringIO, UnicodeIO, unicode 25 26 version = Version(0, 0, 2) -
trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/version.py
r264425 r264715 21 21 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 22 23 import collections24 23 import re 25 24 26 from functools import total_ordering25 from webkitcorepy.string_utils import unicode 27 26 28 27 -
trunk/Tools/Scripts/webkitpy/common/unicode_compatibility.py
r254762 r264715 1 # Copyright (C) 2019 Apple Inc. All rights reserved.1 # Copyright (C) 2019-2020 Apple Inc. All rights reserved. 2 2 # 3 3 # Redistribution and use in source and binary forms, with or without … … 21 21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 22 22 23 import io 24 import sys 25 26 27 BytesIO = io.BytesIO 28 if sys.version_info > (3, 0): 29 StringIO = io.StringIO 30 else: 31 from StringIO import StringIO 32 UnicodeIO = io.StringIO 33 34 unicode = str if sys.version_info > (3, 0) else unicode 35 23 from webkitcorepy import UnicodeIO, StringIO, BytesIO, unicode, string_utils 36 24 37 25 def encode_if_necessary(value, encoding='utf-8', errors='strict'): 38 # In Python 3, string types must be encoded 39 if type(value) == unicode: 40 return value.encode(encoding, errors=errors) 41 return value 26 return string_utils.encode(value, encoding=encoding, errors=errors) 42 27 43 28 44 29 def encode_for(value, target_type, **kwargs): 45 if target_type == bytes: 46 return encode_if_necessary(value, **kwargs) 47 48 if sys.version_info < (3, 0) and target_type == str: 49 return encode_if_necessary(value, **kwargs) 50 return value 30 return string_utils.encode(value, target_type=target_type, **kwargs) 51 31 52 32 53 33 def decode_if_necessary(value, encoding='utf-8', errors='strict'): 54 # In Python 2, string types might need to be decoded 55 if type(value) == bytes: 56 return value.decode(encoding, errors=errors) 57 return value 34 return string_utils.decode(value, encoding=encoding, errors=errors) 58 35 59 36 60 37 def decode_for(value, target_type): 61 if value is None: 62 return None 63 if type(value) == target_type: 64 return value 65 if target_type == unicode: 66 return decode_if_necessary(bytes(value)) 67 return value 38 return string_utils.decode(value, target_type=target_type)
Note: See TracChangeset
for help on using the changeset viewer.