Example #1
0
    def __separate_by_first_occurrence(string,
                                       delimiters,
                                       strip_delim=False,
                                       return_second_part_nonempty=False):
        """
        Separates a string by the first of all given delimiters. Any whitespace
        characters will be stripped away from the parts.

        :param string:                      The string to separate.
        :param delimiters:                  The delimiters.
        :param strip_delim:                 Strips the delimiter from the
                                            result if true.
        :param return_second_part_nonempty: If no delimiter is found and this
                                            is true the contents of the string
                                            will be returned in the second part
                                            of the tuple instead of the first
                                            one.
        :return:                            (first_part, second_part)
        """
        temp_string = string.replace('\\\\', 'oo')
        i = temp_string.find('\\')
        while i != -1:
            temp_string = temp_string[:i] + 'oo' + temp_string[i+2:]
            i = temp_string.find('\\', i+2)

        delim_pos = len(string)
        used_delim = ''
        for delim in delimiters:
            pos = temp_string.find(delim)
            if 0 <= pos < delim_pos:
                delim_pos = pos
                used_delim = delim

        if return_second_part_nonempty and delim_pos == len(string):
            return '', string.strip(' \n')

        first_part = string[:delim_pos]
        second_part = string[delim_pos + (
            len(used_delim) if strip_delim else 0):]

        if not position_is_escaped(second_part, len(second_part) - 1):
            first_part = unescaped_rstrip(first_part)
            second_part = unescaped_rstrip(second_part)

        return (first_part.lstrip().rstrip('\n'),
                second_part.lstrip().rstrip('\n'))
Example #2
0
    def __separate_by_first_occurrence(string,
                                       delimiters,
                                       strip_delim=False,
                                       return_second_part_nonempty=False):
        """
        Separates a string by the first of all given delimiters. Any whitespace
        characters will be stripped away from the parts.

        :param string:                      The string to separate.
        :param delimiters:                  The delimiters.
        :param strip_delim:                 Strips the delimiter from the
                                            result if true.
        :param return_second_part_nonempty: If no delimiter is found and this
                                            is true the contents of the string
                                            will be returned in the second part
                                            of the tuple instead of the first
                                            one.
        :return:                            (first_part, second_part)
        """
        temp_string = string.replace("\\\\", "oo")
        i = temp_string.find("\\")
        while i != -1:
            temp_string = temp_string[:i] + "oo" + temp_string[i + 2:]
            i = temp_string.find("\\", i + 2)

        delim_pos = len(string)
        used_delim = ""
        for delim in delimiters:
            pos = temp_string.find(delim)
            if 0 <= pos < delim_pos:
                delim_pos = pos
                used_delim = delim

        if return_second_part_nonempty and delim_pos == len(string):
            return "", string.strip(" \n")

        first_part = string[:delim_pos]
        second_part = string[delim_pos +
                             (len(used_delim) if strip_delim else 0):]

        if not position_is_escaped(second_part, len(second_part) - 1):
            first_part = unescaped_rstrip(first_part)
            second_part = unescaped_rstrip(second_part)

        return (first_part.lstrip().rstrip("\n"),
                second_part.lstrip().rstrip("\n"))