all repos

gbf @ cccbd0ebc553e7aa30ef637731c4c736986e7dda

⭐ gleaming brainfuck

gbf/src/char.gleam(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import gleam/string

pub fn to_code(s: String) {
  case <<s:utf8>> {
    // lowercase a(ansii 97) to z(ansii 122)
    <<char:int>> if char >= 97 -> char - 96

    // uppercase A(ansii 65) to Z(ansii 90), and special symbols
    <<char:int>> -> char - 38

    _ -> 0
  }
}

pub fn from_code(code: Int) {
  case code {
    c if c == 0x0A || c >= 0x20 && c <= 0x7E -> {
      case string.utf_codepoint(code) {
        Ok(codepoint) -> string.from_utf_codepoints([codepoint])
        Error(_) -> ""
      }
    }
    _ -> ""
  }
}