all repos

scratch @ 753ee96

⭐ me doing recreational ~~drugs~~ programming

scratch/brainfuck/src/gbf/internal/ascii.gleam (view raw)

1
import gleam/bit_array
2
import gleam/string
3
4
pub fn to_code(s: String) {
5
  let bytes = bit_array.from_string(s)
6
  case bit_array.byte_size(bytes) {
7
    1 ->
8
      case bytes {
9
        <<value>> -> value
10
        _ -> 0
11
      }
12
    _ -> 0
13
  }
14
}
15
16
pub fn from_code(code: Int) {
17
  case code {
18
    c if c >= 1 && c <= 255 -> {
19
      case string.utf_codepoint(code) {
20
        Ok(codepoint) -> string.from_utf_codepoints([codepoint])
21
        Error(_) -> ""
22
      }
23
    }
24
    _ -> ""
25
  }
26
}