StringUtil

object StringUtil

Small, locale-independent string helpers. This is a pure-Kotlin port of the portable parts of libtorrent's string_util.cpp and string_util.hpp (plus base64encode from escape_string.cpp).

These deliberately reimplement the C ctype predicates (isalpha, isspace, …) over the ASCII range only, exactly like libtorrent does, so that behaviour is identical on every platform regardless of the C locale. That is why libtorrent wrote its own. Anything in string_util.cpp that depends on sockets, addresses, the global RNG, locale conversion, or raw C memory management (parse_listen_interfaces, print_listen_interfaces, url_random, allocate_string_copy, convert_to_native, is_i2p_url, …) is intentionally not ported here.

Where libtorrent returns std::pair<string_view, string_view> (zero-copy views into the original buffer), the Kotlin equivalents return Pair of copies; the splitting semantics are preserved byte-for-byte.

Functions

Link copied to clipboard

libtorrent base64encode: standard base64 with = padding. Operates on raw bytes (the C++ takes a std::string but treats it as a byte buffer).

Convenience: base64-encode the UTF-8 bytes of s.

Link copied to clipboard

libtorrent convert_path_to_posix (escape_string.cpp): replace every \ with /.

Link copied to clipboard

libtorrent ensure_trailing_slash (inline in string_util.hpp).

Link copied to clipboard

libtorrent is_alpha: ASCII letter.

Link copied to clipboard

libtorrent is_digit (inline in string_util.hpp): ASCII decimal digit.

Link copied to clipboard

libtorrent is_print: printable ASCII (space through '~', i.e. 32..126).

Link copied to clipboard

libtorrent is_space: ASCII whitespace. Note this matches the C set ' ' \t \n \r \f \v, and not Kotlin's Char.isWhitespace, which is Unicode-aware.

Link copied to clipboard

libtorrent ltrim: leading-whitespace-only strip.

Link copied to clipboard

libtorrent parse_comma_separated_string: split input on commas, trimming leading and trailing ASCII whitespace from each element. Empty elements are kept (the C++ pushes back the possibly-empty substring), matching the outgoing_interfaces-style parsing.

Link copied to clipboard
fun splitString(last: String, sep: Char): Pair<String, String>

libtorrent split_string: find separator sep in last. If found, return the part before it and the remainder after it (separator dropped). If not found, return (last, "").

Link copied to clipboard

libtorrent split_string_quotes: like splitString, but if the string starts with a double-quote ("), and the separator itself is not ", then separators are ignored until the closing quote. Faithful port of the index-walking in the C++.

Link copied to clipboard

libtorrent string_begins_no_case: true if s begins with prefix, compared case-insensitively over ASCII. (In the C++ the first argument is the prefix and the loop runs to the prefix's NUL.)

Link copied to clipboard

libtorrent string_ends_with: true if s1 ends with s2.

Link copied to clipboard

libtorrent string_equal_no_case: ASCII case-insensitive equality.

Link copied to clipboard

libtorrent strip_string: drop leading and trailing ASCII whitespace, using the full isSpace set (space tab nl cr ff vt).

Link copied to clipboard
fun toLower(c: Char): Char

libtorrent to_lower: ASCII-only lower-casing (leaves non-ASCII untouched).

Link copied to clipboard

ASCII-only lower-cased copy of s; locale-independent, unlike String.lowercase.

Link copied to clipboard

libtorrent to_string(std::int64_t): decimal text of n with a well-defined, locale-independent result. It handles Long.MIN_VALUE correctly. That magnitude is not representable as a positive signed Long, so the digits are produced via unsigned arithmetic, exactly as the C++ does.

Link copied to clipboard
fun trim(s: String): String

libtorrent trim (escape_string.cpp): strips only " \t\n\r". This is the narrower set the trim helper uses (no form-feed, no vertical-tab), kept distinct from stripString to stay faithful to both call sites.