"""
RCSid:
	$Id: p3compat.py,v 1.9 2026/01/04 23:01:19 sjg Exp $

	@(#) Copyright (c) 2013-2025 Simon J. Gerraty

	SPDX-License-Identifier: BSD-2-Clause
      
	Please send copies of changes and bug-fixes to:
	sjg@crufty.net

"""

import sys

if sys.version_info[0] == 2:
    # b'str' and 'str' are equivalent
    # but not u'str'
    def b(s, encoding="latin-1"):
        if isinstance(s,unicode):
            return s.encode(encoding)
        return s

    def s(b, encoding="latin-1", errors="strict"):
        if isinstance(b,unicode):
            return b.decode(encoding, errors)
        return b

else:
    # u'str' and 'str' are equivalent
    # but not b'str'
    def b(s, encoding="latin-1"):
        if isinstance(s,bytes):
            return s
        return s.encode(encoding)

    def s(b, encoding="latin-1", errors="strict"):
        if isinstance(b,str):
            return b
        return b.decode(encoding, errors)

    def byte_array(b, encoding="latin-1", errors="strict"):
        if isinstance(b,str):
            return bytearray(b, encoding, errors)
        return bytearray(b)
