MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / to_raw_cstring

Function to_raw_cstring

methods.py:1638–1679  ·  view source on GitHub ↗
(value: Union[str, List[str]])

Source from the content-addressed store, hash-verified

1636
1637
1638def 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
1682def get_default_include_paths(env):

Callers 3

build_rd_headerFunction · 0.90
build_raw_headerFunction · 0.90
build_gles3_headerFunction · 0.90

Calls 6

rangeFunction · 0.85
joinMethod · 0.80
encodeMethod · 0.45
rfindMethod · 0.45
decodeMethod · 0.45
formatMethod · 0.45

Tested by

no test coverage detected