example/type.string.primi

Summary

Maintainability
Test Coverage
import ._helpers: assert_error

_ = "Default value for string is empty string"
default = string()
assert(default == '', _)

//
// String concatenation.
//

result = "abc" + "xyz"
assert(result == 'abcxyz')

result = "xyz" + "abc"
assert(result == 'xyzabc')

result = "xyz" + string(5)
assert(result == 'xyz5')

result = "xyzZAQ:" + string(5.123)
assert(result == 'xyzZAQ:5.123')

//
// String formatting.
//

a = 4 / (2 - 1) * 100
damn = 153 * 2

x = "zaplaťte pouze {} Kč za skvělou {}".format(a, damn)
assert(x == "zaplaťte pouze 400 Kč za skvělou 306", "String formatting with string_format()")

x = f"zaplaťte pouze {a} Kč za skvělou {damn}"
assert(x == "zaplaťte pouze 400 Kč za skvělou 306", "String formatting with f-strings.")

//
// String vs string comparison.
//

result = "abc" == "xyz"
assert(result == false)

result = "xyz" == "abc"
assert(result == false)

result = "abc" == "abc"
assert(result == true)

result = "abc" == "abc"
assert(result == true)

result = "xyz" == "xyzz"
assert(result == false)

result = "xyzz" == "xyz"
assert(result == false)

//
// String vs regex comparison.
//

x = "ahojahojahojahojahoj"
if (x == rx'(ahoj){5}') { // A little bit of regex sneaked in here.
    x = "nazdárek!"
}
assert(x == "nazdárek!", "String vs regex matching")

//
// String vs string subtraction.
//

x = x - "ek"
assert(x == 'nazdár!', "Subtracting a substring from strings")

//
// Multiplication of strings.
//

x = "ahóóój" * 6
assert(x == 'ahóóójahóóójahóóójahóóójahóóójahóóój', "Multiplying strings STRING * NUMBER")
assert(x - "óó" == 'ahójahójahójahójahójahój', "Subtracting is aligned to the left")

x = 6 * "ahóóój"
assert(x == 'ahóóójahóóójahóóójahóóójahóóójahóóój', "Multiplying strings NUMBER * STRING")
assert(x - "óó" == 'ahójahójahójahójahójahój', "Subtracting is aligned to the left")

//
// Multiline strings.
//

xyz = "ahojky"
x = xyz + "\n"

// These can be written both ways...
assert(x == 'ahojky
', 'Even assertion works with literal multiline strings')
assert(x == 'ahojky\n', 'Even assertion works with new-line escape sequences')

//
// Iteration over string.
//

string = "Když začínáme myslet, nemáme k dispozici nic jiného než myšlenku v " +
         "její čisté neurčenosti, neboť k určení již patří jedno nebo nějaké " +
         "jiné, ale na začátku ještě nemáme žádné jiné..."

result = ""
tmp = ""
changes = 0
for (c in string) {

    tmp = c

    // Semicolons necessary with single line IF blocks.
    if (c == "ě") { tmp = "e"; }
    if (c == "ž") { tmp = "z"; }
    if (c == "č") { tmp = "c"; }
    if (c == "í") { tmp = "i"; }

    if (tmp != c) {
        changes = changes + 1
    }

    result = result + tmp

}

assert(changes == 15)
assert(result == "Kdyz zacináme myslet, nemáme k dispozici nic jiného nez myšlenku v " +
    "jeji cisté neurcenosti, neboť k urceni jiz patři jedno nebo nejaké " +
    "jiné, ale na zacátku ješte nemáme zádné jiné...")

//
// String comparison with strings.
//

_ = "Strings are compared lexicographically (based on ASCII, so 'A' is lower than 'a')"
assert('c' > 'a', _)
assert('b' > 'a', _)
assert(('a' > 'a') == false, _)
assert(('a' < 'a') == false, _)
assert('b' < 'c', _)
assert('b' <= 'c', _)
assert('b' <= 'b', _)
assert('c' >= 'b', _)
assert('c' >= 'c', _)

assert('A' < 'a', _)
assert('B' < 'b', _)
assert('C' < 'c', _)
assert('a' > 'A', _)
assert('b' > 'B', _)
assert('c' > 'C', _)
assert(('A' > 'a') == false, _)
assert(('B' > 'b') == false, _)
assert(('C' > 'c') == false, _)
assert(('a' < 'A') == false, _)
assert(('b' < 'B') == false, _)
assert(('c' < 'C') == false, _)

assert('aa' > 'a', _)
assert('ab' > 'aa', _)
assert('a' < 'aa', _)
assert('aa' < 'ab', _)

//
// String methods.
//

//
// string.replace()
//

_ = "Basic string.replace() functionality"
x = "abcdef".replace("c", "X")
assert(x == "abXdef", _)

_ = "All occurrences of the needle are replaced"
x = "přítmí ve městě za dvě stě".replace("stě", "šci")
assert(x == "přítmí ve měšci za dvě šci", _)

_ = "string.replace() alsoaccepts regex object as the needle"
x = "přítmí ve městě za dvě stě".replace(rx"\wt\w", "lol")
assert(x == "přlolí ve mělol za dvě lol", _)

text = "Ahojky, jak see máš?"
pairs = {
    'A': 'XXX',
    'k': 'kkk',
    'ee': 'eee',
    'š': 'š?!',
    'š?': 'QUESTIONMARK', // Longer than the key "š" - "š?" will have priority.
}

assert(text.translate(pairs) == 'XXXhojkkky, jakkk seee máQUESTIONMARK')