Skip to content

# text

## `xsdata.utils.text`

### `CharType`

Character types.

Source code in `xsdata/utils/text.py`

    165
    166
    167
    168
    169
    170
    171

    class CharType:
        """Character types."""

        UPPER = 1
        LOWER = 2
        NUMERIC = 3
        OTHER = 4

### `prefix(value, sep=':')`

Return the first part of the string before the separator.

Source code in `xsdata/utils/text.py`

    66
    67
    68

    def prefix(value: str, sep: str = ":") -> str:
        """Return the first part of the string before the separator."""
        return split(value, sep)[0]

### `suffix(value, sep=':')`

Return the last part of the string after the separator.

Source code in `xsdata/utils/text.py`

    71
    72
    73

    def suffix(value: str, sep: str = ":") -> str:
        """Return the last part of the string after the separator."""
        return split(value, sep)[1]

### `split(value, sep=':')`

Split the given value with the given separator once.

Source code in `xsdata/utils/text.py`

    76
    77
    78
    79

    def split(value: str, sep: str = ":") -> tuple:
        """Split the given value with the given separator once."""
        left, _, right = value.partition(sep)
        return (left, right) if right else (None, left)

### `capitalize(value, **kwargs)`

Capitalize the given string.

Source code in `xsdata/utils/text.py`

    82
    83
    84

    def capitalize(value: str, **kwargs: Any) -> str:
        """Capitalize the given string."""
        return value[0].upper() + value[1:]

### `original_case(value, **kwargs)`

Return the input string but ensure it's a valid Python variable.

Source code in `xsdata/utils/text.py`

    87
    88
    89
    90
    91
    92

    def original_case(value: str, **kwargs: Any) -> str:
        """Return the input string but ensure it's a valid Python variable."""
        # Strip out all characters that are not alphanumeric or underscores
        value = re.sub(r"\W", "", value)
        # Then strip out leading digit and underscore characters
        return re.sub(r"^[^a-zA-Z_]+", "", value)

### `pascal_case(value, **kwargs)`

Convert the given string to pascal case.

Source code in `xsdata/utils/text.py`

    95
    96
    97

    def pascal_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to pascal case."""
        return "".join(map(str.title, split_words(value)))

### `camel_case(value, **kwargs)`

Convert the given string to camel case.

Source code in `xsdata/utils/text.py`

    100
    101
    102
    103

    def camel_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to camel case."""
        result = "".join(map(str.title, split_words(value)))
        return result[0].lower() + result[1:]

### `mixed_case(value, **kwargs)`

Convert the given string to mixed case.

Source code in `xsdata/utils/text.py`

    106
    107
    108

    def mixed_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to mixed case."""
        return "".join(split_words(value))

### `mixed_pascal_case(value, **kwargs)`

Convert the given string to mixed pascal case.

Source code in `xsdata/utils/text.py`

    111
    112
    113

    def mixed_pascal_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to mixed pascal case."""
        return capitalize(mixed_case(value))

### `mixed_snake_case(value, **kwargs)`

Convert the given string to mixed snake case.

Source code in `xsdata/utils/text.py`

    116
    117
    118

    def mixed_snake_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to mixed snake case."""
        return "_".join(split_words(value))

### `snake_case(value, **kwargs)`

Convert the given string to snake case.

Source code in `xsdata/utils/text.py`

    121
    122
    123

    def snake_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to snake case."""
        return "_".join(map(str.lower, split_words(value)))

### `screaming_snake_case(value, **kwargs)`

Convert the given string to screaming snake case.

Source code in `xsdata/utils/text.py`

    126
    127
    128

    def screaming_snake_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to screaming snake case."""
        return snake_case(value, **kwargs).upper()

### `kebab_case(value, **kwargs)`

Convert the given string to kebab case.

Source code in `xsdata/utils/text.py`

    131
    132
    133

    def kebab_case(value: str, **kwargs: Any) -> str:
        """Convert the given string to kebab case."""
        return "-".join(split_words(value))

### `split_words(value)`

Split a string on capital letters and not alphanumeric characters.

Source code in `xsdata/utils/text.py`

    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162

    def split_words(value: str) -> list[str]:
        """Split a string on capital letters and not alphanumeric characters."""
        words: list[str] = []
        buffer: list[str] = []
        previous = None

        def flush() -> None:
            if buffer:
                words.append("".join(buffer))
                buffer.clear()

        for char in value:
            tp = classify(char)
            if tp == CharType.OTHER:
                flush()
            elif not previous or tp == previous:
                buffer.append(char)
            elif tp == CharType.UPPER and previous != CharType.UPPER:
                flush()
                buffer.append(char)
            else:
                buffer.append(char)

            previous = tp

        flush()
        return words

### `classify(character)`

String classifier.

Source code in `xsdata/utils/text.py`

    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186

    def classify(character: str) -> int:
        """String classifier."""
        code_point = ord(character)
        if 64 < code_point < 91:
            return CharType.UPPER

        if 96 < code_point < 123:
            return CharType.LOWER

        if 47 < code_point < 58:
            return CharType.NUMERIC

        return CharType.OTHER

### `escape_string(value)`

Escape a string for code generation.

Source code in `xsdata/utils/text.py`

    203
    204
    205
    206
    207
    208
    209

    def escape_string(value: str) -> str:
        """Escape a string for code generation."""

        def replace(match: Match) -> str:
            return ESCAPE_DCT[match.group(0)]

        return ESCAPE.sub(replace, value)

### `alnum(value)`

Return the ascii alphanumerical characters in lower case.

Source code in `xsdata/utils/text.py`

    215
    216
    217

    def alnum(value: str) -> str:
        """Return the ascii alphanumerical characters in lower case."""
        return "".join(filter(__alnum_ascii__.__contains__, value)).lower()