(value: Union[str, List[str]])
| 1636 | |
| 1637 | |
| 1638 | def to_raw_cstring(value: Union[str, List[str]]) -> str: |
| 1639 | MAX_LITERAL = 16 * 1024 |
| 1640 | |
| 1641 | if isinstance(value, list): |
| 1642 | value = "\n".join(value) + "\n" |
| 1643 | |
| 1644 | split: List[bytes] = [] |
| 1645 | offset = 0 |
| 1646 | encoded = value.encode() |
| 1647 | |
| 1648 | while offset <= len(encoded): |
| 1649 | segment = encoded[offset : offset + MAX_LITERAL] |
| 1650 | offset += MAX_LITERAL |
| 1651 | if len(segment) == MAX_LITERAL: |
| 1652 | # Try to segment raw strings at double newlines to keep readable. |
| 1653 | pretty_break = segment.rfind(b"\n\n") |
| 1654 | if pretty_break != -1: |
| 1655 | segment = segment[: pretty_break + 1] |
| 1656 | offset -= MAX_LITERAL - pretty_break - 1 |
| 1657 | # If none found, ensure we end with valid utf8. |
| 1658 | # https://github.com/halloleo/unicut/blob/master/truncate.py |
| 1659 | elif segment[-1] & 0b10000000: |
| 1660 | last_11xxxxxx_index = [i for i in range(-1, -5, -1) if segment[i] & 0b11000000 == 0b11000000][0] |
| 1661 | last_11xxxxxx = segment[last_11xxxxxx_index] |
| 1662 | if not last_11xxxxxx & 0b00100000: |
| 1663 | last_char_length = 2 |
| 1664 | elif not last_11xxxxxx & 0b0010000: |
| 1665 | last_char_length = 3 |
| 1666 | elif not last_11xxxxxx & 0b0001000: |
| 1667 | last_char_length = 4 |
| 1668 | |
| 1669 | if last_char_length > -last_11xxxxxx_index: |
| 1670 | segment = segment[:last_11xxxxxx_index] |
| 1671 | offset += last_11xxxxxx_index |
| 1672 | |
| 1673 | split += [segment] |
| 1674 | |
| 1675 | if len(split) == 1: |
| 1676 | return f'R"<!>({split[0].decode()})<!>"' |
| 1677 | else: |
| 1678 | # Wrap multiple segments in parenthesis to suppress `string-concatenation` warnings on clang. |
| 1679 | return "({})".format(" ".join(f'R"<!>({segment.decode()})<!>"' for segment in split)) |
| 1680 | |
| 1681 | |
| 1682 | def get_default_include_paths(env): |
no test coverage detected