from typing import Optional


def encode_str(buffer: bytearray, position: int,
               value: 'Optional[str]', encoding: str) -> int:
    """
    Encode the string value.

    :param buffer: Buffer to fill
    :param position: Start position
    :param value: Value to encode (can be None or empty string)
    :param encoding: Encoding to use
    :return: Updated position
    """

    if value:
        encoded = value.encode(encoding)
        for byte in encoded:
            buffer[position] = byte
            position += 1

    return position