String manipulations
codext also defines multiple dummy string manipulation/transformation codecs, essentially for use with the CLI tool and for the sake of simplicity.
Case-related operations¶
These transformation functions are simple string transformations, including str's methods.
| Codec | Conversions | Aliases | Comment |
|---|---|---|---|
camelcase |
text --> camel-case text | camel |
no decoding |
capitalize |
text <-> capitalized text | decoding "uncapitalizes" the text | |
casebits |
mixed-case text --> bits | case-bits, case2bits, casebits-inv |
no decoding ; a 0 for each lowercase letter and a 1 for each uppercase letter (the opposite with -inv), whitespaces being kept and other characters ignored |
lowercase |
text <-> lowercase text | lower |
decoding is uppercase |
pascalcase |
text --> pascal-case text | pascal |
no decoding |
screamingsnakecase |
text --> screaming-snake-case text | screaming-snake, screaming_snake_case |
no decoding |
slugify |
text --> slug | slug, kebab, kebabcase |
no decoding |
snakecase |
text --> snake-case text | snake |
no decoding |
swapcase |
text <-> case-swapped text | swap, invert, invertcase |
|
title |
text <-> titled text | decoding "untitles" the text | |
uppercase |
text <-> uppercase text | upper |
decoding is lowercase |
Of course, these transformations have no interest while using them in Python as the str methods can be called. It can be useful while using codext from the terminal (see CLI tool).
Some simple examples:
$ echo -en "test string" | codext encode swap-case
TEST STRING
$ echo -en "test string" | codext encode camel_case
testString
$ echo -en "test string" | codext encode kebab_case
test-string
$ echo -en "hElLo wOrLd, ThIs Is HiDdEn" | codext encode casebits
01010 01010 1010 10 101010
Dummy string operations¶
These transformation functions are simple string transformations.
| Codec | Conversions | Aliases | Comment |
|---|---|---|---|
leftpad |
text --> text with left-padded tokens | lpad, left-pad |
no decoding ; parametrized with a length and, optionally, a character (0 by default), e.g. leftpad-8 or leftpad-8-x ; tokens are runs of non-whitespace characters, the longer ones being left as is |
replace |
text <-> text with multi-chars replaced | parametrized with a string and its replacement | |
reverse |
text <-> reversed text | ||
reverse-words |
text <-> reversed words | same as reverse but not on the whole text, only on the words (text split by whitespace) |
|
rightpad |
text --> text with right-padded tokens | rpad, right-pad |
same as leftpad, but padding on the right |
strip-spaces |
text <-> all whitespaces stripped | ||
substitute |
text <-> text with token substituted | ||
tokenize |
text <-> text split in tokens of length N | parametrized with N |
As in the previous section, these transformations have no interest while using them in Python but well while using codext from the terminal (see CLI tool).
A simple example:
$ echo -en "test string" | codext encode reverse-words | codext encode reverse replace-\ _
string_test
Another example:
$ echo -en "3132333435" | codext encode tokenize-2
31 32 33 34 35
Or padding tokens on the left (or on the right with rightpad), e.g. bits before decoding them as bytes:
$ echo -en "1001000 1101001" | codext encode leftpad-8
01001000 01101001
Or using encodings chaining:
$ echo -en "test string" | codext encode reverse-words reverse substitute-string/phrase
phrase test