all repos

gbf @ 8fe0c0c2e8ee45fbaf0cb0cd0de0f4de661894d2

⭐ 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
26
import gleam/bit_array
import gleam/string

pub fn to_code(s: String) {
  let bytes = bit_array.from_string(s)
  case bit_array.byte_size(bytes) {
    1 ->
      case bytes {
        <<value>> -> value
        _ -> 0
      }
    _ -> 0
  }
}

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