Repository: psf/black
File: src/black/trans.py (Python)

Code before:
        is_fstring = "f" in get_string_prefix(string).lower()

        def breaks_fstring_expression(i: Index) -> bool:
            """
            Returns:
                True iff returning @i would result in the splitting of an
                f-expression (which is NOT allowed).
            """
            if not is_fstring:
                return False

            for (start, end) in fexpr_slices():
                if start <= i < end:
                    return True

            return False

        nameescape_slices = self._get_nameescape_slices(string)

        def breaks_nameescape_expression(i: Index) -> bool:
            """
            Returns:
                True iff returning @i would result in the splitting of an
                \\N{...} expression (which is NOT allowed).
            """
            for (start, end) in nameescape_slices:
                if start <= i < end:
                    return True

            return False

        def passes_all_checks(i: Index) -> bool:
            """
            Returns:
                True iff ALL of the conditions listed in the 'Transformations'
                section of this classes' docstring would be be met by returning @i.
            """
            is_space = string[i] == " "

            is_not_escaped = True
            j = i - 1
            while is_valid_index(j) and string[j] == "\\":
                is_not_escaped = not is_not_escaped
                j -= 1

            is_big_enough = (
                len(string[i:]) >= self.MIN_SUBSTR_SIZE
                and len(string[:i]) >= self.MIN_SUBSTR_SIZE
            )
            return (
                is_space

Review comment (security):
```suggestion
            return any(start <= i < end for start, end in nameescape_slices)
```
A succinct version, if you like it more! But anyways the parentheses are unnecessary.

Code after:
        assert is_valid_index(max_break_idx)
        assert_is_leaf_string(string)

        _illegal_split_indices = self._get_illegal_split_indices(string)

        def breaks_unsplittable_expression(i: Index) -> bool:
            """
            Returns:
                True iff returning @i would result in the splitting of an
                unsplittable expression (which is NOT allowed).
            """
            return i in _illegal_split_indices

        def passes_all_checks(i: Index) -> bool:
            """
            Returns:
                True iff ALL of the conditions listed in the 'Transformations'
                section of this classes' docstring would be be met by returning @i.
            """
            is_space = string[i] == " "

            is_not_escaped = True
            j = i - 1
            while is_valid_index(j) and string[j] == "\\":
                is_not_escaped = not is_not_escaped
                j -= 1

            is_big_enough = (
                len(string[i:]) >= self.MIN_SUBSTR_SIZE
                and len(string[:i]) >= self.MIN_SUBSTR_SIZE
            )
            return (
                is_space
                and is_not_escaped
                and is_big_enough
                and not breaks_unsplittable_expression(i)
            )

        # First, we check all indices BELOW @max_break_idx.
        break_idx = max_break_idx
        while is_valid_index(break_idx - 1) and not passes_all_checks(break_idx):
            break_idx -= 1

        if not passes_all_checks(break_idx):
            # If that fails, we check all indices ABOVE @max_break_idx.
            #
            # If we are able to find a valid index here, the next line is going
            # to be longer than the specified line length, but it's probably
            # better than doing nothing at all.
            break_idx = max_break_idx + 1
            while is_valid_index(break_idx + 1) and not passes_all_checks(break_idx):