Utilitários de texto para fatiar, dividir, juntar, converter caixa e buscar.
import strings
Lembre-se: em Bern uma string é uma lista de caracteres, então os operadores de lista também valem para strings - "ab" <> "cd" é "abcd", e "hello"[0] é 'h'. A biblioteca strings adiciona as operações de texto de nível mais alto.
O trecho do índice início até (sem incluir) fim.
substring("Hello", 1, 4)
-- Saída: "ell"
O caractere em índice (o mesmo que indexar com []).
char_at("Bern", 0)
-- Saída: 'B'
Quebra uma string em uma lista de partes a cada ocorrência de delimitador.
split("a,b,c", ",")
-- Saída: ["a", "b", "c"]
O inverso de split: cola uma lista de strings com um separador.
join(["a", "b", "c"], "-") -- Saída: "a-b-c"
Maiúscula em cada letra.
to_upper("bern")
-- Saída: "BERN"
Minúscula em cada letra.
to_lower("BERN")
-- Saída: "bern"
Maiúscula em um único caractere (não-letras passam inalteradas).
char_to_upper('a')
-- Saída: 'A'
Minúscula em um único caractere.
char_to_lower('Z')
-- Saída: 'z'
Remove espaços, quebras de linha e tabs no início e no fim.
trim(" hello ")
-- Saída: "hello"
Substitui toda ocorrência de antigo por novo.
replace("hello world", "world", "Bern")
-- Saída: "hello Bern"
Se a substring aparece em algum lugar.
contains("documentation", "ment")
-- Saída: true
Se a string começa com prefixo.
starts_with("bernlang", "bern")
-- Saída: true
Se a string termina com sufixo.
ends_with("script.brn", ".brn")
-- Saída: true
Renderiza um inteiro como string (trata negativos). O par do to_int do core.
from_int(-42) -- Saída: "-42" "v" + from_int(2) -- Saída: "v2"
As funções encadeiam bem para limpar e remodelar texto. Aqui uma linha estilo CSV bagunçada é dividida, aparada, normalizada e re-unida:
import core
import strings
line = " Bern , Lang ,DOCS "
tags = split(line, ",")
)| map(\t -> to_lower(trim(t)))
clean = join(tags, ", ")
-- Saída: "bern, lang, docs"
E um pequeno "slugify" que combina to_lower, replace e trim:
import strings
def slugify(title) do
lower = to_lower(trim(title))
return replace(lower, " ", "-")
end
slugify(" Getting Started With Bern ")
-- Saída: "getting-started-with-bern"