OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Detect a UTF-8 Byte-Order Mark in Saved Text

Rust

Read one UTF-8 file or standard input and report whether its first three bytes are the UTF-8 byte-order mark, plus the total byte count. Validate the entire input as UTF-8; treat it as read-only data and do not modify or execute it.

Rust code excerpt: Detect a UTF-8 Byte-Order Mark in Saved Text. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

A UTF-8 byte-order mark (BOM) is optional, but some downstream text tools handle it differently. This small CLI checks only whether the input begins with the exact UTF-8 BOM bytes (`EF BB BF`) and reports the byte length. It reads the data without changing it and rejects invalid UTF-8.

Before you start

  • Standard library only; no external crates, unsafe code, include macros, environment variables, subprocesses, networking, or filesystem writes.
  • Accept exactly one CLI argument: a local UTF-8 file path or `-` for standard input.
  • Read all bytes, reject invalid UTF-8, and report total byte count and whether the first three bytes are exactly the UTF-8 BOM.
  • Write successful results to stdout and actionable failures to stderr; use a nonzero exit only on failure.
  • Treat all input as untrusted text and never execute or alter it.

Build and check

  1. Save the Rust source as `recipe.rs`.
  2. Compile it with `rustc --edition=2021 recipe.rs -o recipe`.
  3. Create a synthetic UTF-8 input whose bytes are `EF BB BF 68 69 0A`.
  4. Run `./recipe sample.txt` and compare stdout with the exact expected output.
  5. Also try `./recipe -` with the same bytes on stdin, a file without a BOM, invalid UTF-8, a missing path, and an incorrect number of arguments; confirm failures are actionable and go to stderr.

Limits to understand

  • The program reads the entire input into memory, so it is intended for reasonably sized saved text.
  • It reports only whether the exact UTF-8 BOM byte sequence is at offset zero; it does not detect other encodings or BOM-like bytes elsewhere.
  • A BOM is valid UTF-8 and remains part of the reported byte count.
  • No execution or test results are claimed here.

What this example adds

No upstream implementation was supplied. This recipe is a focused BOM-and-byte-length audit rather than another line, whitespace, character-grouping, or line-ending summary. It uses only the Rust standard library, validates the complete byte stream as UTF-8, checks the BOM at the byte level, and keeps input read-only.

Run it

Intended runner: Rust 1.90.0, edition 2021. Compile with `rustc --edition=2021 recipe.rs -o recipe`. Run `./recipe <file>` to inspect a file, or `./recipe -` and provide text on standard input. Exactly one argument is required. Read and validation failures are written to stderr with a nonzero exit status; successful counts go to stdout.

Example input

Synthetic input file bytes: `EF BB BF 68 69 0A` (UTF-8 text containing a BOM followed by `hi` and a newline).

Expected output

bytes: 6
utf8_bom: yes
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Rust example
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 OpenViST contributors
use std::env;
use std::fs::File;
use std::io::{self, Read};
use std::process;

fn run() -> Result<(), String> {
    let args: Vec<String> = env::args().skip(1).collect();
    if args.len() != 1 {
        return Err("usage: recipe <file|->".to_string());
    }

    let mut bytes = Vec::new();
    if args[0] == "-" {
        io::stdin()
            .lock()
            .read_to_end(&mut bytes)
            .map_err(|e| format!("cannot read standard input: {e}"))?;
    } else {
        File::open(&args[0])
            .map_err(|e| format!("cannot open '{}': {e}", args[0]))?
            .read_to_end(&mut bytes)
            .map_err(|e| format!("cannot read '{}': {e}", args[0]))?;
    }

    std::str::from_utf8(&bytes)
        .map_err(|e| format!("input is not valid UTF-8: {e}"))?;

    let has_bom = bytes.starts_with(&[0xEF, 0xBB, 0xBF]);
    println!("bytes: {}", bytes.len());
    println!("utf8_bom: {}", if has_bom { "yes" } else { "no" });
    Ok(())
}

fn main() {
    if let Err(message) = run() {
        eprintln!("error: {message}");
        eprintln!("hint: provide one UTF-8 file path or - to read standard input");
        process::exit(2);
    }
}
What to verify after running
  • Compile using the stated Rust 2021 command.
  • For bytes `EF BB BF 68 69 0A`, verify stdout is exactly `bytes: 6` followed by `utf8_bom: yes`.
  • For UTF-8 bytes `68 69 0A`, verify stdout reports `bytes: 3` and `utf8_bom: no`.
  • For malformed UTF-8, verify the program exits nonzero and reports the validation failure on stderr.
  • Verify a missing file and incorrect argument counts produce actionable stderr messages and do not create or change files.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-12.

    Executed 5 synthetic local fixture cases in isolated Rust 1.90.0 on GitHub Actions, without network access. Declared exit codes, output assertions and input integrity checks passed. Live services, installed third-party packages and production behavior remain outside scope. Editorial approval is still required.

    Related browser tools

    Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.