Function in string compiler

byte_at

fn byte_at(string s, int i) -> int
Import
use string::{byte_at};
Source: compiler/src/builtin_docs/string.hy

Returns the byte at byte offset i of s, or -1 when i is out of range.

Offsets count UTF-8 bytes, not characters, and s.len() is the byte length. The result is 0..=255 for any offset in 0..s.len().

Parameters#

NameType
sstring
iint

Returns#

int

Examples#

use string::{byte_at};
use tests::assert;
assert!(byte_at("abc", 0) == 97);
assert!(byte_at("abc", 3) == -1);
assert!(byte_at("abc", -1) == -1);

See also#

  • find_from: Returns the byte offset of the first needle in hay at or after start, or -1.
  • format: Builds a string from the literal fmt and the arguments that follow it.
  • from_bytes: Decodes bytes as UTF-8 text.
  • match_at: Returns whether needle occurs in s starting exactly at byte offset at.
  • rfind: Returns the byte offset of the last needle in hay, or -1.
  • slice_bytes: Returns the part of s between byte offsets start (inclusive) and end (exclusive).
  • to_bytes: Encodes s as UTF-8 into a new Vec<byte>.
↑ ↓ navigate ↵ open