String Module
Provides utility functions for advanced string manipulation.
Includes case conversions, indentation and whitespace normalization,
safe type parsing, character frequency analysis, and code pattern detection.
to_type()
def to_type(string: str, /) -> AnyWill convert a string to the found type, including complex nested structures.
string– The string to convert.
Example Usage
import xulbux as xx
val1 = xx.string.to_type("12345")
val2 = xx.string.to_type("[1, 2, 3]")
val3 = xx.string.to_type('{"enabled": true, "timeout": 30}')val1 = 12345 # int
val2 = [1, 2, 3] # list[int]
val3 = {"enabled": True, "timeout": 30} # dict[str, Any]normalize_spaces()
def normalize_spaces(string: str, /, tab_spaces: int = 4) -> strReplaces all special space characters with normal spaces.
string– The string to normalize.tab_spaces– Number of spaces to replace tab chars with.
escape()
def escape(string: str, /, str_quotes: Literal["'", '"'] | None = None) -> strEscapes Python's special characters (e.g., \n, \t, …) and quotes inside the string.
string– The string to escape.str_quotes– The type of quotes the string will be put inside of (orNoneto not escape quotes):
Can be either"or'and should match the quotes, the string will be put inside of.
So if your string will be"string",str_quotesshould be".
That way, if the string includes the same quotes, they will be escaped.
is_empty()
def is_empty(string: str | None, /, *, spaces_are_empty: bool = False) -> boolReturns True if the string is considered empty and False otherwise.
string– The string to check (orNone, which is considered empty).spaces_are_empty– If true, strings consisting only of spaces are also considered empty.
count_char_repeats()
def count_char_repeats(string: str, char: str, /) -> int- If the string consists of only the same
char, it returns the number of times it is present. - If the string is empty or doesn't consist of only the same character, it returns
0.
string– The string to check.char– The character to check for repetition.
decompose()
def decompose(string: str, /, seps: str = "-_", *, lower_all: bool = True) -> list[str]Will decompose the string (any type of casing, also mixed) into parts.
string– The string to decompose.seps– Additional separators to split the string at.lower_all– If true, all parts will be converted to lowercase.
Example Usage
import xulbux as xx
parts = xx.string.decompose("myHTTPServer_port-config")["my", "http", "server", "port", "config"]to_camel_case()
def to_camel_case(string: str, /, *, upper: bool = True) -> strWill convert the string of any type of casing to CamelCase.
string– The string to convert.upper– If true, it will convert to UpperCamelCase, otherwise to lowerCamelCase.
Example Usage
import xulbux as xx
upper_camel = xx.string.to_camel_case("my_var_name") # MyVarName
lower_camel = xx.string.to_camel_case("my_var_name", upper=False) # myVarNameto_delimited_case()
def to_delimited_case(string: str, /, delimiter: str = "_", *, screaming: bool = False) -> strWill convert the string of any type of casing to delimited case.
string– The string to convert.delimiter– The delimiter to use between parts.screaming– Whether to convert all parts to uppercase.
Example Usage
import xulbux as xx
kebab = xx.string.to_delimited_case("MyVarName", delimiter="-") # my-var-name
screaming_snake = xx.string.to_delimited_case("MyVarName", screaming=True) # MY_VAR_NAMEget_lines()
def get_lines(string: str, /, *, remove_empty_lines: bool = False) -> list[str]Will split the string into lines.
string– The string to split.remove_empty_lines– If true, it will remove all empty lines from the result.
remove_consecutive_empty_lines()
def remove_consecutive_empty_lines(string: str, /, max_consecutive: int = 0) -> strWill remove consecutive empty lines from the string.
string– The string to process.max_consecutive– The maximum number of allowed consecutive empty lines:
- If
0, it will remove all consecutive empty lines. - If bigger than
0, it will only allowmax_consecutiveconsecutive empty lines and everything above it will be cut down tomax_consecutiveempty lines.
- If
chunk()
def chunk(string: str, chunk_size: int, /) -> list[str]Splits the string into chunks of chunk_size characters.
string– The string to split.chunk_size– The number of characters per chunk.
Example Usage
import xulbux as xx
chunks = xx.string.chunk("abcdefghi", 3) # ["abc", "def", "ghi"]add_indent()
def add_indent(string: str, indent: int, /) -> strAdds indent spaces at the beginning of each line.
string– The string to indent.indent– The amount of spaces to add at the beginning of each line.
get_tab_spaces()
def get_tab_spaces(string: str, /) -> intWill try to get the amount of spaces used for indentation.
string– The string to analyze.
change_tab_spaces()
def change_tab_spaces(string: str, space_count: int, /, *, remove_empty_lines: bool = False) -> strRescales indentation across lines to use space_count spaces per indentation level.
string– The string to modify the indentation of.space_count– The new number of spaces per indentation level.remove_empty_lines– If true, empty lines will be removed in the process.
extract_func_calls()
def extract_func_calls(code: str, /) -> list[tuple[str, str]]Will try to get all function calls and return them as a list.
code– The code to analyze.
Example Usage
import xulbux as xx
code_snippet = "result = math.sqrt(foo.bar(10)) + calculate()"
calls = xx.string.extract_func_calls(code_snippet)[
("sqrt", "foo.bar(10)"),
("calculate", ""),
("bar", "10"),
]is_js()
def is_js(code: str, /, *, funcs: set[str] | frozenset[str] = _DEFAULT_JS_FUNCS) -> boolWill check if the code is very likely to be JavaScript.
code– The code to analyze.funcs– A list of custom function names to check for.
Example Usage
import xulbux as xx
js_code = "const add = (a, b) => { return a + b; };"
result = xx.string.is_js(js_code)True