Changeset 264715 in webkit


Ignore:
Timestamp:
Jul 22, 2020 11:35:42 AM (4 years ago)
Author:
Jonathan Bedard
Message:

[webkitcorepy] Add string_utils (Part 1)
https://bugs.webkit.org/show_bug.cgi?id=214405

Reviewed by Dewei Zhu.

Centralize handling of unicode encoding/decoding along with various tools
for printing strings.

  • Scripts/libraries/webkitcorepy/webkitcorepy/init.py:
  • Scripts/libraries/webkitcorepy/webkitcorepy/string_utils.py: Added.

(encode): Encode a string as bytes.
(decode): Decode a bytes as a string.
(ordinal): Convert a number to 1st, 2nd, 3rd, 4th, ect.
(pluralize): Convert a string to it's plurlarized version, if provided number indicates
it should be.
(join): Join a list of elements in a human-readable form.
(out_of): Create a fraction which takes up a constant amount of characters.
(elapsed): Describe the amount of time elapsed as a human-readable string.

  • Scripts/libraries/webkitcorepy/webkitcorepy/tests/string_utils_unittest.py: Added.

(StringUtils): Test string_utils.

  • Scripts/webkitpy/common/unicode_compatibility.py: Replaced with webkitcorepy.string_utils.
Location:
trunk/Tools
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Tools/ChangeLog

    r264711 r264715  
     12020-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
    1252020-07-22  Aakash Jain  <aakash_jain@apple.com>
    226
  • trunk/Tools/Scripts/libraries/webkitcorepy/README.md

    r264425 r264715  
    1111version = Version(1, 2, 3)
    1212```
     13
     14Unicode stream management across Python 2 and 3
     15```
     16from webkitcorepy import BytesIO, StringIO, UnicodeIO, unicode
     17```
     18
     19Encoding and decoding byte strings and unicode strings
     20```
     21from webkitcorepy import string_utils
     22
     23string_utils.encode(...)
     24string_utils.decode(...)
     25```
  • trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py

    r264425 r264715  
    2222
    2323from webkitcorepy.version import Version
    24 version = Version(0, 0, 1)
     24from webkitcorepy.string_utils import BytesIO, StringIO, UnicodeIO, unicode
     25
     26version = Version(0, 0, 2)
  • trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/version.py

    r264425 r264715  
    2121# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2222
    23 import collections
    2423import re
    2524
    26 from functools import total_ordering
     25from webkitcorepy.string_utils import unicode
    2726
    2827
  • 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.
    22#
    33# Redistribution and use in source and binary forms, with or without
     
    2121# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2222
    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 
     23from webkitcorepy import UnicodeIO, StringIO, BytesIO, unicode, string_utils
    3624
    3725def 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)
    4227
    4328
    4429def 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)
    5131
    5232
    5333def 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)
    5835
    5936
    6037def 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.