Search results

There are no results.

std.utf8.encode_scalar

Show source code
Hide source code
fn pub encode_scalar(scalar: Int, bytes: mut ByteArray) -> Int {
  if valid_scalar?(scalar).false? { return 0 }

  let len = codepoint_size(scalar)

  # This is based on Rust's `encode_utf8_raw()` method.
  match len {
    case 1 -> bytes.push(scalar)
    case 2 -> {
      bytes.push(scalar >> 6 & 0x1F | TAG_TWO_B)
      bytes.push(scalar & 0x3F | TAG_CONT)
    }
    case 3 -> {
      bytes.push(scalar >> 12 & 0x0F | TAG_THREE_B)
      bytes.push(scalar >> 6 & 0x3F | TAG_CONT)
      bytes.push(scalar & 0x3F | TAG_CONT)
    }
    case _ -> {
      bytes.push(scalar >> 18 & 0x07 | TAG_FOUR_B)
      bytes.push(scalar >> 12 & 0x3F | TAG_CONT)
      bytes.push(scalar >> 6 & 0x3F | TAG_CONT)
      bytes.push(scalar & 0x3F | TAG_CONT)
    }
  }

  len
}
fn pub static encode_scalar(scalar: Int, bytes: mut ByteArray) -> Int

Encodes a Unicode scalar value into a series of bytes, appending these to the given ByteArray.

The return value is the number of bytes written into the ByteArray. If the scalar is invalid (e.g. it's a surrogate), the return value is 0. In this case no bytes are written into the ByteArray.

Examples

import std.utf8

let bytes = ByteArray.new

utf8.encode_scalar(0x1D11E, bytes) # => 4
bytes # => ByteArray.from_array([240, 157, 132, 158])