1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
#!/usr/bin/env python
"""
String Utils for Python 3
"""
import os
import sys
if sys.version_info[0] != 3:
raise ImportError(" Python version 3 required")
EPICS_STR_ENCODING = os.environ.get('PYTHONIOENCODING', 'utf-8')
NULLCHAR_2 = '\x00'
NULLCHAR = b'\x00'
def s2b(st1):
'string to byte conversion'
if isinstance(st1, bytes):
return st1
return bytes(st1, EPICS_STR_ENCODING)
def b2s(st1):
'byte to string conversion'
if isinstance(st1, str):
return st1
elif isinstance(st1, bytes):
return str(st1, EPICS_STR_ENCODING)
else:
return str(st1)
STR2BYTES, BYTES2STR = s2b, b2s
def strjoin(sep, seq):
"join string sequence with a separator"
if isinstance(sep, bytes):
sep = BYTES2STR(sep)
if len(seq) == 0:
seq = ''
elif isinstance(seq[0], bytes):
tmp =[]
for i in seq:
if i == NULLCHAR:
break
tmp.append(BYTES2STR(i))
seq = tmp
return sep.join(seq)
def is_string(s):
return isinstance(s, str)
def is_string_or_bytes(s):
return isinstance(s, str) or isinstance(s, bytes)
def ascii_string(s):
return bytes(str(s), EPICS_STR_ENCODING)
|