#!/usr/bin/python3
# ===================================================================
# test encode/decode on strings
# -------------------------------------------------------------------
#
# ===================================================================

import sys

# -------------------------------------------------------------------
# am I running Python3?
# -------------------------------------------------------------------
def RunningPython3():
    ##print(sys.version_info)
    if sys.version_info[0] == 3:
        return True
    return False

# -------------------------------------------------------------------
# prompt the user for input
# -------------------------------------------------------------------
def GetUserInput(prompt,py3):
    if py3:
        return input(prompt)
    else:
        return raw_input(prompt)

# -------------------------------------------------------------------
# pause script
# -------------------------------------------------------------------
def Pause(py3):
    print('')
    GetUserInput('Press enter to continue ',py3)

# -------------------------------------------------------------------
# utf8 byte count
# (bytes class (objects) does not have an encode attribute)
# -------------------------------------------------------------------
def UTF8Len(s):
    try:
        return len(s.encode('utf-8'))
    except:
        pass
    return len(s)

# -------------------------------------------------------------------
# ascii byte count
# (bytes class (object) does not have an encode attribute)
# -------------------------------------------------------------------
def AsciiLen(s):
    try:
        return len(s.encode('ascii'))
    except:
        pass
    return len(s)

# -------------------------------------------------------------------
# display string object information
# -------------------------------------------------------------------
def Display(x,title=None):
    print('')
    if title is not None: print(title)
    print('Type       : {}'.format(type(x)))
    print('Length     : {}'.format(len(x)))
    print('UTF-8 Bytes: {}'.format(UTF8Len(x)))
    print('ASCII Bytes: {}'.format(AsciiLen(x)))
    print('Obj Size   : {}'.format(sys.getsizeof(x)))
    print('Print      : {}'.format(x))


# ===================================================================
# main
# ===================================================================

py3 = RunningPython3()

# ---- test literal string

##s1 = 'abc'

s1 = 'a' + chr(1200) + 'c'

s1 = 'a' + '\u265E' + 'c'      # string with utf-8 character

Display(s1,'---- leteral string not encoded ----')

s2 = s1.encode()

Display(s2,'---- leteral string encoded ----')

s3 = s2.decode()

Display(s3,'---- leteral string decoded ----')

# ---- user input string

while True:

    # ---- test a string from the user

    print()
    m1 = GetUserInput('Enter a string ',py3)

    sm1 = m1.strip()           # remove white space

    if sm1 == '':
        break

    Display(m1,'---- input string not encoded ----')

    m2 = m1.encode()

    Display(m2,'---- input string encoded ----')

    m3 = m2.decode()

    Display(m3,'---- input string decoded ----')